blob: f88362cacdb779669dbcf3f8dc29eb7f386db4f6 [file] [log] [blame]
Alex Savatieievec703692019-06-07 15:33:31 -05001#!/bin/bash
2
3function help_and_exit {
4 echo "profiled_run.sh <command>"
5 exit 1
6}
7
8#if [ -z ${1+x} ]; then echo "First parameter should be command to run"; help_and_exit; fi
9#if [ -z ${2+x} ]; then echo "Second parameter should be count of time to run"; help_and_exit; fi
10total=${2}
11count=1
12cmd="${1}"
13
14declare all_req=()
15declare errors=()
16tmp_time=$(mktemp)
17tmp_out=$(mktemp)
18
19function timed_run {
20 if [ -z ${2+x} ]; then
21 echo "--> '${1}'"
22 /usr/bin/time --quiet -f'%e %x' -o ${tmp_time} /bin/bash -c "${1}" 1>/dev/null 2>${tmp_out}
23 real=$(cat ${tmp_time} | awk '{print $1}')
24 errlevel=$(cat ${tmp_time} | awk '{print $2}')
25 if [ 0 -eq ${errlevel} ]; then
26 echo "#${count}(${real}s), '${1:0:12}...'";
27 all_req+=("#${count}, ${real}, '${1}'");
28 else
29 echo "#${count}, ERROR(${errlevel}): '${1}'"
30 errors+=("#${count}: $(cat ${tmp_out})")
31 fi
32 ((count++))
33 else
34 echo "### Running '${1:0:12}...' ${2} times"
35 for (( idx=1; idx<=${2}; idx++ ))
36 do
37 /usr/bin/time --quiet -f'%e %x' -o ${tmp_time} /bin/bash -c "${1}" 1>/dev/null 2>${tmp_out}
38 real=$(cat ${tmp_time} | awk '{print $1}')
39 errlevel=$(cat ${tmp_time} | awk '{print $2}')
40 if [ 0 -eq ${errlevel} ]; then
41 echo "#${count}/${total}, ${real}s";
42 all_req+=("#${count}/${idx}, ${real}, '${1}'");
43 else
44 echo "#${count}/${total}, ERROR(${errlevel}): '${1}'"
45 errors+=("#${count}: $(cat ${tmp_out})")
46 fi
47 ((count++))
48 done
49 fi
50}
51
52function errors {
53 echo "==== Errors"
54 for i in "${!errors[@]}"; do
55 printf "#%s\n\n" "${errors[$i]}"
56 done
57}
58
59function stats {
60 echo "==== Stats"
61 printf '%s\n' "${all_req[@]}" | awk 'BEGIN{min=999;avg=0}
62 {if($2<min){min=$2;}if($2>max){max=$2;}avg+=$2;}
63 END { print "Total requests: "NR", Timings: "min" <-- "avg/NR" --> "max;}'
64}
65
66function clean {
67 rm ${tmp_time}
68 rm ${tmp_out}
69}
70
71timed_run "${cmd}" ${total}
72stats
73errors
74clean