From d080dde361269af8eb8d1eef8a225331a8dcf1e0 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Mon, 8 Apr 2024 14:54:55 +0200 Subject: [PATCH] test: bench: use pretty for console output --- build.zig | 7 +++++++ src/run_tests.zig | 27 ++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/build.zig b/build.zig index 4857ae96..a414b2d6 100644 --- a/build.zig +++ b/build.zig @@ -85,6 +85,13 @@ pub fn build(b: *std.build.Builder) !void { .single_threaded = true, }); try common(tests, options); + + // add jsruntime pretty deps + const pretty = tests.step.owner.createModule(.{ + .source_file = .{ .path = "vendor/jsruntime-lib/src/pretty.zig" }, + }); + tests.addModule("pretty", pretty); + const run_tests = b.addRunArtifact(tests); if (b.args) |args| { run_tests.addArgs(args); diff --git a/src/run_tests.zig b/src/run_tests.zig index 610dfaa6..dcf01372 100644 --- a/src/run_tests.zig +++ b/src/run_tests.zig @@ -3,6 +3,7 @@ const builtin = @import("builtin"); const jsruntime = @import("jsruntime"); const generate = @import("generate.zig"); +const pretty = @import("pretty"); const parser = @import("netsurf.zig"); const apiweb = @import("apiweb.zig"); @@ -99,12 +100,13 @@ const usage = \\ ; -// Out list all the ouputs handled by bench. +// Out list all the ouputs handled by benchmark result and written on stdout. const Out = enum { - none, + text, json, }; +// Which tests must be run. const Run = enum { all, js, @@ -122,7 +124,7 @@ pub fn main() !void { // ignore the exec name. _ = args.next().?; - var out: Out = .none; + var out: Out = .text; var run: Run = .all; while (args.next()) |arg| { @@ -167,11 +169,10 @@ fn run_js(out: Out) !void { try testJSRuntime(bench_alloc.allocator()); const duration = std.time.Instant.since(try std.time.Instant.now(), start); + const stats = bench_alloc.stats(); // get and display the results if (out == .json) { - const stats = bench_alloc.stats(); - const res = [_]struct { name: []const u8, bench: struct { @@ -191,9 +192,25 @@ fn run_js(out: Out) !void { }; try std.json.stringify(res, .{ .whitespace = .indent_2 }, std.io.getStdOut().writer()); + return; } + + // display console result by default + const dur = pretty.Measure{ .unit = "us", .value = duration / us }; + const size = pretty.Measure{ .unit = "kb", .value = stats.alloc_size / kb }; + + // benchmark table + const row_shape = .{ []const u8, pretty.Measure, u64, u64, pretty.Measure }; + const table = try pretty.GenerateTable(1, row_shape, pretty.TableConf{ .margin_left = " " }); + const header = .{ "FUNCTION", "DURATION", "ALLOCATIONS (nb)", "RE-ALLOCATIONS (nb)", "HEAP SIZE" }; + var t = table.init("Benchmark browsercore 🚀", header); + try t.addRow(.{ "js", dur, stats.alloc_nb, stats.realloc_nb, size }); + try t.render(std.io.getStdOut().writer()); } +const kb = 1024; +const us = std.time.ns_per_us; + test { const asyncTest = @import("async/test.zig"); std.testing.refAllDecls(asyncTest);