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

close memory leak with testing allocator #16

Merged
merged 4 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 4 additions & 15 deletions examples/basic.zig
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
const std = @import("std");
const zbench = @import("zbench");

pub const CustomAllocator = struct {
allocator: std.mem.Allocator,

pub fn create() CustomAllocator {
return CustomAllocator{ .allocator = std.heap.page_allocator };
}

pub fn as_mut(self: *CustomAllocator) *std.mem.Allocator {
return &self.allocator;
}
};
const test_allocator = std.testing.allocator;

fn helloWorld() []const u8 {
var result: usize = 0;
Expand All @@ -29,13 +18,13 @@ fn myBenchmark(_: *zbench.Benchmark) void {
}

test "bench test basic" {
var customAllocator = CustomAllocator.create();
var resultsAlloc = std.ArrayList(zbench.BenchmarkResult).init(customAllocator.allocator);
var bench = try zbench.Benchmark.init("My Benchmark", customAllocator.as_mut());
var resultsAlloc = std.ArrayList(zbench.BenchmarkResult).init(test_allocator);
var bench = try zbench.Benchmark.init("My Benchmark", test_allocator);

var benchmarkResults = zbench.BenchmarkResults{
.results = resultsAlloc,
};
defer benchmarkResults.results.deinit();

try zbench.run(myBenchmark, &bench, &benchmarkResults);
}
19 changes: 4 additions & 15 deletions examples/bubble_sort.zig
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
const std = @import("std");
const inc = @import("include");
const zbench = @import("zbench");

pub const CustomAllocator = struct {
allocator: std.mem.Allocator,

pub fn create() CustomAllocator {
return CustomAllocator{ .allocator = std.heap.page_allocator };
}

pub fn as_mut(self: *CustomAllocator) *std.mem.Allocator {
return &self.allocator;
}
};
const test_allocator = std.testing.allocator;

fn bubbleSort(nums: []i32) void {
var i: usize = nums.len - 1;
Expand All @@ -34,11 +23,11 @@ fn myBenchmark(_: *zbench.Benchmark) void {
}

test "bench test bubbleSort" {
var customAllocator = CustomAllocator.create();
var resultsAlloc = std.ArrayList(zbench.BenchmarkResult).init(customAllocator.allocator);
var bench = try zbench.Benchmark.init("Bubble Sort Benchmark", customAllocator.as_mut());
var 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();
try zbench.run(myBenchmark, &bench, &benchmarkResults);
}
13 changes: 7 additions & 6 deletions zbench.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ pub const Benchmark = struct {
maxDuration: u64 = 0,
totalDuration: u64 = 0,
durations: std.ArrayList(u64),
allocator: *std.mem.Allocator,
allocator: std.mem.Allocator,
startTime: u64,

pub fn init(name: []const u8, allocator: *std.mem.Allocator) !Benchmark {
var startTime: u64 = @intCast(std.time.microTimestamp());
pub fn init(name: []const u8, allocator: std.mem.Allocator) !Benchmark {
var startTime: u64 = @intCast(std.time.nanoTimestamp());
if (startTime < 0) {
std.debug.warn("Failed to get start time. Defaulting to 0.\n", .{});
startTime = 0;
}

var bench = Benchmark{
.name = name,
.timer = t.Timer{ .startTime = startTime },
.durations = std.ArrayList(u64).init(allocator.*),
.allocator = allocator,
.timer = t.Timer{ .startTime = startTime },
.durations = std.ArrayList(u64).init(allocator),
.startTime = startTime,
};
bench.timer.start();
Expand Down Expand Up @@ -58,7 +58,7 @@ pub const Benchmark = struct {
self.maxDuration = 0;
self.totalDuration = 0;
self.durations.deinit();
self.durations = std.ArrayList(u64).init(self.allocator.*);
self.durations = std.ArrayList(u64).init(self.allocator);
}

// Function to get elapsed time since benchmark start
Expand Down Expand Up @@ -217,6 +217,7 @@ pub const BenchmarkResults = struct {
};

pub fn run(comptime func: BenchFunc, bench: *Benchmark, benchResult: *BenchmarkResults) !void {
defer bench.durations.deinit();
const MIN_DURATION = 1_000_000; // minimum benchmark time in nanoseconds (1 millisecond)
const MAX_N = 10000; // maximum number of executions for the final benchmark run
const MAX_ITERATIONS = 10; // Define a maximum number of iterations
Expand Down