Skip to content

x.benchmark #

fn setup #

fn setup(bench_func fn () !, params BenchmarkDefaults) !Benchmark

Benchmark.new - constructor of benchmark arguments:- bench_func - function to benchmark. required, if you have no function - you don't need benchmark

  • params - structure of benchmark parameters

struct Benchmark #

@[noinit]
struct Benchmark {
pub mut:
	n                i64 // Number of iterations. Set explicitly or computed from expected time of benchmarking
	bench_func       fn () ! @[required] // function for benchmarking
	bench_time       time.Duration   // benchmark duration
	is_parallel      bool            // if true every bench_func run in separate coroutine
	benchmark_result BenchmarkResult // accumulator of benchmark metrics
	timer_on         bool            // inner flag of time recording
	start_time       time.Time       // start timestamp of timer
	duration         time.Duration   // expected time of benchmark process
	failed           bool            // flag of bench_func failure. true if one of bench_func run failed
	start_memory     usize           // memory status on start benchmark
	start_allocs     usize           // size of object allocated on heap
}

Benchmark represent all significant data for benchmarking. Provide clear way for getting result in convinient way by exported methods

fn (Benchmark) run #

fn (mut b Benchmark) run()

run_benchmark - function for start benchmarking run benchmark n times, or duration time

struct BenchmarkDefaults #

@[params]
struct BenchmarkDefaults {
pub:
	duration    time.Duration = time.second
	is_parallel bool
	n           i64
}

BenchmarkDefaults is params struct for providing parameters of benchmarking to setup function- n - number of iterations. set if you know how many runs of function you need. if you don't know how many you need - set 0

  • duration - by default 1s. expecting duration of all benchmark runs. doesn't work if is_parallel == true
  • is_parallel - if true, every bench_func run in separate coroutine