-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathbenchmark.sh
More file actions
154 lines (130 loc) · 3.69 KB
/
benchmark.sh
File metadata and controls
154 lines (130 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env bash
_BENCH_NAMES=()
_BENCH_REVS=()
_BENCH_ITS=()
_BENCH_AVERAGES=()
_BENCH_MAX_MILLIS=()
function benchmark::parse_annotations() {
local fn_name=$1
local script=$2
local revs=1
local its=1
local max_ms=""
local annotation
annotation=$(awk "/function[[:space:]]+${fn_name}[[:space:]]*\(/ {print prev; exit} {prev=\$0}" "$script")
local revs_pattern='@revs=([0-9]+)'
local revolutions_pattern='@revolutions=([0-9]+)'
local its_pattern='@its=([0-9]+)'
local iterations_pattern='@iterations=([0-9]+)'
local max_ms_pattern='@max_ms=([0-9.]+)'
if [[ $annotation =~ $revs_pattern ]]; then
revs="${BASH_REMATCH[1]}"
elif [[ $annotation =~ $revolutions_pattern ]]; then
revs="${BASH_REMATCH[1]}"
fi
if [[ $annotation =~ $its_pattern ]]; then
its="${BASH_REMATCH[1]}"
elif [[ $annotation =~ $iterations_pattern ]]; then
its="${BASH_REMATCH[1]}"
fi
if [[ $annotation =~ $max_ms_pattern ]]; then
max_ms="${BASH_REMATCH[1]}"
elif [[ $annotation =~ $max_ms_pattern ]]; then
max_ms="${BASH_REMATCH[1]}"
fi
if [[ -n "$max_ms" ]]; then
echo "$revs" "$its" "$max_ms"
else
echo "$revs" "$its"
fi
}
function benchmark::add_result() {
_BENCH_NAMES+=("$1")
_BENCH_REVS+=("$2")
_BENCH_ITS+=("$3")
_BENCH_AVERAGES+=("$4")
_BENCH_MAX_MILLIS+=("$5")
}
# shellcheck disable=SC2155
function benchmark::run_function() {
local fn_name=$1
local revs=$2
local its=$3
local max_ms=$4
local durations
durations=()
for ((i=1; i<=its; i++)); do
local start_time=$(clock::now)
(
for ((r=1; r<=revs; r++)); do
"$fn_name" >/dev/null 2>&1
done
)
local end_time=$(clock::now)
local dur_ns=$(math::calculate "($end_time - $start_time)")
local dur_ms=$(math::calculate "$dur_ns / 1000000")
durations+=("$dur_ms")
if env::is_bench_mode_enabled; then
local label="$(helper::normalize_test_function_name "$fn_name")"
local line="$label [$i/$its] ${dur_ms} ms"
state::print_line "successful" "$line"
fi
done
local sum=0
for d in "${durations[@]}"; do
sum=$(math::calculate "$sum + $d")
done
local avg=$(math::calculate "$sum / ${#durations[@]}")
benchmark::add_result "$fn_name" "$revs" "$its" "$avg" "$max_ms"
}
function benchmark::print_results() {
if ! env::is_bench_mode_enabled; then
return
fi
if (( ${#_BENCH_NAMES[@]} == 0 )); then
return
fi
if env::is_simple_output_enabled; then
printf "\n"
fi
printf "\nBenchmark Results (avg ms)\n"
print_line 80 "="
printf "\n"
local has_threshold=false
for val in "${_BENCH_MAX_MILLIS[@]}"; do
if [[ -n "$val" ]]; then
has_threshold=true
break
fi
done
if $has_threshold; then
printf '%-40s %6s %6s %10s %12s\n' "Name" "Revs" "Its" "Avg(ms)" "Status"
else
printf '%-40s %6s %6s %10s\n' "Name" "Revs" "Its" "Avg(ms)"
fi
for i in "${!_BENCH_NAMES[@]}"; do
local name="${_BENCH_NAMES[$i]}"
local revs="${_BENCH_REVS[$i]}"
local its="${_BENCH_ITS[$i]}"
local avg="${_BENCH_AVERAGES[$i]}"
local max_ms="${_BENCH_MAX_MILLIS[$i]}"
if [[ -z "$max_ms" ]]; then
printf '%-40s %6s %6s %10s\n' "$name" "$revs" "$its" "$avg"
continue
fi
if (( $(echo "$avg <= $max_ms" | bc -l) )); then
local raw="≤ ${max_ms}"
local padded
padded=$(printf "%14s" "$raw")
printf '%-40s %6s %6s %10s %12s\n' "$name" "$revs" "$its" "$avg" "$padded"
continue
fi
local raw="> ${max_ms}"
local padded
padded=$(printf "%12s" "$raw")
printf '%-40s %6s %6s %10s %s%s%s\n' \
"$name" "$revs" "$its" "$avg" \
"$_COLOR_FAILED" "$padded" "${_COLOR_DEFAULT}"
done
console_results::print_execution_time
}