diff --git a/build.zig b/build.zig index fba2d7c..acb79e6 100644 --- a/build.zig +++ b/build.zig @@ -57,17 +57,28 @@ fn setupTesting(b: *std.Build, target: std.zig.CrossTarget, optimize: std.builti fn setupExamples(b: *std.Build, target: std.zig.CrossTarget, optimize: std.builtin.OptimizeMode) void { const example_step = b.step("test_examples", "Build examples"); - const example_names = [_][]const u8{ "basic", "bubble_sort", "sleep" }; + const example_names = [_][]const u8{ + "basic", + "bubble_sort", + "progress", + "sleep", + }; for (example_names) |example_name| { const example = b.addTest(.{ .name = example_name, - .root_source_file = .{ .path = b.fmt("examples/{s}.zig", .{example_name}) }, + .root_source_file = .{ + .path = b.fmt("examples/{s}.zig", .{example_name}), + }, .target = target, .optimize = optimize, }); const install_example = b.addInstallArtifact(example, .{}); - const zbench_mod = b.addModule("zbench", .{ .source_file = .{ .path = "zbench.zig" } }); + const zbench_mod = b.addModule("zbench", .{ + .root_source_file = .{ + .path = "zbench.zig", + }, + }); example.addModule("zbench", zbench_mod); example_step.dependOn(&example.step); example_step.dependOn(&install_example.step); diff --git a/examples/progress.zig b/examples/progress.zig new file mode 100644 index 0000000..3b32690 --- /dev/null +++ b/examples/progress.zig @@ -0,0 +1,42 @@ +const std = @import("std"); +const zbench = @import("zbench"); +const test_allocator = std.testing.allocator; + +fn helloWorld(loops: usize) []const u8 { + var result: usize = 0; + var i: usize = 0; + while (i < loops) : (i += 1) { + const square = i * i; + result += square; + } + + return "Hello, world!"; +} + +fn myBenchmark1(_: std.mem.Allocator) void { + _ = helloWorld(1000); +} + +fn myBenchmark2(_: std.mem.Allocator) void { + _ = helloWorld(2000); +} + +test "bench test progress" { + const stdout = std.io.getStdOut().writer(); + var bench = zbench.Benchmark.init(test_allocator, .{}); + defer bench.deinit(); + + try bench.add("My Benchmark 1", myBenchmark1, .{ .iterations = 10 }); + try bench.add("My Benchmark 2", myBenchmark2, .{ .iterations = 10 }); + + try bench.prettyPrintHeader(stdout); + var iter = try bench.iterator(); + while (true) switch (iter.next()) { + .progress => |_| {}, + .result => |x| try x.prettyPrint(stdout, true), + .finished => |x| { + x.deinit(); + break; + }, + }; +} diff --git a/zbench.zig b/zbench.zig index 91c5372..55b9c01 100644 --- a/zbench.zig +++ b/zbench.zig @@ -80,7 +80,7 @@ pub const Benchmark = struct { pub const Iterator = struct { allocator: std.mem.Allocator, benchmarks: []const Definition, - runner: ?*anyopaque, + runner: ?runner.Runner, results: []Result, pub const Progress = struct {