Lightweight microbenchmarking with warmup, automatic batching, and allocation tracking.
Requires Zig 0.14.1.
zig fetch --save=zlap git+https://github.com/lovez-dev/zlap.git#mainAdd the module to your executable in build.zig:
const zlap = b.dependency("zlap", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("zlap", zlap.module("zlap"));const std = @import("std");
const zlap = @import("zlap");
pub fn main() !void {
const result = try zlap.runConfigured(.{
.batch_size = 100,
.batch_repeats = 10,
.sample_count = 30,
.rounds = 3,
}, allocAndFill);
result.print("alloc + memset + free 4KB");
}
fn allocAndFill(allocator: std.mem.Allocator, operations: usize) !void {
for (0..operations) |_| {
const buffer = try allocator.alloc(u8, 4096);
@memset(buffer, 0xAB);
std.mem.doNotOptimizeAway(buffer);
allocator.free(buffer);
}
}When working from a checkout of zlap, run the included example with:
zig build bench -Doptimize=ReleaseFastBoth zig build bench and zig build run-example build and run the same example; use
bench with an optimized build for measurements.
The workload must complete exactly the requested number of operations and release its resources before returning, including on error. Setup and cleanup inside the workload are timed too. Workloads run synchronously and must not leave background work running.
For computation benchmarks, use runtime inputs and pass results to
std.mem.doNotOptimizeAway. This prevents unused work from being removed, but does not prevent
constant folding or loop hoisting.
All Config fields are optional:
warmup_batches = 5- unmeasured calls before calibration in each round.batch_size = 1000- operations per workload call.batch_repeats = 10- initial calls per sample.sample_count = 30- measured samples per round.min_sample_ns = 10_000_000- calibration duration target; zero disables calibration.rounds = 5- rounds in the same process.allocator = null- defaults tostd.heap.page_allocator.count_allocations = false- enables cumulative requested-byte tracking.
Calibration doubles the calls per sample until the duration target is reached, then keeps that count
for the round. Each sample reads the clock once before and once after the work. Warmup and
calibration are excluded from results. With calibration disabled, each sample uses exactly
batch_repeats calls.
Batch size, repeats, sample count, and rounds must be positive. The total sample count cannot exceed
sample_size (10,000). runRepeated(config, workload, rounds) uses its explicit round count
instead of config.rounds.
Timing uses std.time.Timer and can return error.TimerUnsupported. The duration target is not a
timeout: the runner cannot interrupt a workload. Rounds do not reset workload state or allocators;
manage memory and arena resets yourself.
With count_allocations = true, successful allocation requests and growth through the supplied
allocator are counted. Shrinking and freeing do not subtract bytes. This measures cumulative
requested bytes, not peak memory usage, and adds overhead to timed work.
Tracking does not support concurrent allocator calls through the same wrapper. Release allocations through the supplied wrapper before each workload call returns, including on error, and do not retain that allocator.
The counter is u64 on all targets. Measured overflow returns error.AllocationCountOverflow
without changing successful allocator operations; warmup and calibration counts, including overflow,
are discarded.
Every retained sample is a batch's elapsed time divided by its operation count. Percentiles and ranges describe batch averages, not individual-operation tail latency. Fractional ns/op values do not imply sub-nanosecond clock resolution.
print(name)writes timings and allocation statistics to stderr.samples()returns sorted ns/op values. Samples are stored inline; no separate cleanup is needed. The slice remains valid only while the result stays at the same address.mean()is the unweighted sample mean;overallMean()istotal_ns / operations. They can differ when rounds calibrate to different operation counts.median(),percentile(95),stdDev(), andcoefficientOfVariation()provide the median, nearest-rank percentile, sample standard deviation, and relative variation. Percentiles accept a comptime value from 0 to 100; values outside this range are rejected at compile time.
The fields expose total measured time, operation count, cumulative requested bytes, and
sample count. min_ns and max_ns are the smallest and largest batch-average ns/op values;
allocations_measured distinguishes disabled tracking from zero measured bytes.
zig build test
zig build test -Doptimize=ReleaseFastUse optimized builds for performance measurements. Repeat runs with the same workload and environment, then compare the mean, median, and variation before drawing conclusions from small differences.