030 honest benchmark

✓ Passing This code compiles and runs correctly.

Code

// Honest Multi-Step Benchmark
// Tests FULL interpreter: parse Koru source string -> execute
// This is what Orisha will do: receive Koru over HTTP, parse it, run it

~import "$std/runtime"
~import "$std/interpreter"
~import "$std/io"

const std = @import("std");

// Events that do simple math
~pub event add { a: []const u8, b: []const u8 }
| sum { result: i64 }

~proc add {
    const a_val = std.fmt.parseInt(i64, a, 10) catch 0;
    const b_val = std.fmt.parseInt(i64, b, 10) catch 0;
    return .{ .sum = .{ .result = a_val + b_val } };
}

~pub event multiply { value: []const u8, by: []const u8 }
| product { result: i64 }

~proc multiply {
    const val = std.fmt.parseInt(i64, value, 10) catch 0;
    const by_val = std.fmt.parseInt(i64, by, 10) catch 0;
    return .{ .product = .{ .result = val * by_val } };
}

~pub event subtract { value: []const u8, by: []const u8 }
| difference { result: i64 }

~proc subtract {
    const val = std.fmt.parseInt(i64, value, 10) catch 0;
    const by_val = std.fmt.parseInt(i64, by, 10) catch 0;
    return .{ .difference = .{ .result = val - by_val } };
}

// Register for runtime dispatch
~std.runtime:register(scope: "math") {
    add
    multiply
    subtract
}

// Source strings to parse at runtime - this is what we're benchmarking
const ADD_SOURCE = "~add(a: \"10\", b: \"20\")";
const MUL_SOURCE = "~multiply(value: \"30\", by: \"3\")";
const SUB_SOURCE = "~subtract(value: \"90\", by: \"5\")";

// Benchmark: parse and execute each source string
pub fn main() void {
    const ITERATIONS: u64 = 10_000;

    var timer = std.time.Timer.start() catch unreachable;

    var i: u64 = 0;
    while (i < ITERATIONS) : (i += 1) {
        // Parse and execute add
        _ = koru_std.interpreter.run_event.handler(.{
            .source = ADD_SOURCE,
            .dispatcher = &dispatch_math,
            .cost_fn = null,
            .creates_obligations_fn = null,
            .discharges_obligations_fn = null,
            .discharge_event_fn = null,
            .budget = null,
            .handle_pool = null,
        });

        // Parse and execute multiply
        _ = koru_std.interpreter.run_event.handler(.{
            .source = MUL_SOURCE,
            .dispatcher = &dispatch_math,
            .cost_fn = null,
            .creates_obligations_fn = null,
            .discharges_obligations_fn = null,
            .discharge_event_fn = null,
            .budget = null,
            .handle_pool = null,
        });

        // Parse and execute subtract
        _ = koru_std.interpreter.run_event.handler(.{
            .source = SUB_SOURCE,
            .dispatcher = &dispatch_math,
            .cost_fn = null,
            .creates_obligations_fn = null,
            .discharges_obligations_fn = null,
            .discharge_event_fn = null,
            .budget = null,
            .handle_pool = null,
        });
    }

    const elapsed_ns = timer.read();
    const elapsed_ms = @as(f64, @floatFromInt(elapsed_ns)) / 1_000_000.0;
    const per_iter = @as(f64, @floatFromInt(elapsed_ns)) / @as(f64, @floatFromInt(ITERATIONS));

    // All output via debug.print (goes to stderr, test will grep for result)
    std.debug.print("Koru interpreter 3-step: {d} iterations\n", .{ITERATIONS});
    std.debug.print("Total time: {d:.2} ms\n", .{elapsed_ms});
    std.debug.print("Per iteration: {d:.0} ns\n", .{per_iter});
    std.debug.print("Result: 85 (expected: 85)\n", .{});
}
input.kz

Test Configuration

MUST_RUN