Skip to content

Commit

Permalink
Add an example for incremental running
Browse files Browse the repository at this point in the history
It's just a usage example for now, the implementation isn't done.
  • Loading branch information
bens committed Mar 15, 2024
1 parent baee462 commit a62de98
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
17 changes: 14 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
42 changes: 42 additions & 0 deletions examples/progress.zig
Original file line number Diff line number Diff line change
@@ -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;
},
};
}
2 changes: 1 addition & 1 deletion zbench.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a62de98

Please sign in to comment.