Skip to content

Commit

Permalink
Fix small formatting bug, move prettyPrintHeader to utils (#43)
Browse files Browse the repository at this point in the history
After 1389ebf we forget to print a newline in the for-loop
within BenchmarkResults.prettyPrint, causing messed-up formatting.
We also forgot to call BenchmarkResults.getColor when determining
the color for total-time. Both of these are now fixed. Also moved
prettyPrintHeader to util/format.zig for consistency.
  • Loading branch information
Bryysen authored and hendriknielaender committed Feb 3, 2024
1 parent fce673d commit 9a9e301
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
10 changes: 10 additions & 0 deletions util/format.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ pub fn duration(buffer: []u8, d: u64) ![]u8 {
return formatted;
}

/// Pretty-prints the header for the result pretty-print table
/// writer: Type that has the associated method print (for example std.io.getStdOut.writer())
pub fn prettyPrintHeader(writer: anytype) !void {
try writer.print(
"\n{s:<22} {s:<8} {s:<14} {s:<22} {s:<28} {s:<10} {s:<10} {s:<10}\n",
.{ "benchmark", "runs", "total time", "time/run (avg ± σ)", "(min ... max)", "p75", "p99", "p995" },
);
try writer.print("-----------------------------------------------------------------------------------------------------------------------------\n", .{});
}

/// Pretty-prints the name of the benchmark
/// writer: Type that has the associated method print (for example std.io.getStdOut.writer())
pub fn prettyPrintName(name: []const u8, writer: anytype, color: Color) !void {
Expand Down
20 changes: 6 additions & 14 deletions zbench.zig
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ pub const Benchmark = struct {
const min_max_str = try std.fmt.bufPrint(min_max_buffer[0..], "({s} ... {s})", .{ min_str, max_str });

const stdout = std.io.getStdOut().writer();
prettyPrintHeader();
format.prettyPrintHeader();
try stdout.print("---------------------------------------------------------------------------------------------------------------\n", .{});
try stdout.print(
"{s:<22} \x1b[90m{d:<8} \x1b[90m{s:<10} \x1b[33m{s:<22} \x1b[95m{s:<28} \x1b[90m{s:<10} {s:<10} {s:<10}\x1b[0m\n\n",
Expand Down Expand Up @@ -270,7 +270,7 @@ pub const BenchmarkResult = struct {
/// writer: Type that has the associated method print (for example std.io.getStdOut.writer())
/// header: Whether to pretty-print the header or not
pub fn prettyPrint(self: Self, writer: anytype, header: bool) !void {
if (header) try prettyPrintHeader(writer);
if (header) try format.prettyPrintHeader(writer);

try format.prettyPrintName(self.name, writer, Color.none);
try format.prettyPrintTotalOperations(self.total_operations, writer, Color.cyan);
Expand All @@ -283,16 +283,6 @@ pub const BenchmarkResult = struct {
}
};

/// Pretty-prints the header for the result pretty-print table
/// writer: Type that has the associated method print (for example std.io.getStdOut.writer())
pub fn prettyPrintHeader(writer: anytype) !void {
try writer.print(
"\n{s:<22} {s:<8} {s:<14} {s:<22} {s:<28} {s:<10} {s:<10} {s:<10}\n",
.{ "benchmark", "runs", "total time", "time/run (avg ± σ)", "(min ... max)", "p75", "p99", "p995" },
);
try writer.print("-----------------------------------------------------------------------------------------------------------------------------\n", .{});
}

/// BenchmarkResults acts as a container for multiple benchmark results.
/// It provides functionality to format and print these results.
pub const BenchmarkResults = struct {
Expand Down Expand Up @@ -323,14 +313,16 @@ pub const BenchmarkResults = struct {
/// Formats and prints the benchmark results in a readable format.
pub fn prettyPrint(self: *BenchmarkResults) !void {
var writer = self.out_stream.writer();
try prettyPrintHeader(writer);
try format.prettyPrintHeader(writer);
for (self.results.items) |result| {
try format.prettyPrintName(result.name, writer, Color.none);
try format.prettyPrintTotalOperations(result.total_operations, writer, Color.cyan);
try format.prettyPrintTotalTime(result.total_time, writer, Color.cyan);
try format.prettyPrintTotalTime(result.total_time, writer, self.getColor(result.total_time));
try format.prettyPrintAvgStd(result.avg_duration, result.std_duration, writer, Color.green);
try format.prettyPrintMinMax(result.min_duration, result.max_duration, writer, Color.blue);
try format.prettyPrintPercentiles(result.percentiles.p75, result.percentiles.p99, result.percentiles.p995, writer, Color.cyan);

_ = try writer.write("\n");
}

try self.out_stream.flush();
Expand Down

0 comments on commit 9a9e301

Please sign in to comment.