Skip to content

Commit 6f7c875

Browse files
Merge pull request #1067 from lightpanda-io/more_testing_metrics
Add libdom RSS and v8 total_physical_size to testing --json output
2 parents 516a783 + 4771021 commit 6f7c875

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

src/browser/mimalloc.zig

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,45 @@ pub fn destroy() void {
4040
heap = null;
4141
}
4242

43+
pub fn getRSS() i64 {
44+
if (@import("builtin").mode != .Debug) {
45+
// just don't trust my implementation, plus a caller might not know
46+
// that this requires parsing some unstructured data
47+
@compileError("Only available in debug builds");
48+
}
49+
var buf: [4096]u8 = undefined;
50+
var fba = std.heap.FixedBufferAllocator.init(&buf);
51+
var writer = std.Io.Writer.Allocating.init(fba.allocator());
52+
53+
c.mi_stats_print_out(struct {
54+
fn print(msg: [*c]const u8, data: ?*anyopaque) callconv(.c) void {
55+
const w: *std.Io.Writer = @ptrCast(@alignCast(data.?));
56+
w.writeAll(std.mem.span(msg)) catch |err| {
57+
std.debug.print("Failed to write mimalloc data: {}", .{err});
58+
};
59+
}
60+
}.print, &writer.writer);
61+
62+
const data = writer.written();
63+
const index = std.mem.indexOf(u8, data, "rss: ") orelse return -1;
64+
const sep = std.mem.indexOfScalarPos(u8, data, index + 5, ' ') orelse return -2;
65+
const value = std.fmt.parseFloat(f64, data[index+5..sep]) catch return -3;
66+
const unit = data[sep+1..];
67+
if (std.mem.startsWith(u8, unit, "KiB,")) {
68+
return @as(i64, @intFromFloat(value)) * 1024;
69+
}
70+
71+
if (std.mem.startsWith(u8, unit, "MiB,")) {
72+
return @as(i64, @intFromFloat(value)) * 1024 * 1024;
73+
}
74+
75+
if (std.mem.startsWith(u8, unit, "GiB,")) {
76+
return @as(i64, @intFromFloat(value)) * 1024 * 1024 * 1024;
77+
}
78+
79+
return -4;
80+
}
81+
4382
pub export fn m_alloc(size: usize) callconv(.c) ?*anyopaque {
4483
std.debug.assert(heap != null);
4584
return c.mi_heap_malloc(heap.?, size);

src/test_runner.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ pub const std_options = std.Options{
3535
};
3636

3737
pub var js_runner_duration: usize = 0;
38+
pub var v8_peak_memory: usize = 0;
39+
pub var libdom_memory: i64 = 0;
3840
pub var tracking_allocator: Allocator = undefined;
3941

4042
pub fn main() !void {
@@ -194,13 +196,13 @@ pub fn main() !void {
194196
.duration = js_runner_duration,
195197
.alloc_nb = 0,
196198
.realloc_nb = 0,
197-
.alloc_size = 0,
199+
.alloc_size = libdom_memory,
198200
} },
199201
.{ .name = "v8", .bench = .{
200202
.duration = js_runner_duration,
201203
.alloc_nb = 0,
202204
.realloc_nb = 0,
203-
.alloc_size = 0,
205+
.alloc_size = v8_peak_memory,
204206
} },
205207
.{ .name = "main", .bench = .{
206208
.duration = js_runner_duration,

src/testing.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ pub fn setup() !void {
379379
test_session = try test_browser.newSession();
380380
}
381381
pub fn shutdown() void {
382+
@import("root").v8_peak_memory = test_browser.env.isolate.getHeapStatistics().total_physical_size;
383+
@import("root").libdom_memory = @import("browser/mimalloc.zig").getRSS();
382384
test_browser.deinit();
383385
test_app.deinit();
384386
}

0 commit comments

Comments
 (0)