0

I am quite new to bash scripts. How do I execute the below command to capture the time to execute the command.

I have my deals captured in an array variable. I then am looping through the deals to execute a TradeService remote call. I want to capture the time for this call and aggregate it across all the deals.

  1. What is the best way to execute this call and capture the time within my script
  2. How do I manage time in my script, post aggregation of time I want to show it in hours/minutes/seconds.
#!/bin/bash

DEALS="1739719, 1714630, 1733697, 1723940, 1666257, 1665239"

IFS=', ' read -ra array <<< "$DEALS"

for i in "${array[@]}"
do
        echo "Executing for DEALS $i"

RUN_TIME = time echo -n '{"source_system": "PROGA", "deal_id": $array[$i], "as_of": "2019-14-01T23:59:00Z"}' | java -jar ~/support/lib/polyglot.jar --command=call --endpoint=trades-server:10443 --full_method='TradeService/GetDeals' > /dev/null

echo "Runtime is $RUN_TIME"

done
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
serah
  • 2,057
  • 7
  • 36
  • 56
  • The duplicates to the prior question you asked went into why you can't have spaces around the `=` in an assignment. For capturing output of `time` specifically, also see [BashFAQ #32](http://mywiki.wooledge.org/BashFAQ/032). – Charles Duffy Mar 14 '19 at 20:13

2 Answers2

1

Original answer

#!/bin/bash

DEALS="1739719, 1714630, 1733697, 1723940, 1666257, 1665239"

IFS=', ' read -ra array <<< "$DEALS"
total=0

for i in "${array[@]}" ; do
    echo "Executing for DEALS $i"

    startdeals=$(date +%s)
    java -jar ~/support/lib/polyglot.jar --command=call --endpoint=trades-server:10443 --full_method='TradeService/GetDeals' > /dev/null
    enddeals=$(date +%s)
    let "diff=$enddeals - $startdeals"
    let "total=$total + $diff"
    echo "Runtime for $i : $(date -d@$diff -u +%H:%M:%S)"
done

echo "Overall execution time: $(date -d@$total -u +%H:%M:%S)"

EDIT: As pointed out by @Charles Duffy now we use $SECONDS instead date, assuming that execution time will be seconds-level.

#!/bin/bash

DEALS="1739719, 1714630, 1733697, 1723940, 1666257, 1665239"

IFS=', ' read -ra array <<< "$DEALS"

for i in "${array[@]}" ; do
    echo "Executing for DEALS $i"

    startdeals="$SECONDS"
    java -jar ~/support/lib/polyglot.jar --command=call --endpoint=trades-server:10443 --full_method='TradeService/GetDeals' > /dev/null
    enddeals="$SECONDS"
    diff=$(( enddeals - startdeals ))
    echo "Runtime for $i : $diff seconds."
done

echo "Overall execution time: $SECONDS seconds."
downtheroad
  • 409
  • 4
  • 11
  • 1
    If you're only going to have seconds-level resolution anyhow, why not just use `$SECONDS` to get the number of seconds since the shell started execution? That also has a lot less performance impact than running `date`. – Charles Duffy Mar 14 '19 at 20:11
  • @CharlesDuffy you're right, edited accordingly – downtheroad Mar 14 '19 at 20:37
  • 2
    As an aside, I would generally suggest `diff=$(( enddeals - startdeals ))` rather than the non-standardized `let` (which bash has for backwards compatibility with ksh88, which predates the 1992 POSIX sh standard). See also https://wiki.bash-hackers.org/scripting/obsolete – Charles Duffy Mar 14 '19 at 20:39
0

You could use date +%s before and after an operation to find the time difference in seconds. For example :

array=( 5 10 15 20 25 )
for val in "${array[@]}"
do
 starttime=$(date +%s)
 sleep "$val" # sleeping for '$val' seconds in each iteration
 #you should replace the above with the actual process.
 endtime=$(date +%s)
 echo "Time taken for the process is $((endtime-starttime))s"
done

This would give

Time taken for the process is 5s
Time taken for the process is 10s
Time taken for the process is 15s
Time taken for the process is 20s
Time taken for the process is 25s

However, I should acknowledge that it can give just up to seconds precision.

The +%s format string requires a special mention. The date manpage says :

%s seconds since 1970-01-01 00:00:00 UTC

In essence, we just calculate the time elapsed (in seconds) with respect to a reference point in the past and then calculate the difference between end and start times. Once you have time in seconds, use well defined mathematics segregate hours and minutes.

As @ChalresDuffy pointed out in this comment, you could use the $SECONDS internal variable to calculate the time difference. Also, $EPOCHREALTIME could be used if granularity of micro-seconds is needed.

sjsam
  • 21,411
  • 5
  • 55
  • 102