Skip to content

Commit

Permalink
feat(benchmark): add lifecycle hooks for enhanced benchmark control
Browse files Browse the repository at this point in the history
  • Loading branch information
hendriknielaender committed Mar 7, 2024
1 parent 7e0b9d4 commit 310e220
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
17 changes: 16 additions & 1 deletion examples/basic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ fn helloWorld() []const u8 {
return "Hello, world!";
}

fn beforeAllHook() void {
std.debug.print("Starting benchmarking...\n", .{});
}

fn afterAllHook() void {
std.debug.print("Finished benchmarking.\n", .{});
}

fn myBenchmark(_: *zbench.Benchmark) void {
_ = helloWorld();
}
Expand All @@ -21,7 +29,14 @@ test "bench test basic" {
const resultsAlloc = std.ArrayList(zbench.BenchmarkResult).init(test_allocator);
var benchmarkResults = zbench.BenchmarkResults.init(resultsAlloc);
defer benchmarkResults.deinit();
var bench = try zbench.Benchmark.init("My Benchmark", test_allocator, .{ .iterations = 10, .display_system_info = true });
var bench = try zbench.Benchmark.init("My Benchmark", test_allocator, .{
.iterations = 10,
.display_system_info = true,
.hooks = .{
.beforeAll = beforeAllHook,
.afterAll = afterAllHook,
},
});

try zbench.run(myBenchmark, &bench, &benchmarkResults);
try benchmarkResults.prettyPrint();
Expand Down
41 changes: 41 additions & 0 deletions zbench.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ const format = @import("./util/format.zig");
const quicksort = @import("./util/quicksort.zig");
const platform = @import("./util/platform.zig");

/// LifecycleHooks containing optional hooks for lifecycle events in benchmarking.
/// Each field in this struct is a nullable function pointer.
const LifecycleHooks = struct {
beforeAll: ?*const fn () void = null,
afterAll: ?*const fn () void = null,
beforeEach: ?*const fn () void = null,
afterEach: ?*const fn () void = null,
};

/// Configuration for benchmarking.
/// This struct holds settings to control the behavior of benchmark executions.
pub const Config = struct {
Expand All @@ -27,6 +36,16 @@ pub const Config = struct {
/// before concluding. Helps in avoiding long-running benchmarks.
time_budget: u64 = 2e9, // 2 seconds

/// Configuration for lifecycle hooks in benchmarking.
/// Provides the ability to define custom actions at different stages of the benchmark process:
/// - `beforeAll`: A hook that runs once before all benchmarks begin.
/// - `afterAll`: A hook that runs once after all benchmarks have completed.
/// - `beforeEach`: A hook that runs before each individual benchmark.
/// - `afterEach`: A hook that runs after each individual benchmark.
/// This allows for custom setup and teardown operations, as well as fine-grained control
/// over the environment in which benchmarks are run.
hooks: LifecycleHooks = .{},

/// Flag to indicate whether system information should be displayed. Default is false.
/// If true, detailed system information (e.g., CPU, memory) will be displayed
/// along with the benchmark results. Useful for understanding the environment
Expand Down Expand Up @@ -317,6 +336,18 @@ pub fn run(comptime func: BenchFunc, bench: *Benchmark, benchResult: *BenchmarkR
, .{ info.platform, info.cpu, info.cpu_cores, info.memory_total });
}

// Call beforeAll hook if defined
if (bench.config.hooks.beforeAll) |hook| {
hook();
}

defer {
// Call afterAll hook if defined
if (bench.config.hooks.afterAll) |hook| {
hook();
}
}

if (bench.config.iterations != 0) {
// If user-defined iterations are specified, use them directly
bench.N = bench.config.iterations;
Expand Down Expand Up @@ -361,9 +392,19 @@ pub fn run(comptime func: BenchFunc, bench: *Benchmark, benchResult: *BenchmarkR
bench.reset();
var j: usize = 0;
while (j < bench.N) : (j += 1) {
// Call beforeEach hook if defined
if (bench.config.hooks.beforeEach) |hook| {
hook();
}

bench.start();
func(bench);
bench.stop();

// Call afterEach hook if defined
if (bench.config.hooks.afterEach) |hook| {
hook();
}
}

bench.setTotalOperations(bench.N);
Expand Down

0 comments on commit 310e220

Please sign in to comment.