@@ -4,12 +4,14 @@ _BENCH_NAMES=()
44_BENCH_REVS=()
55_BENCH_ITS=()
66_BENCH_AVERAGES=()
7+ _BENCH_MAX_MILLIS=()
78
89function benchmark::parse_annotations() {
910 local fn_name=$1
1011 local script=$2
1112 local revs=1
1213 local its=1
14+ local max_ms=" "
1315
1416 local annotation
1517 annotation=$( awk " /function[[:space:]]+${fn_name} [[:space:]]*\(/ {print prev; exit} {prev=\$ 0}" " $script " )
@@ -26,21 +28,33 @@ function benchmark::parse_annotations() {
2628 its=" ${BASH_REMATCH[1]} "
2729 fi
2830
29- echo " $revs " " $its "
31+ if [[ $annotation =~ @max_ms= ([0-9.]+) ]]; then
32+ max_ms=" ${BASH_REMATCH[1]} "
33+ elif [[ $annotation =~ @max_ms= ([0-9.]+) ]]; then
34+ max_ms=" ${BASH_REMATCH[1]} "
35+ fi
36+
37+ if [[ -n " $max_ms " ]]; then
38+ echo " $revs " " $its " " $max_ms "
39+ else
40+ echo " $revs " " $its "
41+ fi
3042}
3143
3244function benchmark::add_result() {
3345 _BENCH_NAMES+=(" $1 " )
3446 _BENCH_REVS+=(" $2 " )
3547 _BENCH_ITS+=(" $3 " )
3648 _BENCH_AVERAGES+=(" $4 " )
49+ _BENCH_MAX_MILLIS+=(" $5 " )
3750}
3851
3952# shellcheck disable=SC2155
4053function benchmark::run_function() {
4154 local fn_name=$1
4255 local revs=$2
4356 local its=$3
57+ local max_ms=$4
4458 local durations=()
4559
4660 for (( i= 1 ; i<= its; i++ )) ; do
@@ -55,28 +69,75 @@ function benchmark::run_function() {
5569 local dur_ms=$( math::calculate " $dur_ns / 1000000" )
5670 durations+=(" $dur_ms " )
5771
58- local line=" bench $fn_name [$i /$its ] ${dur_ms} ms"
59- state::print_line " successful" " $line "
72+ if env::is_bench_mode_enabled; then
73+ local label=" $( helper::normalize_test_function_name " $fn_name " ) "
74+ local line=" $label [$i /$its ] ${dur_ms} ms"
75+ state::print_line " successful" " $line "
76+ fi
6077 done
6178
6279 local sum=0
6380 for d in " ${durations[@]} " ; do
6481 sum=$( math::calculate " $sum + $d " )
6582 done
6683 local avg=$( math::calculate " $sum / ${# durations[@]} " )
67- benchmark::add_result " $fn_name " " $revs " " $its " " $avg "
84+ benchmark::add_result " $fn_name " " $revs " " $its " " $avg " " $max_ms "
6885}
6986
7087function benchmark::print_results() {
88+ if ! env::is_bench_mode_enabled; then
89+ return
90+ fi
91+
7192 if (( ${# _BENCH_NAMES[@]} == 0 )) ; then
7293 return
7394 fi
95+
7496 if env::is_simple_output_enabled; then
7597 printf " \n"
7698 fi
99+
77100 printf " \nBenchmark Results (avg ms)\n"
78- printf ' %-40s %8s %8s %8s\n' " Name" " Revs" " Its" " Avg(ms)"
101+ print_line 80 " ="
102+ printf " \n"
103+
104+ local has_threshold=false
105+ for val in " ${_BENCH_MAX_MILLIS[@]} " ; do
106+ if [[ -n " $val " ]]; then
107+ has_threshold=true
108+ break
109+ fi
110+ done
111+
112+ if $has_threshold ; then
113+ printf ' %-40s %6s %6s %10s %12s\n' " Name" " Revs" " Its" " Avg(ms)" " Status"
114+ else
115+ printf ' %-40s %6s %6s %10s\n' " Name" " Revs" " Its" " Avg(ms)"
116+ fi
117+
79118 for i in " ${! _BENCH_NAMES[@]} " ; do
80- printf ' %-40s %8s %8s %8s\n' " ${_BENCH_NAMES[$i]} " " ${_BENCH_REVS[$i]} " " ${_BENCH_ITS[$i]} " " ${_BENCH_AVERAGES[$i]} "
119+ local name=" ${_BENCH_NAMES[$i]} "
120+ local revs=" ${_BENCH_REVS[$i]} "
121+ local its=" ${_BENCH_ITS[$i]} "
122+ local avg=" ${_BENCH_AVERAGES[$i]} "
123+ local max_ms=" ${_BENCH_MAX_MILLIS[$i]} "
124+
125+ if [[ -z " $max_ms " ]]; then
126+ printf ' %-40s %6s %6s %10s\n' " $name " " $revs " " $its " " $avg "
127+ continue
128+ fi
129+
130+ if (( $(echo "$avg <= $max_ms " | bc - l) )) ; then
131+ local raw=" ≤ ${max_ms} "
132+ printf -v padded " %14s" " $raw "
133+ printf ' %-40s %6s %6s %10s %12s\n' " $name " " $revs " " $its " " $avg " " $padded "
134+ continue
135+ fi
136+
137+ local raw=" > ${max_ms} "
138+ printf -v padded " %12s" " $raw "
139+ printf ' %-40s %6s %6s %10s %s%s%s\n' \
140+ " $name " " $revs " " $its " " $avg " \
141+ " $_COLOR_FAILED " " $padded " " ${_COLOR_DEFAULT} "
81142 done
82143}
0 commit comments