2019-09-16 16:29:06 +02:00
|
|
|
module benchmark
|
|
|
|
|
|
|
|
import time
|
2019-10-07 07:51:26 +02:00
|
|
|
import term
|
2019-09-16 16:29:06 +02:00
|
|
|
/*
|
|
|
|
Example usage of this module:
|
|
|
|
```
|
|
|
|
import benchmark
|
|
|
|
mut bmark := benchmark.new_benchmark()
|
|
|
|
// by default the benchmark will be verbose, i.e. it will include timing information
|
2019-10-24 11:47:21 +02:00
|
|
|
// if you want it to be silent, set bmark.verbose = false
|
|
|
|
for {
|
|
|
|
bmark.step() // call this when you want to advance the benchmark.
|
2019-09-16 16:29:06 +02:00
|
|
|
// The timing info in bmark.step_message will be measured starting from the last call to bmark.step
|
|
|
|
....
|
|
|
|
|
|
|
|
//bmark.fail() // call this if the step failed
|
|
|
|
//bmark.step_message(('failed')
|
|
|
|
|
|
|
|
bmark.ok() // call this when the step succeeded
|
|
|
|
println( bmark.step_message('ok')
|
|
|
|
}
|
|
|
|
bmark.stop() // call when you want to finalize the benchmark
|
|
|
|
println( bmark.total_message('remarks about the benchmark') )
|
|
|
|
```
|
2020-02-26 16:29:46 +01:00
|
|
|
|
2020-02-28 14:05:20 +01:00
|
|
|
benchmark.start() and b.measure() are convenience methods,
|
2020-02-26 16:29:46 +01:00
|
|
|
intended to be used in combination. Their goal is to make
|
|
|
|
benchmarking of small snippets of code as *short*, easy to
|
|
|
|
write, and then to read and analyze the results, as possible.
|
|
|
|
Example:
|
|
|
|
```v
|
|
|
|
import benchmark
|
|
|
|
b := benchmark.start()
|
|
|
|
|
|
|
|
// your code 1 ...
|
|
|
|
b.measure('code_1')
|
|
|
|
|
|
|
|
// your code 2 ...
|
|
|
|
b.measure('code_2')
|
|
|
|
```
|
|
|
|
... which will produce on stdout something like this:
|
|
|
|
SPENT 17 ms in code_1
|
|
|
|
SPENT 462 ms in code_2
|
2019-09-16 16:29:06 +02:00
|
|
|
*/
|
|
|
|
|
2019-12-19 22:29:37 +01:00
|
|
|
|
2019-12-30 05:23:54 +01:00
|
|
|
const (
|
2020-03-21 09:48:02 +01:00
|
|
|
BOK = term.ok_message('OK ')
|
2019-12-30 05:23:54 +01:00
|
|
|
BFAIL = term.fail_message('FAIL')
|
2020-04-08 16:52:40 +02:00
|
|
|
BSKIP = term.warn_message('SKIP')
|
2020-02-26 16:29:46 +01:00
|
|
|
BSPENT = 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-04-24 07:33:25 +02:00
|
|
|
bench_timer time.StopWatch
|
|
|
|
step_timer time.StopWatch
|
2019-12-19 22:29:37 +01:00
|
|
|
ntotal int
|
|
|
|
nok int
|
|
|
|
nfail int
|
2020-03-21 09:48:02 +01:00
|
|
|
nskip int
|
2019-12-19 22:29:37 +01:00
|
|
|
verbose bool
|
2019-12-30 05:23:54 +01:00
|
|
|
nexpected_steps int
|
|
|
|
cstep int
|
2020-04-18 04:49:03 +02:00
|
|
|
no_cstep bool
|
2019-12-30 05:23:54 +01:00
|
|
|
bok string
|
|
|
|
bfail string
|
2019-09-16 16:29:06 +02:00
|
|
|
}
|
|
|
|
|
2019-12-19 22:29:37 +01:00
|
|
|
pub fn new_benchmark() Benchmark {
|
2019-09-16 16:29:06 +02:00
|
|
|
return Benchmark{
|
2020-04-24 07:33:25 +02:00
|
|
|
bench_timer: time.new_stopwatch()
|
2019-09-16 16:29:06 +02:00
|
|
|
verbose: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-18 04:49:03 +02:00
|
|
|
pub fn new_benchmark_no_cstep() Benchmark {
|
|
|
|
return Benchmark{
|
2020-04-24 07:33:25 +02:00
|
|
|
bench_timer: time.new_stopwatch()
|
2020-04-18 04:49:03 +02:00
|
|
|
verbose: true
|
|
|
|
no_cstep: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 20:28:42 +01:00
|
|
|
pub fn new_benchmark_pointer() &Benchmark {
|
|
|
|
return &Benchmark{
|
2020-04-24 07:33:25 +02:00
|
|
|
bench_timer: time.new_stopwatch()
|
2020-03-04 20:28:42 +01:00
|
|
|
verbose: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 05:23:54 +01:00
|
|
|
pub fn (b mut Benchmark) set_total_expected_steps(n int) {
|
|
|
|
b.nexpected_steps = n
|
|
|
|
}
|
|
|
|
|
2019-09-16 16:29:06 +02:00
|
|
|
pub fn (b mut Benchmark) stop() {
|
2020-04-19 03:40:32 +02:00
|
|
|
b.bench_timer.stop()
|
2019-09-16 16:29:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (b mut 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
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (b mut 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++
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (b mut 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-03-21 09:48:02 +01:00
|
|
|
pub fn (b mut 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++
|
|
|
|
}
|
|
|
|
|
2019-10-07 07:51:26 +02:00
|
|
|
pub fn (b mut 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
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (b mut 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
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (b mut 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-02-26 16:29:46 +01:00
|
|
|
pub fn start() Benchmark {
|
|
|
|
mut b := new_benchmark()
|
|
|
|
b.step()
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (b mut Benchmark) measure(label string) i64 {
|
|
|
|
b.ok()
|
2020-04-24 09:26:47 +02:00
|
|
|
res := b.step_timer.elapsed().microseconds()
|
2020-02-26 16:29:46 +01:00
|
|
|
println(b.step_message_with_label(BSPENT, 'in $label'))
|
|
|
|
b.step()
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-12-30 05:23:54 +01:00
|
|
|
pub fn (b &Benchmark) step_message_with_label(label string, msg string) string {
|
2020-04-24 09:26:47 +02:00
|
|
|
timed_line := b.tdiff_in_ms(msg, b.step_timer.elapsed().microseconds())
|
|
|
|
if b.nexpected_steps > 1 {
|
2019-12-30 05:23:54 +01:00
|
|
|
mut sprogress := ''
|
|
|
|
if b.nexpected_steps < 10 {
|
2020-04-18 04:49:03 +02:00
|
|
|
sprogress = if b.no_cstep { 'TMP1/${b.nexpected_steps:1d}' } else {
|
|
|
|
'${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-04-18 04:49:03 +02:00
|
|
|
sprogress = if b.no_cstep { 'TMP2/${b.nexpected_steps:2d}' } else {
|
|
|
|
'${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-04-18 04:49:03 +02:00
|
|
|
sprogress = if b.no_cstep { 'TMP3/${b.nexpected_steps:3d}' } else {
|
|
|
|
'${b.cstep:3d}/${b.nexpected_steps:3d}'
|
|
|
|
}
|
2020-04-24 09:26:47 +02:00
|
|
|
} else {
|
|
|
|
sprogress = if b.no_cstep { 'TMP4/${b.nexpected_steps:4d}' } else {
|
|
|
|
'${b.cstep:4d}/${b.nexpected_steps:4d}'
|
|
|
|
}
|
2019-12-30 05:23:54 +01:00
|
|
|
}
|
2020-04-24 09:26:47 +02:00
|
|
|
return '${label:-5s} [${sprogress}] ${timed_line}'
|
2019-12-30 05:23:54 +01:00
|
|
|
}
|
|
|
|
return '${label:-5s}${timed_line}'
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (b &Benchmark) step_message_ok(msg string) string {
|
|
|
|
return b.step_message_with_label(BOK, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (b &Benchmark) step_message_fail(msg string) string {
|
|
|
|
return b.step_message_with_label(BFAIL, msg)
|
2019-09-16 16:29:06 +02:00
|
|
|
}
|
|
|
|
|
2020-03-21 09:48:02 +01:00
|
|
|
pub fn (b &Benchmark) step_message_skip(msg string) string {
|
|
|
|
return b.step_message_with_label(BSKIP, msg)
|
|
|
|
}
|
|
|
|
|
2019-12-06 13:24:53 +01:00
|
|
|
pub fn (b &Benchmark) total_message(msg string) string {
|
2020-04-08 17:49:00 +02:00
|
|
|
mut tmsg := '${msg}\n ok, fail, skip, total = ' + term.ok_message('${b.nok:5d}') + ', ' + if b.nfail > 0 { term.red('${b.nfail:5d}') } else { '${b.nfail:5d}' } + ', ' + if b.nskip > 0 { term.bright_yellow('${b.nskip:5d}') } else { '${b.nskip:5d}' } + ', ' + '${b.ntotal:5d}'
|
2019-09-16 16:29:06 +02:00
|
|
|
if b.verbose {
|
|
|
|
tmsg = '<=== total time spent $tmsg'
|
|
|
|
}
|
2020-04-24 09:26:47 +02:00
|
|
|
mut spaces := ' '
|
|
|
|
if b.nexpected_steps > 1 {
|
|
|
|
// NB: the formula below accounts for the progress bar [step/total]
|
|
|
|
str_steps := '$b.nexpected_steps'
|
|
|
|
x := 4 + str_steps.len * 2 + 5
|
|
|
|
spaces = ' '.repeat(x)
|
|
|
|
}
|
|
|
|
return spaces + b.tdiff_in_ms(tmsg, b.bench_timer.elapsed().microseconds())
|
2019-09-16 16:29:06 +02:00
|
|
|
}
|
|
|
|
|
2020-04-24 09:26:47 +02: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
|
|
|
|
2019-12-19 22:29:37 +01:00
|
|
|
// //////////////////////////////////////////////////////////////////
|
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-04-24 09:26:47 +02:00
|
|
|
return '${tdiff/1000.0:9.3f} ms $s'
|
2019-09-16 16:29:06 +02:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|