Skip to content

Commit

Permalink
Fix the same problem in the std.Progress branch
Browse files Browse the repository at this point in the history
Also added a test example for memory tracking to compare overhead.
  • Loading branch information
bens committed Mar 22, 2024
1 parent b3b2b91 commit 5a5adb2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fn setupExamples(b: *std.Build, target: std.zig.CrossTarget, optimize: std.built
"bubble_sort",
"hooks",
"json",
"memory_tracking",
"parameterised",
"progress",
"sleep",
Expand Down
25 changes: 25 additions & 0 deletions examples/memory_tracking.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const std = @import("std");
const zbench = @import("zbench");

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

test "bench test basic" {
const stdout = std.io.getStdOut().writer();
var bench = zbench.Benchmark.init(std.testing.allocator, .{
.iterations = 64,
});
defer bench.deinit();

try bench.add("My Benchmark 1", myBenchmark, .{});
try bench.add("My Benchmark 2", myBenchmark, .{
.track_allocations = true,
});

try stdout.writeAll("\n");
try bench.run(stdout);
}
4 changes: 2 additions & 2 deletions zbench.zig
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ pub const Benchmark = struct {

/// Run all benchmarks using an iterator, collecting progress information
/// incrementally.
pub fn iterator(self: Benchmark) !Iterator {
pub fn iterator(self: *const Benchmark) !Iterator {
return Iterator{
.allocator = self.allocator,
.b = &self,
.b = self,
.remaining = self.benchmarks.items,
.runner = null,
};
Expand Down

0 comments on commit 5a5adb2

Please sign in to comment.