2019-09-16 17:29:06 +03:00
|
|
|
module benchmark
|
|
|
|
|
|
|
|
import time
|
2019-10-07 08:51:26 +03:00
|
|
|
import term
|
2019-09-16 17:29:06 +03: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 12:47:21 +03: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 17:29:06 +03: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') )
|
|
|
|
```
|
|
|
|
*/
|
|
|
|
|
2019-12-20 00:29:37 +03:00
|
|
|
|
|
|
|
pub struct Benchmark {
|
2019-09-16 17:29:06 +03:00
|
|
|
pub mut:
|
|
|
|
bench_start_time i64
|
2019-12-20 00:29:37 +03:00
|
|
|
bench_end_time i64
|
|
|
|
step_start_time i64
|
|
|
|
step_end_time i64
|
|
|
|
ntotal int
|
|
|
|
nok int
|
|
|
|
nfail int
|
|
|
|
verbose bool
|
2019-09-16 17:29:06 +03:00
|
|
|
}
|
|
|
|
|
2019-12-20 00:29:37 +03:00
|
|
|
pub fn new_benchmark() Benchmark {
|
2019-09-16 17:29:06 +03:00
|
|
|
return Benchmark{
|
|
|
|
bench_start_time: benchmark.now()
|
|
|
|
verbose: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (b mut Benchmark) stop() {
|
|
|
|
b.bench_end_time = benchmark.now()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (b mut Benchmark) step() {
|
|
|
|
b.step_start_time = benchmark.now()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (b mut Benchmark) fail() {
|
|
|
|
b.step_end_time = benchmark.now()
|
|
|
|
b.ntotal++
|
|
|
|
b.nfail++
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (b mut Benchmark) ok() {
|
|
|
|
b.step_end_time = benchmark.now()
|
|
|
|
b.ntotal++
|
|
|
|
b.nok++
|
|
|
|
}
|
|
|
|
|
2019-10-07 08:51:26 +03:00
|
|
|
pub fn (b mut Benchmark) fail_many(n int) {
|
|
|
|
b.step_end_time = benchmark.now()
|
2019-12-20 00:29:37 +03:00
|
|
|
b.ntotal += n
|
|
|
|
b.nfail += n
|
2019-10-07 08:51:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (b mut Benchmark) ok_many(n int) {
|
|
|
|
b.step_end_time = benchmark.now()
|
2019-12-20 00:29:37 +03:00
|
|
|
b.ntotal += n
|
|
|
|
b.nok += n
|
2019-10-07 08:51:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (b mut Benchmark) neither_fail_nor_ok() {
|
|
|
|
b.step_end_time = benchmark.now()
|
|
|
|
}
|
|
|
|
|
2019-12-06 15:24:53 +03:00
|
|
|
pub fn (b &Benchmark) step_message(msg string) string {
|
2019-09-16 17:29:06 +03:00
|
|
|
return b.tdiff_in_ms(msg, b.step_start_time, b.step_end_time)
|
|
|
|
}
|
|
|
|
|
2019-12-06 15:24:53 +03:00
|
|
|
pub fn (b &Benchmark) total_message(msg string) string {
|
2019-12-20 00:29:37 +03:00
|
|
|
mut tmsg := '$msg \n ok, fail, total = ' + term.ok_message('${b.nok:5d}') + ', ' + if b.nfail > 0 { term.fail_message('${b.nfail:5d}') } else { '${b.nfail:5d}' } + ', ' + '${b.ntotal:5d}'
|
2019-09-16 17:29:06 +03:00
|
|
|
if b.verbose {
|
|
|
|
tmsg = '<=== total time spent $tmsg'
|
|
|
|
}
|
|
|
|
return b.tdiff_in_ms(tmsg, b.bench_start_time, b.bench_end_time)
|
|
|
|
}
|
|
|
|
|
2019-12-06 15:24:53 +03:00
|
|
|
pub fn (b &Benchmark) total_duration() i64 {
|
2019-10-07 08:51:26 +03:00
|
|
|
return (b.bench_end_time - b.bench_start_time)
|
|
|
|
}
|
2019-09-16 17:29:06 +03:00
|
|
|
|
2019-12-20 00:29:37 +03:00
|
|
|
// //////////////////////////////////////////////////////////////////
|
2019-12-06 15:24:53 +03:00
|
|
|
fn (b &Benchmark) tdiff_in_ms(s string, sticks i64, eticks i64) string {
|
2019-09-16 17:29:06 +03:00
|
|
|
if b.verbose {
|
|
|
|
tdiff := (eticks - sticks)
|
2019-11-06 23:04:40 +03:00
|
|
|
return '${tdiff:6lld} ms | $s'
|
2019-09-16 17:29:06 +03:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2019-09-16 18:27:55 +03:00
|
|
|
|
|
|
|
fn now() i64 {
|
|
|
|
return time.ticks()
|
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
|