-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an incremental API for running benchmarks
Allows to get results as they become available and show a progress bar type display, and allows to limit memory use by freeing results when they're received. Changed run() to return `!void` and got rid of the `Results` struct, it's not really needed. If people want the collection of `Result` values they can easily get them with `iterator()`. Split hooks and system info into their own sample programs
- Loading branch information
Showing
10 changed files
with
262 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,19 @@ | ||
const std = @import("std"); | ||
const zbench = @import("zbench"); | ||
const test_allocator = std.testing.allocator; | ||
|
||
fn helloWorld() []const u8 { | ||
fn myBenchmark(_: std.mem.Allocator) void { | ||
var result: usize = 0; | ||
var i: usize = 0; | ||
while (i < 1000) : (i += 1) { | ||
const square = i * i; | ||
result += square; | ||
for (0..1_000_000) |i| { | ||
std.mem.doNotOptimizeAway(i); | ||
result += i * i; | ||
} | ||
|
||
return "Hello, world!"; | ||
} | ||
|
||
fn beforeAllHook() void { | ||
std.debug.print("Starting benchmarking...\n", .{}); | ||
} | ||
|
||
fn afterAllHook() void { | ||
std.debug.print("Finished benchmarking.\n", .{}); | ||
} | ||
|
||
fn myBenchmark(_: std.mem.Allocator) void { | ||
_ = helloWorld(); | ||
} | ||
|
||
test "bench test basic" { | ||
const stdout = std.io.getStdOut().writer(); | ||
var bench = zbench.Benchmark.init(test_allocator, .{}); | ||
var bench = zbench.Benchmark.init(std.testing.allocator, .{}); | ||
defer bench.deinit(); | ||
|
||
try bench.add("My Benchmark", myBenchmark, .{ | ||
.iterations = 10, | ||
.hooks = .{ | ||
.before_all = beforeAllHook, | ||
.after_all = afterAllHook, | ||
}, | ||
}); | ||
|
||
const sysinfo = try bench.getSystemInfo(); | ||
try std.fmt.format(stdout, "\n{}\n", .{sysinfo}); | ||
|
||
const results = try bench.run(); | ||
defer results.deinit(); | ||
try results.prettyPrint(stdout, true); | ||
try bench.add("My Benchmark", myBenchmark, .{}); | ||
try bench.run(stdout); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const std = @import("std"); | ||
const zbench = @import("zbench"); | ||
|
||
fn beforeAllHook() void { | ||
std.debug.print("Starting benchmarking...\n", .{}); | ||
} | ||
|
||
fn afterAllHook() void { | ||
std.debug.print("Finished benchmarking.\n", .{}); | ||
} | ||
|
||
fn myBenchmark(_: std.mem.Allocator) void { | ||
var result: usize = 0; | ||
for (0..1_000_000) |i| { | ||
std.mem.doNotOptimizeAway(i); | ||
result += i * i; | ||
} | ||
} | ||
|
||
test "bench test hooks" { | ||
const stdout = std.io.getStdOut().writer(); | ||
var bench = zbench.Benchmark.init(std.testing.allocator, .{}); | ||
defer bench.deinit(); | ||
|
||
try bench.add("My Benchmark", myBenchmark, .{ | ||
.iterations = 100, | ||
.hooks = .{ | ||
.before_all = beforeAllHook, | ||
.after_all = afterAllHook, | ||
}, | ||
}); | ||
try bench.run(stdout); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const std = @import("std"); | ||
const zbench = @import("zbench"); | ||
const test_allocator = std.testing.allocator; | ||
|
||
fn myBenchmark1(_: std.mem.Allocator) void { | ||
var result: usize = 0; | ||
for (0..100_000) |i| { | ||
std.mem.doNotOptimizeAway(i); | ||
result += i * i; | ||
} | ||
} | ||
|
||
fn myBenchmark2(_: std.mem.Allocator) void { | ||
var result: usize = 0; | ||
for (0..200_000) |i| { | ||
std.mem.doNotOptimizeAway(i); | ||
result += i * i; | ||
} | ||
} | ||
|
||
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, .{}); | ||
try bench.add("My Benchmark 2", myBenchmark2, .{}); | ||
|
||
var progress = std.Progress{}; | ||
const progress_node = progress.start("", 0); | ||
defer progress_node.end(); | ||
|
||
try stdout.writeAll("\n"); | ||
try bench.prettyPrintHeader(stdout); | ||
var iter = try bench.iterator(); | ||
while (try iter.next()) |step| switch (step) { | ||
.progress => |p| { | ||
progress_node.setEstimatedTotalItems(p.total_runs); | ||
progress_node.setCompletedItems(p.completed_runs); | ||
progress_node.setName(p.current_name); | ||
progress.maybeRefresh(); | ||
}, | ||
.result => |x| { | ||
defer x.deinit(); | ||
progress_node.setName(""); | ||
progress_node.setEstimatedTotalItems(0); | ||
progress_node.setCompletedItems(0); | ||
progress.refresh(); | ||
try x.prettyPrint(stdout, true); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const std = @import("std"); | ||
const zbench = @import("zbench"); | ||
|
||
fn myBenchmark(_: std.mem.Allocator) void { | ||
var result: usize = 0; | ||
for (0..1_000_000) |i| { | ||
std.mem.doNotOptimizeAway(i); | ||
result += i * i; | ||
} | ||
} | ||
|
||
test "bench test system info" { | ||
const stdout = std.io.getStdOut().writer(); | ||
var bench = zbench.Benchmark.init(std.testing.allocator, .{}); | ||
defer bench.deinit(); | ||
|
||
const sysinfo = try bench.getSystemInfo(); | ||
try std.fmt.format(stdout, "\n{}\n", .{sysinfo}); | ||
|
||
try bench.add("My Benchmark", myBenchmark, .{}); | ||
try bench.run(stdout); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.