benchmark #

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
// if you want it to be silent, set bmark.verbose = false
for {
    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'))
}
bmark.stop()
// call when you want to finalize the benchmark
println(bmark.total_message('remarks about the benchmark'))

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.

Example:

import time
import benchmark

mut b := benchmark.start()
// your code section 1 ...
time.sleep(1500 * time.millisecond)
b.measure('code_1')
// your code section 2 ...
time.sleep(500 * time.millisecond)
b.measure('code_2')

... which will produce on stdout something like this:

SPENT 1500.063 ms in code_1
SPENT  500.061 ms in code_2

Constants #

const (
	b_ok    = term.ok_message('OK  ')
	b_fail  = term.fail_message('FAIL')
	b_skip  = term.warn_message('SKIP')
	b_spent = term.ok_message('SPENT')
)

fn new_benchmark #

fn new_benchmark() Benchmark

new_benchmark returns a Benchmark instance on the stack.

fn new_benchmark_no_cstep #

fn new_benchmark_no_cstep() Benchmark

new_benchmark_no_cstep returns a new Benchmark instance with step counting disabled.

fn new_benchmark_pointer #

fn new_benchmark_pointer() &Benchmark

new_benchmark_pointer returns a new Benchmark instance allocated on the heap.
This is useful for long-lived use of Benchmark instances.

fn start #

fn start() Benchmark

start returns a new, running, instance of Benchmark.
This is a shorthand for calling new_benchmark().step().

struct Benchmark #

struct Benchmark {
pub mut:
	bench_timer     time.StopWatch
	verbose         bool
	no_cstep        bool
	step_timer      time.StopWatch
	ntotal          int
	nok             int
	nfail           int
	nskip           int
	nexpected_steps int
	njobs           int
	cstep           int
	bok             string
	bfail           string
}

fn (Benchmark) set_total_expected_steps #

fn (mut b Benchmark) set_total_expected_steps(n int)

set_total_expected_steps sets the the total amount of steps the benchmark is expected to take.

fn (Benchmark) stop #

fn (mut b Benchmark) stop()

stop stops the internal benchmark timer.

fn (Benchmark) step #

fn (mut b Benchmark) step()

step increases the step count by 1 and restarts the internal timer.

fn (Benchmark) fail #

fn (mut b Benchmark) fail()

fail increases the fail count by 1 and stops the internal timer.

fn (Benchmark) ok #

fn (mut b Benchmark) ok()

ok increases the ok count by 1 and stops the internal timer.

fn (Benchmark) skip #

fn (mut b Benchmark) skip()

skip increases the skip count by 1 and stops the internal timer.

fn (Benchmark) fail_many #

fn (mut b Benchmark) fail_many(n int)

fail_many increases the fail count by n and stops the internal timer.

fn (Benchmark) ok_many #

fn (mut b Benchmark) ok_many(n int)

ok_many increases the ok count by n and stops the internal timer.

fn (Benchmark) neither_fail_nor_ok #

fn (mut b Benchmark) neither_fail_nor_ok()

neither_fail_nor_ok stops the internal timer.

fn (Benchmark) measure #

fn (mut b Benchmark) measure(label string) i64

measure prints the current time spent doing label, since the benchmark was started.

fn (Benchmark) step_message_with_label_and_duration #

fn (b &Benchmark) step_message_with_label_and_duration(label string, msg string, sduration time.Duration) string

step_message_with_label_and_duration returns a string describing the current step.

fn (Benchmark) step_message_with_label #

fn (b &Benchmark) step_message_with_label(label string, msg string) string

step_message_with_label returns a string describing the current step using current time as duration.

fn (Benchmark) step_message #

fn (b &Benchmark) step_message(msg string) string

step_message returns a string describing the current step.

fn (Benchmark) step_message_ok #

fn (b &Benchmark) step_message_ok(msg string) string

step_message_ok returns a string describing the current step with an standard "OK" label.

fn (Benchmark) step_message_fail #

fn (b &Benchmark) step_message_fail(msg string) string

step_message_fail returns a string describing the current step with an standard "FAIL" label.

fn (Benchmark) step_message_skip #

fn (b &Benchmark) step_message_skip(msg string) string

step_message_skip returns a string describing the current step with an standard "SKIP" label.

fn (Benchmark) total_message #

fn (b &Benchmark) total_message(msg string) string

total_message returns a string with total summary of the benchmark run.

fn (Benchmark) total_duration #

fn (b &Benchmark) total_duration() i64

total_duration returns the duration in ms.