006 coordinator timing

✓ Passing This code compiles and runs correctly.

Code

// Test 430_006: Timed Compiler Pipeline
//
// This proves you can WRAP the entire compilation pipeline with timing!
// We override compiler.coordinate to measure compilation time:
//
// 1. Record start time
// 2. Delegate to default pipeline
// 3. Record end time and report duration
// 4. Compiled program RUNS and prints timing to stderr
//
// This is COMPILE-TIME METAPROGRAMMING with actual instrumentation!

~import "$std/compiler"
~import "$std/io"

const std = @import("std");

// Override the compiler coordinator - TIME the pipeline!
~std.compiler:coordinate =
    timer_start()
    | started start_time |>
        std.compiler:coordinate.default(program_ast, allocator)
        | coordinated c |>
            timer_end(start_ns: start_time.ns)
            | elapsed e |> coordinated {
                ast: c.ast,
                code: c.code,
                metrics: std.fmt.allocPrint(allocator, "Pipeline completed in {d}ms", .{e.ms}) catch "timing error"
            }
        | error e |> error { message: e.message }

// Timer events - comptime because they're called from coordinate handler
~[comptime] event timer_start {}
| started { ns: i128 }

~[comptime] proc timer_start {
    return .{ .started = .{ .ns = std.time.nanoTimestamp() } };
}

~[comptime] event timer_end { start_ns: i128 }
| elapsed { ms: f64 }

~[comptime] proc timer_end {
    const end_ns = std.time.nanoTimestamp();
    const elapsed_ns = end_ns - start_ns;
    const ms: f64 = @as(f64, @floatFromInt(elapsed_ns)) / 1_000_000.0;
    std.debug.print("⏱️  Compilation took {d:.2}ms\n", .{ms});
    return .{ .elapsed = .{ .ms = ms } };
}

// The actual program - this compiles and runs!
~std.io:print.ln("hello from timed compilation!")
input.kz

Expected Output

hello from timed compilation!

Test Configuration

MUST_RUN

Post-validation Script:

#!/bin/bash
# Post-validation: Verify timing instrumentation worked
#
# Checks that:
# 1. Timer output appears in backend.err
# 2. Timing value is reasonable (> 0ms, < 60000ms)

set -e

if [ ! -f "backend.err" ]; then
    echo "❌ FAIL: No backend.err file found"
    exit 1
fi

# Extract the timing line
TIMING_LINE=$(grep "⏱️  Compilation took" backend.err || true)

if [ -z "$TIMING_LINE" ]; then
    echo "❌ FAIL: No timing output found in backend.err"
    echo "   Expected: ⏱️  Compilation took X.XXms"
    exit 1
fi

# Extract the milliseconds value
MS=$(echo "$TIMING_LINE" | sed -n 's/.*took \([0-9.]*\)ms.*/\1/p')

if [ -z "$MS" ]; then
    echo "❌ FAIL: Could not parse timing value from: $TIMING_LINE"
    exit 1
fi

# Validate timing is reasonable (using awk for float comparison)
VALID=$(awk -v ms="$MS" 'BEGIN { print (ms > 0 && ms < 60000) ? "yes" : "no" }')

if [ "$VALID" != "yes" ]; then
    echo "❌ FAIL: Timing value out of range: ${MS}ms"
    echo "   Expected: 0 < ms < 60000"
    exit 1
fi

echo "✅ Timing instrumentation working: ${MS}ms"
exit 0