-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
introduce scoped logging & revise debug print #34
Changes from 5 commits
730ef4f
4966c55
18cffa6
bf9a112
feded9d
4a8462b
f9d7fc8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
//!zig-autodoc-guide: docs/advanced.md | ||
|
||
const std = @import("std"); | ||
const log = std.log.scoped(.zbench); | ||
|
||
const c = @import("./util/color.zig"); | ||
const format = @import("./util/format.zig"); | ||
|
||
|
@@ -82,8 +84,9 @@ pub const Benchmark = struct { | |
} | ||
|
||
/// Prints a report of total operations performed during the benchmark. | ||
pub fn report(self: *Benchmark) void { | ||
std.debug.print("Total operations: {}\n", .{self.total_operations}); | ||
pub fn report(self: *Benchmark) !void { | ||
const stdout = std.io.getStdOut().writer(); | ||
try stdout.print("\nTotal operations: {}\n", .{self.total_operations}); | ||
} | ||
|
||
pub const Percentiles = struct { | ||
|
@@ -125,7 +128,7 @@ pub const Benchmark = struct { | |
if (len > 1) { | ||
lastIndex = len - 1; | ||
} else { | ||
std.debug.print("Cannot calculate percentiles: recorded less than two durations\n", .{}); | ||
log.debug("Cannot calculate percentiles: recorded less than two durations", .{}); | ||
return Percentiles{ .p75 = 0, .p99 = 0, .p995 = 0 }; | ||
} | ||
quickSort(self.durations.items, 0, lastIndex - 1); | ||
|
@@ -168,9 +171,16 @@ pub const Benchmark = struct { | |
var min_max_buffer: [128]u8 = undefined; | ||
const min_max_str = try std.fmt.bufPrint(min_max_buffer[0..], "({s} ... {s})", .{ min_str, max_str }); | ||
|
||
std.debug.print("{s:<22} {s:<8} {s:<22} {s:<28} {s:<10} {s:<10} {s:<10}\n", .{ "benchmark", "runs", "time (avg ± σ)", "(min ... max)", "p75", "p99", "p995" }); | ||
std.debug.print("---------------------------------------------------------------------------------------------------------------\n", .{}); | ||
std.debug.print("{s:<22} \x1b[90m{d:<8} \x1b[33m{s:<22} \x1b[95m{s:<28} \x1b[90m{s:<10} {s:<10} {s:<10}\x1b[0m\n", .{ self.name, self.total_operations, avg_std_str, min_max_str, p75_str, p99_str, p995_str }); | ||
const stdout = std.io.getStdOut().writer(); | ||
try stdout.print( | ||
"{s:<22} {s:<8} {s:<22} {s:<28} {s:<10} {s:<10} {s:<10}\n", | ||
.{ "benchmark", "runs", "time (avg ± σ)", "(min ... max)", "p75", "p99", "p995" }, | ||
); | ||
try stdout.print("---------------------------------------------------------------------------------------------------------------\n", .{}); | ||
try stdout.print( | ||
"{s:<22} \x1b[90m{d:<8} \x1b[33m{s:<22} \x1b[95m{s:<28} \x1b[90m{s:<10} {s:<10} {s:<10}\x1b[0m\n", | ||
.{ self.name, self.total_operations, avg_std_str, min_max_str, p75_str, p99_str, p995_str }, | ||
); | ||
} | ||
|
||
/// Calculate the average duration | ||
|
@@ -248,7 +258,7 @@ pub const BenchmarkResults = struct { | |
/// Formats and prints the benchmark results in a readable format. | ||
pub fn prettyPrint(self: BenchmarkResults) !void { | ||
const stdout = std.io.getStdOut().writer(); | ||
std.debug.print("--------------------------------------------------------------------------------------\n", .{}); | ||
stdout.print("--------------------------------------------------------------------------------------\n", .{}); | ||
|
||
for (self.results.items) |result| { | ||
try stdout.print("{s}", .{result.name}); | ||
|
@@ -317,7 +327,7 @@ pub fn run(comptime func: BenchFunc, bench: *Benchmark, benchResult: *BenchmarkR | |
}); | ||
|
||
bench.setTotalOperations(bench.N); | ||
bench.report(); | ||
|
||
try bench.report(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need to call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, good point. Should we rename There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose? I don't have strong opinions on the naming either way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. implemented via 4a8462b |
||
try bench.prettyPrint(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for bumping the version! 👍