Skip to content
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

Merged
merged 7 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const std = @import("std");
const version = std.SemanticVersion{ .major = 0, .minor = 1, .patch = 0 };
const log = std.log.scoped(.zbench_build);

const version = std.SemanticVersion{ .major = 0, .minor = 1, .patch = 2 };
Copy link
Owner

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! 👍


pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
Expand Down Expand Up @@ -43,15 +45,15 @@ fn setupTesting(b: *std.Build, target: std.zig.CrossTarget, optimize: std.builti
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);

const test_dirs = [_][]const u8{"util", "."};
const test_dirs = [_][]const u8{ "util", "." };
for (test_dirs) |dir| {
addTestsFromDir(b, test_step, dir, target, optimize);
}
}

fn addTestsFromDir(b: *std.Build, test_step: *std.Build.Step, dir_path: []const u8, target: std.zig.CrossTarget, optimize: std.builtin.OptimizeMode) void {
const iterableDir = std.fs.cwd().openIterableDir(dir_path, .{}) catch {
std.debug.print("Failed to open directory: {any}\n", .{dir_path});
log.warn("Failed to open directory: {s}", .{dir_path});
return;
};

Expand All @@ -60,7 +62,7 @@ fn addTestsFromDir(b: *std.Build, test_step: *std.Build.Step, dir_path: []const
const optionalEntry = it.next() catch |err| {
//TODO: break if access denied
//if (err == std.fs.IterableDir.ChmodError) break;
std.debug.print("Directory iteration error: {any}\n", .{err});
log.warn("Directory iteration error: {any}", .{err});
continue;
};

Expand Down
26 changes: 18 additions & 8 deletions zbench.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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});
Expand Down Expand Up @@ -317,7 +327,7 @@ pub fn run(comptime func: BenchFunc, bench: *Benchmark, benchResult: *BenchmarkR
});

bench.setTotalOperations(bench.N);
bench.report();

try bench.report();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to call Benchmark.report anymore now that Benchmark.prettyPrint displays the number of runs performed in the pretty-table

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, good point. Should we rename prettyPrint --> report then ?

Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implemented via 4a8462b

try bench.prettyPrint();
}
Loading