Skip to content

Commit c08ec68

Browse files
authored
Merge pull request #8 from ncode/juliano/moar_perf
feat(benchmark): include token and event counts in allocation reports
2 parents d21bd88 + b1049a8 commit c08ec68

1 file changed

Lines changed: 44 additions & 8 deletions

File tree

‎tools/benchmark.zig‎

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ const AllocationRun = struct {
7171
retained_bytes: usize,
7272
};
7373

74+
const CountMetrics = struct {
75+
token_count: usize,
76+
event_count: usize,
77+
};
78+
7479
const conformance_subset =
7580
\\---
7681
\\foo:
@@ -95,6 +100,7 @@ pub fn main(init: std.process.Init) !void {
95100
defer deinitFixtures(allocator, fixtures);
96101

97102
try stdout.print("yaml benchmark\n", .{});
103+
try stdout.print("types scanner_token_size={} parser_event_size={}\n", .{ @sizeOf(yaml_internal.scanner.Token), @sizeOf(yaml_internal.parser.Event) });
98104
try stdout.print("format: input bytes api iterations ns_per_op bytes_per_sec checksum\n", .{});
99105

100106
for (fixtures) |fixture| {
@@ -110,15 +116,16 @@ pub fn main(init: std.process.Init) !void {
110116

111117
try stdout.print("allocation counts\n", .{});
112118
for (fixtures) |fixture| {
113-
try reportAllocations(allocator, stdout, fixture, .scanner);
114-
try reportAllocations(allocator, stdout, fixture, .parser_events);
115-
try reportAllocations(allocator, stdout, fixture, .composer_events);
116-
try reportAllocations(allocator, stdout, fixture, .loader_events);
119+
const counts = try countTokensAndEvents(allocator, fixture.input);
120+
try reportAllocations(allocator, stdout, fixture, .scanner, counts);
121+
try reportAllocations(allocator, stdout, fixture, .parser_events, counts);
122+
try reportAllocations(allocator, stdout, fixture, .composer_events, counts);
123+
try reportAllocations(allocator, stdout, fixture, .loader_events, counts);
117124
if (fixture.single_document) {
118-
try reportAllocations(allocator, stdout, fixture, .load_default);
119-
try reportAllocations(allocator, stdout, fixture, .load_failsafe_allow);
125+
try reportAllocations(allocator, stdout, fixture, .load_default, counts);
126+
try reportAllocations(allocator, stdout, fixture, .load_failsafe_allow, counts);
120127
} else {
121-
try reportAllocations(allocator, stdout, fixture, .load_stream_default);
128+
try reportAllocations(allocator, stdout, fixture, .load_stream_default, counts);
122129
}
123130
}
124131

@@ -258,6 +265,19 @@ test "public load allocation metrics report retained and peak live bytes" {
258265
try std.testing.expectEqual(metrics.allocated_bytes, metrics.freed_bytes);
259266
}
260267

268+
test "benchmark count metrics include scanner tokens and parser events" {
269+
const counts = try countTokensAndEvents(std.testing.allocator,
270+
\\name: yaml
271+
\\items:
272+
\\ - one
273+
\\ - two
274+
\\
275+
);
276+
277+
try std.testing.expect(counts.token_count > 0);
278+
try std.testing.expect(counts.event_count > 0);
279+
}
280+
261281
fn expectFixture(fixtures: []const Fixture, name: []const u8) !void {
262282
for (fixtures) |fixture| {
263283
if (std.mem.eql(u8, fixture.name, name)) return;
@@ -384,14 +404,17 @@ fn reportAllocations(
384404
stdout: *Io.Writer,
385405
fixture: Fixture,
386406
allocation_path: AllocationPath,
407+
counts: CountMetrics,
387408
) !void {
388409
const metrics = try measureAllocations(allocator, fixture.input, allocation_path);
389410
try stdout.print(
390-
"allocs input={s} bytes={} api={s} allocations={} frees={} allocated_bytes={} freed_bytes={} retained_bytes={} peak_live_bytes={} checksum={}\n",
411+
"allocs input={s} bytes={} api={s} token_count={} event_count={} allocations={} frees={} allocated_bytes={} freed_bytes={} retained_bytes={} peak_live_bytes={} checksum={}\n",
391412
.{
392413
fixture.name,
393414
fixture.input.len,
394415
allocation_path.label(),
416+
counts.token_count,
417+
counts.event_count,
395418
metrics.allocations,
396419
metrics.frees,
397420
metrics.allocated_bytes,
@@ -517,6 +540,19 @@ fn measurePublicLoadAllocations(
517540
};
518541
}
519542

543+
fn countTokensAndEvents(allocator: std.mem.Allocator, input: []const u8) !CountMetrics {
544+
var token_stream = try yaml_internal.scanner.scan(allocator, input);
545+
defer token_stream.deinit();
546+
547+
var event_stream = try yaml_internal.parseTokens(allocator, token_stream.tokens);
548+
defer event_stream.deinit();
549+
550+
return .{
551+
.token_count = token_stream.tokens.len,
552+
.event_count = event_stream.events.len,
553+
};
554+
}
555+
520556
fn parseEvents(allocator: std.mem.Allocator, input: []const u8) !yaml_internal.parser.EventStream {
521557
var token_stream = try yaml_internal.scanner.scan(allocator, input);
522558
defer token_stream.deinit();

0 commit comments

Comments
 (0)