Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions docs/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ zBench provides two ways to register hooks: globally or for a given benchmark.
Global registration adds hooks to each added benchmark.

```zig
pub fn main() !void {
var threaded: std.Io.Threaded = .init_single_threaded;
const io = threaded.io();

pub fn main(init: std.process.Init) !void {
const io = init.io;
const stdout: std.Io.File = .stdout();

var bench = zbench.Benchmark.init(std.heap.page_allocator, .{ .hooks = .{
var bench = zbench.Benchmark.init(init.gpa, .{ .hooks = .{
.before_all = beforeAllHook,
.after_all = afterAllHook,
} });
Expand All @@ -50,13 +48,11 @@ In this example, both Benchmark 1 and Benchmark 2 will execute `beforeAllHook` a
Hooks can also be included with the `add` and `addParam` methods.

```zig
pub fn main() !void {
var threaded: std.Io.Threaded = .init_single_threaded;
const io = threaded.io();

pub fn main(init: std.process.Init) !void {
const io = init.io;
const stdout: std.Io.File = .stdout();

var bench = zbench.Benchmark.init(std.heap.page_allocator, .{});
var bench = zbench.Benchmark.init(init.gpa, .{});
defer bench.deinit();

try bench.add("Benchmark 1", myBenchmark, .{
Expand Down
22 changes: 12 additions & 10 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,25 @@ Declare zbench as a dependency and add it to your `build.zig.zon`. Replace `<COM
Import zbench in your Zig code and create a benchmark function:

```zig
const std = @import("std");
const zbench = @import("zbench");

fn benchmarkMyFunction(_: *zbench.Benchmark) void {
fn benchmarkMyFunction(_: std.mem.Allocator) void {
// Benchmark code here
}
```

Run the benchmark in a test:
Run the benchmark from main:

```zig
pub fn main() !void {
const resultsAlloc = std.ArrayList(zbench.BenchmarkResult).init(test_allocator);
var bench = try zbench.Benchmark.init("My Benchmark", std.heap.page_allocator);
var benchmarkResults = zbench.BenchmarkResults{
.results = resultsAlloc,
};
defer benchmarkResults.results.deinit();
try zbench.run(myBenchmark, &bench, &benchmarkResults);
pub fn main(init: std.process.Init) !void {
const io = init.io;
const stdout: std.Io.File = .stdout();

var bench = zbench.Benchmark.init(init.gpa, .{});
defer bench.deinit();

try bench.add("My Benchmark", benchmarkMyFunction, .{});
try bench.run(io, stdout);
}
```
7 changes: 3 additions & 4 deletions examples/basic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ fn myBenchmark(allocator: std.mem.Allocator) void {
}
}

pub fn main() !void {
var threaded: std.Io.Threaded = .init_single_threaded;
const io = threaded.io();
pub fn main(init: std.process.Init) !void {
const io = init.io;
const stdout: std.Io.File = .stdout();

var bench = zbench.Benchmark.init(std.heap.page_allocator, .{});
var bench = zbench.Benchmark.init(init.gpa, .{});
defer bench.deinit();

try bench.add("My Benchmark", myBenchmark, .{});
Expand Down
7 changes: 3 additions & 4 deletions examples/bubble_sort.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ fn myBenchmark(_: std.mem.Allocator) void {
_ = bubbleSort(&numbers);
}

pub fn main() !void {
var threaded: std.Io.Threaded = .init_single_threaded;
const io = threaded.io();
pub fn main(init: std.process.Init) !void {
const io = init.io;
const stdout: std.Io.File = .stdout();

var bench = zbench.Benchmark.init(std.heap.page_allocator, .{});
var bench = zbench.Benchmark.init(init.gpa, .{});
defer bench.deinit();

try bench.add("Bubble Sort Benchmark", myBenchmark, .{});
Expand Down
20 changes: 8 additions & 12 deletions examples/bubble_sort_hooks.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ const inc = @import("include");
const zbench = @import("zbench");

// Global variables modified/accessed by the hooks.
var gpa = std.heap.DebugAllocator(.{}){};
var benchmark_allocator: std.mem.Allocator = undefined;
const array_size: usize = 100;
// BenchmarkData contains the data generation logic.
var benchmark_data: BenchmarkData = undefined;

// Hooks do not accept any parameters and cannot return anything.
fn beforeAll() void {
benchmark_data.init(gpa.allocator(), array_size) catch unreachable;
benchmark_data.init(benchmark_allocator, array_size) catch unreachable;
}

fn beforeEach() void {
Expand Down Expand Up @@ -45,7 +45,7 @@ fn afterEach() void {
}

fn afterAll() void {
benchmark_data.deinit(gpa.allocator());
benchmark_data.deinit(benchmark_allocator);
}

const BenchmarkData = struct {
Expand Down Expand Up @@ -74,17 +74,13 @@ const BenchmarkData = struct {
}
};

pub fn main() !void {
var threaded: std.Io.Threaded = .init_single_threaded;
const io = threaded.io();
pub fn main(init: std.process.Init) !void {
const io = init.io;
const stdout: std.Io.File = .stdout();
benchmark_allocator = init.gpa;

var bench = zbench.Benchmark.init(gpa.allocator(), .{});
defer {
bench.deinit();
const deinit_status = gpa.deinit();
if (deinit_status == .leak) std.debug.panic("Memory leak detected", .{});
}
var bench = zbench.Benchmark.init(init.gpa, .{});
defer bench.deinit();

try bench.add("Bubble Sort Benchmark", myBenchmark, .{
.track_allocations = true, // Option used to show that hooks are not included in the tracking.
Expand Down
7 changes: 3 additions & 4 deletions examples/hooks.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ fn myBenchmark(_: std.mem.Allocator) void {
}
}

pub fn main() !void {
var threaded: std.Io.Threaded = .init_single_threaded;
const io = threaded.io();
pub fn main(init: std.process.Init) !void {
const io = init.io;
const stdout: std.Io.File = .stdout();

var bench = zbench.Benchmark.init(std.heap.page_allocator, .{});
var bench = zbench.Benchmark.init(init.gpa, .{});
defer bench.deinit();

try bench.add("My Benchmark", myBenchmark, .{
Expand Down
14 changes: 4 additions & 10 deletions examples/json.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const std = @import("std");
const zbench = @import("zbench");
var gpa = std.heap.DebugAllocator(.{}){};

fn myBenchmark(alloc: std.mem.Allocator) void {
var result: usize = 0;
Expand All @@ -12,19 +11,14 @@ fn myBenchmark(alloc: std.mem.Allocator) void {
}
}

pub fn main() !void {
var threaded: std.Io.Threaded = .init_single_threaded;
const io = threaded.io();
pub fn main(init: std.process.Init) !void {
const io = init.io;
const stdout: std.Io.File = .stdout();
var filewriter: std.Io.File.Writer = stdout.writerStreaming(io, &.{});
const writer: *std.Io.Writer = &filewriter.interface;

var bench = zbench.Benchmark.init(gpa.allocator(), .{});
defer {
bench.deinit();
const deinit_status = gpa.deinit();
if (deinit_status == .leak) std.debug.panic("Memory leak detected", .{});
}
var bench = zbench.Benchmark.init(init.gpa, .{});
defer bench.deinit();

try bench.add("My Benchmark 1", myBenchmark, .{
.iterations = 10,
Expand Down
15 changes: 4 additions & 11 deletions examples/memory_comparison.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ const zbench = @import("zbench");
const builtin = @import("builtin");
const c = std.c;

var gpa = std.heap.DebugAllocator(.{}){};

const OldSystemInfoBenchmark = struct {
io: std.Io,

Expand Down Expand Up @@ -188,21 +186,16 @@ fn benchmarkStackUsageNew(allocator: std.mem.Allocator) void {
std.mem.doNotOptimizeAway(&scratch);
}

pub fn main() !void {
var threaded: std.Io.Threaded = .init_single_threaded;
const io = threaded.io();
pub fn main(init: std.process.Init) !void {
const io = init.io;
const stdout: std.Io.File = .stdout();
var filewriter: std.Io.File.Writer = stdout.writerStreaming(io, &.{});
const writer: *std.Io.Writer = &filewriter.interface;

var bench = zbench.Benchmark.init(gpa.allocator(), .{
var bench = zbench.Benchmark.init(init.gpa, .{
.iterations = 100,
});
defer {
bench.deinit();
const deinit_status = gpa.deinit();
if (deinit_status == .leak) std.debug.panic("Memory leak detected", .{});
}
defer bench.deinit();

try writer.writeAll("Memory Usage Comparison: System Info Retrieval\n");
try writer.writeAll("==============================================\n\n");
Expand Down
14 changes: 4 additions & 10 deletions examples/memory_tracking.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const std = @import("std");
const zbench = @import("zbench");
var gpa = std.heap.DebugAllocator(.{}){};

// amount of memory that should appear in the [MEMORY] section of the output
const NUM_BYTES = 1024;
Expand All @@ -12,19 +11,14 @@ fn myBenchmark(allocator: std.mem.Allocator) void {
}
}

pub fn main() !void {
var threaded: std.Io.Threaded = .init_single_threaded;
const io = threaded.io();
pub fn main(init: std.process.Init) !void {
const io = init.io;
const stdout: std.Io.File = .stdout();

var bench = zbench.Benchmark.init(gpa.allocator(), .{
var bench = zbench.Benchmark.init(init.gpa, .{
.iterations = 64,
});
defer {
bench.deinit();
const deinit_status = gpa.deinit();
if (deinit_status == .leak) std.debug.panic("Memory leak detected", .{});
}
defer bench.deinit();

try bench.add("My Benchmark 1", myBenchmark, .{});
try bench.add("My Benchmark 2", myBenchmark, .{ .track_allocations = true });
Expand Down
7 changes: 3 additions & 4 deletions examples/parameterised.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ const MyBenchmark = struct {
}
};

pub fn main() !void {
var threaded: std.Io.Threaded = .init_single_threaded;
const io = threaded.io();
pub fn main(init: std.process.Init) !void {
const io = init.io;
const stdout: std.Io.File = .stdout();

var bench = zbench.Benchmark.init(std.heap.page_allocator, .{});
var bench = zbench.Benchmark.init(init.gpa, .{});
defer bench.deinit();

try bench.addParam("My Benchmark 1", &MyBenchmark.init(100_000), .{});
Expand Down
10 changes: 3 additions & 7 deletions examples/progress.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@ fn myBenchmark2(_: std.mem.Allocator) void {
}
}

pub fn main() !void {
// std.Progress spawns a redraw worker via `io.concurrent`, so this example
// needs a threaded I/O runtime instead of `init_single_threaded`.
var threaded = std.Io.Threaded.init(std.heap.page_allocator, .{});
defer threaded.deinit();
const io = threaded.io();
pub fn main(init: std.process.Init) !void {
const io = init.io;
const stdout: std.Io.File = .stdout();

var bench = zbench.Benchmark.init(std.heap.page_allocator, .{});
var bench = zbench.Benchmark.init(init.gpa, .{});
defer bench.deinit();

try bench.add("My Benchmark 1", myBenchmark1, .{});
Expand Down
15 changes: 4 additions & 11 deletions examples/shuffling_allocator.zig
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
const std = @import("std");
const zbench = @import("zbench");

var gpa = std.heap.DebugAllocator(.{}){};

fn myBenchmark(allocator: std.mem.Allocator) void {
for (0..2000) |_| {
const buf = allocator.alloc(u8, 512) catch @panic("OOM");
defer allocator.free(buf);
}
}

pub fn main() !void {
var threaded: std.Io.Threaded = .init_single_threaded;
const io = threaded.io();
pub fn main(init: std.process.Init) !void {
const io = init.io;
const stdout: std.Io.File = .stdout();

var bench = zbench.Benchmark.init(gpa.allocator(), .{ .iterations = 64 });
defer {
bench.deinit();
const deinit_status = gpa.deinit();
if (deinit_status == .leak) std.debug.panic("Memory leak detected", .{});
}
var bench = zbench.Benchmark.init(init.gpa, .{ .iterations = 64 });
defer bench.deinit();

try bench.add("My Benchmark 1", myBenchmark, .{});
try bench.add("My Benchmark 2 (tracking)", myBenchmark, .{
Expand Down
24 changes: 13 additions & 11 deletions examples/sleep.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ const std = @import("std");
const zbench = @import("zbench");
const log = std.log.scoped(.zbench_example_sleep);

// using a global io here so we can use it in the benchmarked function implicitly
var threaded: std.Io.Threaded = .init_single_threaded;
const io = threaded.io();
const SleepBenchmark = struct {
io: std.Io,

fn sleepBenchmark(_: std.mem.Allocator) void {
io.sleep(.fromMilliseconds(100), .awake) catch |err| {
log.err("sleep failed: {}", .{err});
};
}
pub fn run(self: *SleepBenchmark, _: std.mem.Allocator) void {
self.io.sleep(.fromMilliseconds(100), .awake) catch |err| {
log.err("sleep failed: {}", .{err});
};
}
};

pub fn main() !void {
pub fn main(init: std.process.Init) !void {
const io = init.io;
const stdout: std.Io.File = .stdout();
const sleep_benchmark = SleepBenchmark{ .io = io };

var bench = zbench.Benchmark.init(std.heap.page_allocator, .{});
var bench = zbench.Benchmark.init(init.gpa, .{});
defer bench.deinit();

try bench.add("Sleep Benchmark", sleepBenchmark, .{});
try bench.addParam("Sleep Benchmark", &sleep_benchmark, .{});
try bench.run(io, stdout);
}
10 changes: 5 additions & 5 deletions examples/systeminfo.zig
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const std = @import("std");
const zbench = @import("zbench");

pub fn main() !void {
var threaded: std.Io.Threaded = .init_single_threaded;
const io = threaded.io();
var stdout: std.Io.File.Writer = std.Io.File.stdout().writerStreaming(io, &.{});
const writer = &stdout.interface;
pub fn main(init: std.process.Init) !void {
const io = init.io;
const stdout: std.Io.File = .stdout();
var filewriter: std.Io.File.Writer = stdout.writerStreaming(io, &.{});
const writer: *std.Io.Writer = &filewriter.interface;

try writer.print("\n\n{f}\n", .{try zbench.getSystemInfo()});
}
Loading
Loading