Skip to content

Commit

Permalink
Added init/deinit to BenchmarkResults and updated examples
Browse files Browse the repository at this point in the history
This was done to circumvent hendriknielaender#49, which is (most likely) caused by a
bug in the compiler.
  • Loading branch information
Bryysen committed Feb 19, 2024
1 parent fd2ba89 commit 9fba59c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
8 changes: 3 additions & 5 deletions examples/basic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ fn myBenchmark(_: *zbench.Benchmark) void {
}

test "bench test basic" {
const resultsAlloc = std.ArrayList(zbench.BenchmarkResult).init(test_allocator);
var bench = try zbench.Benchmark.init("My Benchmark", test_allocator, .{ .iterations = 10 });
var benchmarkResults = zbench.BenchmarkResults{
.results = resultsAlloc,
};
defer benchmarkResults.results.deinit();
var benchmarkResults = zbench.BenchmarkResults.init(test_allocator);
defer benchmarkResults.deinit();

try zbench.run(myBenchmark, &bench, &benchmarkResults);
try benchmarkResults.prettyPrint();
}
8 changes: 3 additions & 5 deletions examples/bubble_sort.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ fn myBenchmark(_: *zbench.Benchmark) void {
}

test "bench test bubbleSort" {
const resultsAlloc = std.ArrayList(zbench.BenchmarkResult).init(test_allocator);
var bench = try zbench.Benchmark.init("Bubble Sort Benchmark", test_allocator, .{});
var benchmarkResults = zbench.BenchmarkResults{
.results = resultsAlloc,
};
defer benchmarkResults.results.deinit();
var benchmarkResults = zbench.BenchmarkResults.init(test_allocator);
defer benchmarkResults.deinit();

try zbench.run(myBenchmark, &bench, &benchmarkResults);
try benchmarkResults.prettyPrint();
}
8 changes: 3 additions & 5 deletions examples/sleep.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ fn sleepBenchmark(_: *zbench.Benchmark) void {
}

test "bench test sleepy" {
const resultsAlloc = std.ArrayList(zbench.BenchmarkResult).init(test_allocator);
var bench = try zbench.Benchmark.init("Sleep Benchmark", test_allocator, .{});
var benchmarkResults = zbench.BenchmarkResults{
.results = resultsAlloc,
};
defer benchmarkResults.results.deinit();
var benchmarkResults = zbench.BenchmarkResults.init(test_allocator);
defer benchmarkResults.deinit();

try zbench.run(sleepBenchmark, &bench, &benchmarkResults);
try benchmarkResults.prettyPrint();
}
17 changes: 16 additions & 1 deletion zbench.zig
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,26 @@ pub fn prettyPrintHeader(writer: anytype) !void {
/// It provides functionality to format and print these results.
pub const BenchmarkResults = struct {
const Color = c.Color;
const BufferedStdoutWriter = std.io.BufferedWriter(1024, @TypeOf(std.io.getStdOut().writer()));

/// A dynamic list of BenchmarkResult objects.
results: std.ArrayList(BenchmarkResult),
/// A handle to a buffered stdout-writer. Used for printing-operations
out_stream: std.io.BufferedWriter(1024, @TypeOf(std.io.getStdOut().writer())) = .{ .unbuffered_writer = std.io.getStdOut().writer() },
out_stream: BufferedStdoutWriter,

// NOTE: This init function is technically not needed however it was added to circumvent a (most likely) bug
// in the 0.11.0 compiler (see issue #49)
pub fn init(alloc: std.mem.Allocator) BenchmarkResults {
return BenchmarkResults {
.results = std.ArrayList(BenchmarkResult).init(alloc),
.out_stream = .{ .unbuffered_writer = std.io.getStdOut().writer() },
};
}

pub fn deinit(self: *BenchmarkResults) void {
// self.out_stream.flush();
self.results.deinit();
}

/// Determines the color representation based on the total-time of the benchmark.
/// total_time: The total-time to evaluate.
Expand Down

0 comments on commit 9fba59c

Please sign in to comment.