File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ module benchmark
2
2
3
3
import time
4
4
import term
5
+ import arrays
5
6
6
7
pub const b_ok = term.ok_message ('OK ' )
7
8
pub const b_fail = term.fail_message ('FAIL' )
@@ -24,6 +25,7 @@ pub mut:
24
25
bok string
25
26
bfail string
26
27
measured_steps []string
28
+ step_data map [string ][]f64
27
29
}
28
30
29
31
// new_benchmark returns a `Benchmark` instance on the stack.
@@ -145,6 +147,7 @@ pub fn (mut b Benchmark) record_measure(label string) i64 {
145
147
b.ok ()
146
148
res := b.step_timer.elapsed ().microseconds ()
147
149
b.measured_steps << b.step_message_with_label (benchmark.b_spent, 'in ${label} ' )
150
+ b.step_data[label] << res
148
151
b.step ()
149
152
return res
150
153
}
@@ -244,6 +247,12 @@ pub fn (b &Benchmark) total_message(msg string) string {
244
247
}
245
248
}
246
249
tmsg + = '${b.ntotal} total. ${term.colorize(term.bold, 'Elapsed time:')} ${b.bench_timer.elapsed().microseconds() / 1000} ms${njobs_label} .'
250
+ if msg in b.step_data && b.step_data[msg].len > 1 {
251
+ min := arrays.min (b.step_data[msg]) or { 0 } / 1000.0
252
+ max := arrays.max (b.step_data[msg]) or { 0 } / 1000.0
253
+ avg := (arrays.sum (b.step_data[msg]) or { 0 } / b.step_data[msg].len) / 1000.0
254
+ tmsg + = ' Min: ${min:.3f} ms. Max: ${max:.3f} ms. Avg: ${avg:.3f} ms'
255
+ }
247
256
return tmsg
248
257
}
249
258
Original file line number Diff line number Diff line change @@ -36,3 +36,29 @@ fn test_record_measure() {
36
36
assert res.contains ('ms in sleeping 1' )
37
37
assert res.contains ('SPENT' )
38
38
}
39
+
40
+ fn test_total_message () {
41
+ mut b := benchmark.start ()
42
+ for _ in 0 .. 100 {
43
+ time.sleep (time.millisecond)
44
+ x := b.record_measure ('sleeping 1' )
45
+ assert x > 1_000
46
+ }
47
+
48
+ res := b.total_message ('sleeping 1' )
49
+ println (res)
50
+
51
+ assert res.contains (' Min: ' )
52
+ assert res.contains (' Max: ' )
53
+ assert res.contains (' Avg: ' )
54
+
55
+ time.sleep (time.millisecond)
56
+ y := b.record_measure ('sleeping 2' )
57
+ assert y > 1_000
58
+ // Should not contain min max avg, insufficient information
59
+ res2 := b.total_message ('sleeping 2' )
60
+
61
+ assert ! res2 .contains (' Min: ' )
62
+ assert ! res2 .contains (' Max: ' )
63
+ assert ! res2 .contains (' Avg: ' )
64
+ }
You can’t perform that action at this time.
0 commit comments