2019-09-16 16:29:06 +02:00
|
|
|
module benchmark
|
|
|
|
|
|
|
|
import time
|
2019-10-07 07:51:26 +02:00
|
|
|
import term
|
2019-12-19 22:29:37 +01:00
|
|
|
|
2020-08-07 15:44:49 +02:00
|
|
|
pub const (
|
2020-12-27 14:51:55 +01:00
|
|
|
b_ok = term.ok_message('OK ')
|
|
|
|
b_fail = term.fail_message('FAIL')
|
2020-05-22 17:36:09 +02:00
|
|
|
b_skip = term.warn_message('SKIP')
|
|
|
|
b_spent = term.ok_message('SPENT')
|
2019-12-30 05:23:54 +01:00
|
|
|
)
|
|
|
|
|
2019-12-19 22:29:37 +01:00
|
|
|
pub struct Benchmark {
|
2019-09-16 16:29:06 +02:00
|
|
|
pub mut:
|
2020-12-27 14:51:55 +01:00
|
|
|
bench_timer time.StopWatch
|
|
|
|
verbose bool
|
|
|
|
no_cstep bool
|
|
|
|
step_timer time.StopWatch
|
|
|
|
ntotal int
|
|
|
|
nok int
|
|
|
|
nfail int
|
|
|
|
nskip int
|
|
|
|
nexpected_steps int
|
|
|
|
cstep int
|
|
|
|
bok string
|
|
|
|
bfail string
|
2019-09-16 16:29:06 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// new_benchmark returns a `Benchmark` instance on the stack.
|
2019-12-19 22:29:37 +01:00
|
|
|
pub fn new_benchmark() Benchmark {
|
2019-09-16 16:29:06 +02:00
|
|
|
return Benchmark{
|
2020-05-30 09:20:54 +02:00
|
|
|
bench_timer: time.new_stopwatch({})
|
2019-09-16 16:29:06 +02:00
|
|
|
verbose: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// new_benchmark_no_cstep returns a new `Benchmark` instance with step counting disabled.
|
2020-04-18 04:49:03 +02:00
|
|
|
pub fn new_benchmark_no_cstep() Benchmark {
|
|
|
|
return Benchmark{
|
2020-05-30 09:20:54 +02:00
|
|
|
bench_timer: time.new_stopwatch({})
|
2020-04-18 04:49:03 +02:00
|
|
|
verbose: true
|
|
|
|
no_cstep: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// new_benchmark_pointer returns a new `Benchmark` instance allocated on the heap.
|
|
|
|
// This is useful for long-lived use of `Benchmark` instances.
|
2020-03-04 20:28:42 +01:00
|
|
|
pub fn new_benchmark_pointer() &Benchmark {
|
|
|
|
return &Benchmark{
|
2020-05-30 09:20:54 +02:00
|
|
|
bench_timer: time.new_stopwatch({})
|
2020-03-04 20:28:42 +01:00
|
|
|
verbose: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// set_total_expected_steps sets the the total amount of steps the benchmark is expected to take.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut b Benchmark) set_total_expected_steps(n int) {
|
2019-12-30 05:23:54 +01:00
|
|
|
b.nexpected_steps = n
|
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// stop stops the internal benchmark timer.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut b Benchmark) stop() {
|
2020-04-19 03:40:32 +02:00
|
|
|
b.bench_timer.stop()
|
2019-09-16 16:29:06 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// step increases the step count by 1 and restarts the internal timer.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut b Benchmark) step() {
|
2020-04-19 03:40:32 +02:00
|
|
|
b.step_timer.restart()
|
2020-04-18 04:49:03 +02:00
|
|
|
if !b.no_cstep {
|
|
|
|
b.cstep++
|
|
|
|
}
|
2019-09-16 16:29:06 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// fail increases the fail count by 1 and stops the internal timer.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut b Benchmark) fail() {
|
2020-04-19 03:40:32 +02:00
|
|
|
b.step_timer.stop()
|
2019-09-16 16:29:06 +02:00
|
|
|
b.ntotal++
|
|
|
|
b.nfail++
|
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// ok increases the ok count by 1 and stops the internal timer.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut b Benchmark) ok() {
|
2020-04-19 03:40:32 +02:00
|
|
|
b.step_timer.stop()
|
2019-09-16 16:29:06 +02:00
|
|
|
b.ntotal++
|
|
|
|
b.nok++
|
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// skip increases the skip count by 1 and stops the internal timer.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut b Benchmark) skip() {
|
2020-04-19 03:40:32 +02:00
|
|
|
b.step_timer.stop()
|
2020-03-21 09:48:02 +01:00
|
|
|
b.ntotal++
|
|
|
|
b.nskip++
|
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// fail_many increases the fail count by `n` and stops the internal timer.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut b Benchmark) fail_many(n int) {
|
2020-04-19 03:40:32 +02:00
|
|
|
b.step_timer.stop()
|
2019-12-19 22:29:37 +01:00
|
|
|
b.ntotal += n
|
|
|
|
b.nfail += n
|
2019-10-07 07:51:26 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// ok_many increases the ok count by `n` and stops the internal timer.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut b Benchmark) ok_many(n int) {
|
2020-04-19 03:40:32 +02:00
|
|
|
b.step_timer.stop()
|
2019-12-19 22:29:37 +01:00
|
|
|
b.ntotal += n
|
|
|
|
b.nok += n
|
2019-10-07 07:51:26 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// neither_fail_nor_ok stops the internal timer.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut b Benchmark) neither_fail_nor_ok() {
|
2020-04-19 03:40:32 +02:00
|
|
|
b.step_timer.stop()
|
2019-10-07 07:51:26 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// start returns a new, running, instance of `Benchmark`.
|
|
|
|
// This is a shorthand for calling `new_benchmark().step()`.
|
2020-02-26 16:29:46 +01:00
|
|
|
pub fn start() Benchmark {
|
|
|
|
mut b := new_benchmark()
|
|
|
|
b.step()
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// measure prints the current time spent doing `label`, since the benchmark was started.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut b Benchmark) measure(label string) i64 {
|
2020-02-26 16:29:46 +01:00
|
|
|
b.ok()
|
2020-04-24 09:26:47 +02:00
|
|
|
res := b.step_timer.elapsed().microseconds()
|
2020-05-22 17:36:09 +02:00
|
|
|
println(b.step_message_with_label(b_spent, 'in $label'))
|
2020-02-26 16:29:46 +01:00
|
|
|
b.step()
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// step_message_with_label_and_duration returns a string describing the current step.
|
2020-08-07 15:44:49 +02:00
|
|
|
pub fn (b &Benchmark) step_message_with_label_and_duration(label string, msg string, sduration time.Duration) string {
|
|
|
|
timed_line := b.tdiff_in_ms(msg, sduration.microseconds())
|
2020-04-24 09:26:47 +02:00
|
|
|
if b.nexpected_steps > 1 {
|
2019-12-30 05:23:54 +01:00
|
|
|
mut sprogress := ''
|
|
|
|
if b.nexpected_steps < 10 {
|
2020-12-27 14:51:55 +01:00
|
|
|
sprogress = if b.no_cstep {
|
|
|
|
'TMP1/${b.nexpected_steps:1d}'
|
|
|
|
} else {
|
2020-04-18 04:49:03 +02:00
|
|
|
'${b.cstep:1d}/${b.nexpected_steps:1d}'
|
|
|
|
}
|
2020-04-24 09:26:47 +02:00
|
|
|
} else if b.nexpected_steps >= 10 && b.nexpected_steps < 100 {
|
2020-12-27 14:51:55 +01:00
|
|
|
sprogress = if b.no_cstep {
|
|
|
|
'TMP2/${b.nexpected_steps:2d}'
|
|
|
|
} else {
|
2020-04-18 04:49:03 +02:00
|
|
|
'${b.cstep:2d}/${b.nexpected_steps:2d}'
|
|
|
|
}
|
2020-04-24 09:26:47 +02:00
|
|
|
} else if b.nexpected_steps >= 100 && b.nexpected_steps < 1000 {
|
2020-12-27 14:51:55 +01:00
|
|
|
sprogress = if b.no_cstep {
|
|
|
|
'TMP3/${b.nexpected_steps:3d}'
|
|
|
|
} else {
|
2020-04-18 04:49:03 +02:00
|
|
|
'${b.cstep:3d}/${b.nexpected_steps:3d}'
|
|
|
|
}
|
2020-04-24 09:26:47 +02:00
|
|
|
} else {
|
2020-12-27 14:51:55 +01:00
|
|
|
sprogress = if b.no_cstep {
|
|
|
|
'TMP4/${b.nexpected_steps:4d}'
|
|
|
|
} else {
|
2020-04-24 09:26:47 +02:00
|
|
|
'${b.cstep:4d}/${b.nexpected_steps:4d}'
|
|
|
|
}
|
2019-12-30 05:23:54 +01:00
|
|
|
}
|
2020-12-27 14:51:55 +01:00
|
|
|
return '${label:-5s} [$sprogress] $timed_line'
|
2019-12-30 05:23:54 +01:00
|
|
|
}
|
2020-12-27 14:51:55 +01:00
|
|
|
return '${label:-5s}$timed_line'
|
2019-12-30 05:23:54 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// step_message_with_label returns a string describing the current step using current time as duration.
|
2020-08-07 15:44:49 +02:00
|
|
|
pub fn (b &Benchmark) step_message_with_label(label string, msg string) string {
|
|
|
|
return b.step_message_with_label_and_duration(label, msg, b.step_timer.elapsed())
|
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// step_message returns a string describing the current step.
|
2019-12-06 13:24:53 +01:00
|
|
|
pub fn (b &Benchmark) step_message(msg string) string {
|
2019-12-30 05:23:54 +01:00
|
|
|
return b.step_message_with_label('', msg)
|
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// step_message_ok returns a string describing the current step with an standard "OK" label.
|
2019-12-30 05:23:54 +01:00
|
|
|
pub fn (b &Benchmark) step_message_ok(msg string) string {
|
2020-05-22 17:36:09 +02:00
|
|
|
return b.step_message_with_label(b_ok, msg)
|
2019-12-30 05:23:54 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// step_message_fail returns a string describing the current step with an standard "FAIL" label.
|
2019-12-30 05:23:54 +01:00
|
|
|
pub fn (b &Benchmark) step_message_fail(msg string) string {
|
2020-05-22 17:36:09 +02:00
|
|
|
return b.step_message_with_label(b_fail, msg)
|
2019-09-16 16:29:06 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// step_message_skip returns a string describing the current step with an standard "SKIP" label.
|
2020-03-21 09:48:02 +01:00
|
|
|
pub fn (b &Benchmark) step_message_skip(msg string) string {
|
2020-05-22 17:36:09 +02:00
|
|
|
return b.step_message_with_label(b_skip, msg)
|
2020-03-21 09:48:02 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// total_message returns a string with total summary of the benchmark run.
|
2019-12-06 13:24:53 +01:00
|
|
|
pub fn (b &Benchmark) total_message(msg string) string {
|
2021-01-17 18:09:29 +01:00
|
|
|
mut tmsg := '${term.colorize(term.bold, 'Summary:')} '
|
2021-01-13 10:07:12 +01:00
|
|
|
if b.nfail > 0 {
|
2021-01-17 18:09:29 +01:00
|
|
|
tmsg += term.colorize(term.bold, term.colorize(term.red, '$b.nfail failed')) + ', '
|
2019-09-16 16:29:06 +02:00
|
|
|
}
|
2021-01-13 10:07:12 +01:00
|
|
|
if b.nok > 0 {
|
2021-01-17 18:09:29 +01:00
|
|
|
tmsg += term.colorize(term.bold, term.colorize(term.green, '$b.nok passed')) + ', '
|
2021-01-13 10:07:12 +01:00
|
|
|
}
|
|
|
|
if b.nskip > 0 {
|
2021-01-17 18:09:29 +01:00
|
|
|
tmsg += term.colorize(term.bold, term.colorize(term.yellow, '$b.nskip skipped')) + ', '
|
2020-04-24 09:26:47 +02:00
|
|
|
}
|
2021-01-23 09:33:22 +01:00
|
|
|
tmsg += '$b.ntotal total. ${term.colorize(term.bold, 'Runtime:')} ${b.bench_timer.elapsed().microseconds() / 1000} ms.\n'
|
2021-01-17 18:09:29 +01:00
|
|
|
tmsg += term.colorize(term.gray, msg)
|
2021-01-13 10:07:12 +01:00
|
|
|
return tmsg
|
2019-09-16 16:29:06 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// total_duration returns the duration in ms.
|
2019-12-06 13:24:53 +01:00
|
|
|
pub fn (b &Benchmark) total_duration() i64 {
|
2020-04-24 07:33:25 +02:00
|
|
|
return b.bench_timer.elapsed().milliseconds()
|
2019-10-07 07:51:26 +02:00
|
|
|
}
|
2019-09-16 16:29:06 +02:00
|
|
|
|
2020-12-18 18:21:20 +01:00
|
|
|
// tdiff_in_ms prefixes `s` with a time difference calculation.
|
2020-04-19 03:40:32 +02:00
|
|
|
fn (b &Benchmark) tdiff_in_ms(s string, tdiff i64) string {
|
2019-09-16 16:29:06 +02:00
|
|
|
if b.verbose {
|
2020-12-27 14:51:55 +01:00
|
|
|
return '${f64(tdiff) / 1000.0:9.3f} ms $s'
|
2019-09-16 16:29:06 +02:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|