2020-06-08 00:04:23 +03:00
|
|
|
Example usage of this module:
|
2020-12-06 00:54:41 +03:00
|
|
|
```v
|
2020-06-08 00:04:23 +03:00
|
|
|
import benchmark
|
2020-12-06 00:54:41 +03:00
|
|
|
|
2020-06-08 00:04:23 +03:00
|
|
|
mut bmark := benchmark.new_benchmark()
|
|
|
|
// by default the benchmark will be verbose, i.e. it will include timing information
|
|
|
|
// if you want it to be silent, set bmark.verbose = false
|
|
|
|
for {
|
2020-12-06 00:54:41 +03:00
|
|
|
bmark.step() // call this when you want to advance the benchmark.
|
|
|
|
// 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'))
|
2020-06-08 00:04:23 +03:00
|
|
|
}
|
2020-12-06 00:54:41 +03:00
|
|
|
bmark.stop()
|
|
|
|
// call when you want to finalize the benchmark
|
|
|
|
println(bmark.total_message('remarks about the benchmark'))
|
2020-06-08 00:04:23 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
benchmark.start() and b.measure() are convenience methods,
|
|
|
|
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.
|
2020-12-06 00:54:41 +03:00
|
|
|
|
2020-06-08 00:04:23 +03:00
|
|
|
Example:
|
|
|
|
```v
|
2020-07-11 11:49:11 +03:00
|
|
|
import time
|
2020-06-08 00:04:23 +03:00
|
|
|
import benchmark
|
|
|
|
|
2020-12-06 00:54:41 +03:00
|
|
|
mut b := benchmark.start()
|
2020-07-11 11:49:11 +03:00
|
|
|
// your code section 1 ...
|
|
|
|
time.sleep_ms(1500)
|
2020-06-08 00:04:23 +03:00
|
|
|
b.measure('code_1')
|
2020-07-11 11:49:11 +03:00
|
|
|
// your code section 2 ...
|
|
|
|
time.sleep_ms(500)
|
2020-06-08 00:04:23 +03:00
|
|
|
b.measure('code_2')
|
|
|
|
```
|
2020-12-06 00:54:41 +03:00
|
|
|
|
2020-06-08 00:04:23 +03:00
|
|
|
... which will produce on stdout something like this:
|
2020-07-11 11:49:11 +03:00
|
|
|
```text
|
|
|
|
SPENT 1500.063 ms in code_1
|
|
|
|
SPENT 500.061 ms in code_2
|
|
|
|
```
|