-
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.
Merge branch 'incremental' into memory-tracking
- Loading branch information
Showing
11 changed files
with
370 additions
and
104 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,40 +1,19 @@ | ||
const std = @import("std"); | ||
const zbench = @import("zbench"); | ||
const test_allocator = std.testing.allocator; | ||
|
||
fn myBenchmark(allocator: std.mem.Allocator) void { | ||
fn myBenchmark(_: std.mem.Allocator) void { | ||
var result: usize = 0; | ||
for (0..1000) |i| result += i * i; | ||
const buf = allocator.alloc(u8, 2 * 1024) catch unreachable; | ||
defer allocator.free(buf); | ||
} | ||
|
||
fn beforeAllHook() void { | ||
std.debug.print("Starting benchmarking...\n", .{}); | ||
} | ||
|
||
fn afterAllHook() void { | ||
std.debug.print("Finished benchmarking.\n", .{}); | ||
for (0..1_000_000) |i| { | ||
std.mem.doNotOptimizeAway(i); | ||
result += i * i; | ||
} | ||
} | ||
|
||
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, | ||
}, | ||
.enable_allocation_tracking = true, | ||
}); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const std = @import("std"); | ||
const zbench = @import("zbench"); | ||
const test_allocator = std.testing.allocator; | ||
|
||
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 json" { | ||
const stdout = std.io.getStdOut().writer(); | ||
var bench = zbench.Benchmark.init(test_allocator, .{}); | ||
defer bench.deinit(); | ||
|
||
try bench.add("My Benchmark 1", myBenchmark, .{ .iterations = 10 }); | ||
|
||
try stdout.writeAll("["); | ||
var iter = try bench.iterator(); | ||
var i: usize = 0; | ||
while (try iter.next()) |step| switch (step) { | ||
.progress => |_| {}, | ||
.result => |x| { | ||
defer x.deinit(); | ||
defer i += 1; | ||
if (0 < i) try stdout.writeAll(", "); | ||
try x.writeJSON(stdout); | ||
}, | ||
}; | ||
try stdout.writeAll("]\n"); | ||
} |
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,31 @@ | ||
const std = @import("std"); | ||
const zbench = @import("zbench"); | ||
const test_allocator = std.testing.allocator; | ||
|
||
const MyBenchmark = struct { | ||
loops: usize, | ||
|
||
fn init(loops: usize) MyBenchmark { | ||
return .{ .loops = loops }; | ||
} | ||
|
||
pub fn run(self: MyBenchmark, _: std.mem.Allocator) void { | ||
var result: usize = 0; | ||
for (0..self.loops) |i| { | ||
std.mem.doNotOptimizeAway(i); | ||
result += i * i; | ||
} | ||
} | ||
}; | ||
|
||
test "bench test parameterised" { | ||
const stdout = std.io.getStdOut().writer(); | ||
var bench = zbench.Benchmark.init(test_allocator, .{}); | ||
defer bench.deinit(); | ||
|
||
try bench.addParam("My Benchmark 1", &MyBenchmark.init(100_000), .{}); | ||
try bench.addParam("My Benchmark 2", &MyBenchmark.init(200_000), .{}); | ||
|
||
try stdout.writeAll("\n"); | ||
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
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 zbench.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.