2022-01-05 17:06:08 +01:00
|
|
|
## Description:
|
|
|
|
|
2022-01-07 12:28:50 +01:00
|
|
|
`benchmark` provides tools for measuring and reporting on the performance of code.
|
2022-01-05 17:06:08 +01:00
|
|
|
|
2022-01-07 12:28:50 +01:00
|
|
|
## Example 1:
|
2022-01-05 17:06:08 +01:00
|
|
|
|
2020-12-05 22:54:41 +01:00
|
|
|
```v
|
2020-06-07 23:04:23 +02:00
|
|
|
import benchmark
|
2020-12-05 22:54:41 +01:00
|
|
|
|
2020-06-07 23:04:23 +02: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-05 22:54:41 +01: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-07 23:04:23 +02:00
|
|
|
}
|
2020-12-05 22:54:41 +01:00
|
|
|
bmark.stop()
|
|
|
|
// call when you want to finalize the benchmark
|
|
|
|
println(bmark.total_message('remarks about the benchmark'))
|
2020-06-07 23:04:23 +02:00
|
|
|
```
|
|
|
|
|
2022-01-07 12:28:50 +01:00
|
|
|
`.start()` and `.measure()` are convenience methods,
|
2020-06-07 23:04:23 +02:00
|
|
|
intended to be used in combination. Their goal is to make
|
2022-01-07 12:28:50 +01:00
|
|
|
benchmarking of small snippets of code as _short_, easy to
|
|
|
|
write, and easy to read and analyze as possible.
|
|
|
|
|
|
|
|
## Example 2:
|
2020-12-05 22:54:41 +01:00
|
|
|
|
2020-06-07 23:04:23 +02:00
|
|
|
```v
|
2020-07-11 10:49:11 +02:00
|
|
|
import time
|
2020-06-07 23:04:23 +02:00
|
|
|
import benchmark
|
|
|
|
|
2020-12-05 22:54:41 +01:00
|
|
|
mut b := benchmark.start()
|
2020-07-11 10:49:11 +02:00
|
|
|
// your code section 1 ...
|
2021-02-28 17:01:31 +01:00
|
|
|
time.sleep(1500 * time.millisecond)
|
2020-06-07 23:04:23 +02:00
|
|
|
b.measure('code_1')
|
2020-07-11 10:49:11 +02:00
|
|
|
// your code section 2 ...
|
2021-02-28 17:01:31 +01:00
|
|
|
time.sleep(500 * time.millisecond)
|
2020-06-07 23:04:23 +02:00
|
|
|
b.measure('code_2')
|
|
|
|
```
|
2020-12-05 22:54:41 +01:00
|
|
|
|
2020-06-07 23:04:23 +02:00
|
|
|
... which will produce on stdout something like this:
|
2022-01-07 12:28:50 +01:00
|
|
|
|
2020-07-11 10:49:11 +02:00
|
|
|
```text
|
|
|
|
SPENT 1500.063 ms in code_1
|
|
|
|
SPENT 500.061 ms in code_2
|
|
|
|
```
|