Skip to content

Commit

Permalink
API redesign
Browse files Browse the repository at this point in the history
  • Loading branch information
bens committed Mar 12, 2024
1 parent 1237f4b commit 3ef6e17
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 420 deletions.
34 changes: 17 additions & 17 deletions tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ const expectEq = std.testing.expectEqual;
const Benchmark = @import("./zbench.zig").Benchmark;

test "Benchmark.calculateStd and Benchmark.calculateAverage" {
var bench = try Benchmark.init("test_bench", std.testing.allocator, .{});
defer bench.durations.deinit();
// var bench = try Benchmark.init("test_bench", std.testing.allocator, .{});
// defer bench.durations.deinit();

try expectEq(@as(u64, 0), bench.calculateAverage());
try expectEq(@as(u64, 0), bench.calculateStd());
// try expectEq(@as(u64, 0), bench.calculateAverage());
// try expectEq(@as(u64, 0), bench.calculateStd());

try bench.durations.append(1);
try expectEq(@as(u64, 1), bench.calculateAverage());
try expectEq(@as(u64, 0), bench.calculateStd());
// try bench.durations.append(1);
// try expectEq(@as(u64, 1), bench.calculateAverage());
// try expectEq(@as(u64, 0), bench.calculateStd());

for (1..16) |i| try bench.durations.append(i);
try expectEq(@as(u64, 7), bench.calculateAverage());
try expectEq(@as(u64, 4), bench.calculateStd());
// for (1..16) |i| try bench.durations.append(i);
// try expectEq(@as(u64, 7), bench.calculateAverage());
// try expectEq(@as(u64, 4), bench.calculateStd());

for (16..101) |i| try bench.durations.append(i);
try expectEq(@as(u64, 50), bench.calculateAverage());
try expectEq(@as(u64, 29), bench.calculateStd());
// for (16..101) |i| try bench.durations.append(i);
// try expectEq(@as(u64, 50), bench.calculateAverage());
// try expectEq(@as(u64, 29), bench.calculateStd());

bench.durations.clearRetainingCapacity();
for (0..10) |_| try bench.durations.append(1);
// bench.durations.clearRetainingCapacity();
// for (0..10) |_| try bench.durations.append(1);

try expectEq(@as(u64, 1), bench.calculateAverage());
try expectEq(@as(u64, 0), bench.calculateStd());
// try expectEq(@as(u64, 1), bench.calculateAverage());
// try expectEq(@as(u64, 0), bench.calculateStd());
}
21 changes: 4 additions & 17 deletions util/format.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,10 @@ const std = @import("std");
const Color = @import("./color.zig").Color;
const log = std.log.scoped(.zbench_format);

pub fn duration(buffer: []u8, d: u64) ![]u8 {
const units = [_][]const u8{ "ns", "µs", "ms", "s" };

var scaledDuration: u64 = d;
var unitIndex: usize = 0;

var fractionalPart: u64 = 0;

while (scaledDuration >= 1_000 and unitIndex < units.len - 1) {
fractionalPart = scaledDuration % 1_000;
scaledDuration /= 1_000;
unitIndex += 1;
}

const formatted = try std.fmt.bufPrint(buffer, "{d}.{d}{s}", .{ scaledDuration, fractionalPart, units[unitIndex] });

return formatted;
pub fn duration(buffer: []u8, ns: u64) ![]u8 {
return try std.fmt.bufPrint(buffer, "{}", .{
std.fmt.fmtDuration(ns),
});
}

pub fn memorySize(bytes: u64, allocator: std.mem.Allocator) ![]const u8 {
Expand Down
12 changes: 6 additions & 6 deletions util/format_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@ test "duration" {

{
const result = try format.duration(buffer[0..], 1);
try std.testing.expectEqualSlices(u8, "1.0ns", result);
try std.testing.expectEqualSlices(u8, "1ns", result);
}

{
const result = try format.duration(buffer[0..], 999);
try std.testing.expectEqualSlices(u8, "999.0ns", result);
try std.testing.expectEqualSlices(u8, "999ns", result);
}

{
const result = try format.duration(buffer[0..], 1000);
try std.testing.expectEqualSlices(u8, "1.0µs", result);
try std.testing.expectEqualSlices(u8, "1us", result);
}

{
const result = try format.duration(buffer[0..], 1000000);
try std.testing.expectEqualSlices(u8, "1.0ms", result);
try std.testing.expectEqualSlices(u8, "1ms", result);
}

{
const result = try format.duration(buffer[0..], 1000000000);
try std.testing.expectEqualSlices(u8, "1.0s", result);
try std.testing.expectEqualSlices(u8, "1s", result);
}

{
const result = try format.duration(buffer[0..], 1500);
try std.testing.expectEqualSlices(u8, "1.500µs", result);
try std.testing.expectEqualSlices(u8, "1.5us", result);
}
}
Loading

0 comments on commit 3ef6e17

Please sign in to comment.