// benchmark std.json vs aether

const bigdata = @embedFile("10mb.json");
const std = @import("std");

fn aetherbenchAux(allocator: std.mem.Allocator) !void {
    const aether = @import("aether");

    const Tokenizer = aether.Tokenizer;
    const Language = aether.Language;

    var tokenizer: Tokenizer = try .init(allocator, bigdata);
    defer tokenizer.deinit(allocator);

    var self = Language.init;

    // we use a better storing strategy
    const idx: usize = try Language.parse(&self, allocator, &tokenizer);
    const root = self.index.get(idx);

    try std.testing.expect(root == .object);
}

fn stdjsonbenchAux(allocator: std.mem.Allocator) !void {
    // taken from https://github.com/ziglang/zig/blob/9d534790ebc869ec933e932abe4be8b9e3593bbc/lib/std/json/dynamic_test.zig#L20
    // and modified to use a 10mb file
    // 2025-06-05
    const json = std.json;
    const root = try json.parseFromSliceLeaky(json.Value, allocator, bigdata, .{});

    try std.testing.expect(root == .object);
}

pub fn aetherbench(allocator: std.mem.Allocator) void {
    aetherbenchAux(allocator) catch @panic("aetherbench failed");
}
pub fn stdjsonbench(allocator: std.mem.Allocator) void {
    stdjsonbenchAux(allocator) catch @panic("aetherbench failed");
}

pub fn main() !void {
    const zbench = @import("zbench");
    const stdout = std.io.getStdOut().writer();
    var gpa: std.heap.GeneralPurposeAllocator(.{}) = .{};
    const allocator = gpa.allocator();

    var bench = zbench.Benchmark.init(allocator, .{
        .iterations = 1,
    });
    defer bench.deinit();

    try bench.add("aether", &aetherbench, .{
        .track_allocations = true,
    });
    try bench.add("std.json", &stdjsonbench, .{
        .track_allocations = true,
    });

    try stdout.writeByte('\n');
    try zbench.prettyPrintHeader(stdout);
    var iter = try bench.iterator();
    while (try iter.next()) |step| switch (step) {
        .progress => |_| {},
        .result => |x| {
            defer x.deinit();
            try x.prettyPrint(allocator, stdout, true);
        },
    };
}
