Skip to content
Open
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
51 changes: 51 additions & 0 deletions packages/core/bench_runner.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const std = @import("std");
const builtin = @import("builtin");

pub const Options = struct {
iterations: usize = 100,
};

fn percentile(samples: []u64, perc: f64) u64 {
const idx = @as(usize, @intFromFloat(perc * @as(f64, @floatFromInt(samples.len - 1))));
return samples[idx];
}

fn runBench(name: []const u8, func: *const fn() anyerror!void, opts: Options) !void {
const samples = try std.heap.page_allocator.alloc(u64, opts.iterations);
defer std.heap.page_allocator.free(samples);

var timer = try std.time.Timer.start();
for (samples, 0..) |*s, i| {
_ = i;
const start = timer.read();
try func();
const end = timer.read();
s.* = end - start;
}

std.sort.heap(u64, samples, {}, std.sort.asc(u64));

var total: u128 = 0;
for (samples) |s| total += s;

const avg = total / samples.len;
const min = samples[0];
const max = samples[samples.len - 1];
const p75 = percentile(samples, 0.75);
const p99 = percentile(samples, 0.99);

std.debug.print("{s}: avg {d} ns (min {d}, p75 {d}, p99 {d}, max {d})\n", .{ name, avg, min, p75, p99, max });
}


pub fn main() !void {
// Print discovered test names
for (builtin.test_functions) |t| {
std.debug.print("found test: {s}\n", .{t.name});
}

for (builtin.test_functions) |t| {
if (std.mem.indexOf(u8, t.name, "bench") == null) continue;
try runBench(t.name, t.func, .{});
}
}
23 changes: 23 additions & 0 deletions packages/core/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,27 @@ pub fn build(b: *std.Build) !void {
&install_step.step,
test_debugger,
});

// ---------------- Benchmarks ----------------
const bench_tests = b.addTest(.{
.root_source_file = b.path("src/wasm.zig"),
.name = "bench",
.target = target,
.optimize = optimize,
.test_runner = .{
.path = b.path("bench_runner.zig"),
.mode = .simple,
},
});

const run_bench = b.addRunArtifact(bench_tests);
if (b.args) |args| {
run_bench.addArgs(args);
}

const bench_step = b.step("bench", "Run benchmarks");
pipe(.{
&run_bench.step,
bench_step,
});
}
1 change: 1 addition & 0 deletions packages/core/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ test {
_ = @import("./renderer/Renderer.zig");
_ = @import("./renderer/gradient.zig");
_ = @import("./layout/Selection.zig");
_ = @import("./sample_bench.zig");
}
9 changes: 9 additions & 0 deletions packages/core/src/sample_bench.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const std = @import("std");

pub const options = .{ .iterations = 10 };

test "simple bench" {
var sum: usize = 0;
for (0..1000) |i| sum += i;
std.mem.doNotOptimizeAway(sum);
}
1 change: 1 addition & 0 deletions packages/core/src/wasm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -586,4 +586,5 @@ test {
_ = @import("./uni/LineBreakStream.zig");
_ = @import("./layout/v2/text/white-space-processor.zig");
_ = @import("./layout/v2/text/LinesBuilder.zig");
_ = @import("./sample_bench.zig");
}