const std = @import("std"); const abi = @import("../abi.zig"); const config = @import("../config.zig"); const http_state = @import("../protocol/http_state.zig"); const runner_log = @import("log.zig"); const runner_metrics = @import("metrics.zig"); const template = @import("../template.zig"); const workflow_iteration = @import("../workflow/iteration.zig"); const workflow_host = @import("../workflow/host.zig"); const workflow_recovery = @import("../workflow/recovery.zig"); const workflow_vu = @import("../workflow/vu.zig"); const CookieJar = http_state.CookieJar; const OwnedHeader = http_state.OwnedHeader; const clearOwnedHeaders = http_state.clearOwnedHeaders; const WorkflowVu = workflow_vu.WorkflowVu; const LogEntry = struct { ts_unix_nanos: u64, level: []const u8, message: []const u8, component: ?[]const u8 = null, source: ?[]const u8 = null, event: ?[]const u8 = null, phase: ?[]const u8 = null, vu_id: ?u32 = null, iteration_no: ?u64 = null, }; fn parseLogEntry(bytes: []const u8) !std.json.Parsed(LogEntry) { return std.json.parseFromSlice(LogEntry, std.testing.allocator, bytes, .{}); } fn exactPercentile(values: []const u64, percent: usize) !u64 { const sorted = try std.testing.allocator.dupe(u64, values); defer std.testing.allocator.free(sorted); std.sort.block(u64, sorted, {}, std.sort.asc(u64)); if (sorted.len == 0) return 0; const index = ((sorted.len - 1) * percent) / 100; return sorted[index]; } fn expectHistogramPercentileBounded(approx: u64, exact: u64) !void { try std.testing.expect(approx >= exact); const exact_f64: f64 = @floatFromInt(exact); const capped = @as(u64, @intFromFloat(@ceil(exact_f64 * 1.02))) + 1; try std.testing.expect(approx <= @max(exact + 1, capped)); } test "cookie jar serializes and parses cookie headers" { var jar = CookieJar.init(std.testing.allocator); defer jar.deinit(); try jar.set("sid", "abc"); try jar.set("theme", "light"); const header = try jar.headerValue(std.testing.allocator); defer if (header) |value| std.testing.allocator.free(value); try std.testing.expect(header != null); try std.testing.expect(std.mem.indexOf(u8, header.?, "sid=abc") != null); try std.testing.expect(std.mem.indexOf(u8, header.?, "theme=light") != null); var headers = try std.testing.allocator.alloc(OwnedHeader, 2); defer clearOwnedHeaders(std.testing.allocator, headers); headers[0] = .{ .name = try std.testing.allocator.dupe(u8, "Set-Cookie"), .value = try std.testing.allocator.dupe(u8, "sid=xyz; Path=/"), }; headers[1] = .{ .name = try std.testing.allocator.dupe(u8, "set-cookie"), .value = try std.testing.allocator.dupe(u8, "theme=dark; HttpOnly"), }; jar.clear(); try jar.updateFromHeaders(headers); try std.testing.expectEqualStrings("xyz", jar.cookies.get("sid").?); try std.testing.expectEqualStrings("dark", jar.cookies.get("theme").?); } test "structured logger emits JSON with context fields" { var buffer: [512]u8 = undefined; var stream = std.io.fixedBufferStream(&buffer); var writer = stream.writer(); var logger = runner_log.Logger.init(std.testing.allocator, runner_log.Sink.fromWriterPtr(&writer)); try logger.log(.info, "progress update", .{ .component = "runner", .source = .host, .event = "progress", .vu_id = 3, .iteration_no = 9, }); var parsed = try parseLogEntry(stream.getWritten()); defer parsed.deinit(); try std.testing.expect(parsed.value.ts_unix_nanos != 0); try std.testing.expectEqualStrings("info", parsed.value.level); try std.testing.expectEqualStrings("progress update", parsed.value.message); try std.testing.expectEqualStrings("runner", parsed.value.component.?); try std.testing.expectEqualStrings("host", parsed.value.source.?); try std.testing.expectEqualStrings("progress", parsed.value.event.?); try std.testing.expectEqual(@as(u32, 3), parsed.value.vu_id.?); try std.testing.expectEqual(@as(u64, 9), parsed.value.iteration_no.?); } test "structured logger filters entries below the configured level" { var buffer: [256]u8 = undefined; var stream = std.io.fixedBufferStream(&buffer); var writer = stream.writer(); var logger = runner_log.Logger.initWithLevel(std.testing.allocator, runner_log.Sink.fromWriterPtr(&writer), .warn); try logger.log(.info, "ignored", .{ .component = "runner" }); try std.testing.expectEqualStrings("", stream.getWritten()); } test "workflow host_log emits guest structured logs" { var buffer: [512]u8 = undefined; var stream = std.io.fixedBufferStream(&buffer); var writer = stream.writer(); var logger = runner_log.Logger.init(std.testing.allocator, runner_log.Sink.fromWriterPtr(&writer)); var host = workflow_host.WorkflowHostState{ .allocator = std.testing.allocator, .store = template.VarStore.init(std.testing.allocator), .cookie_jar = CookieJar.init(std.testing.allocator), .prng = std.Random.DefaultPrng.init(7), .logger = &logger, .log_phase = "iteration", .vu_id = 7, .iteration_no = 11, }; defer host.deinit(); const message = "guest warning"; try std.testing.expectEqual(@as(i32, 0), workflow_host.workflowHostLog(&host, 2, message.ptr, message.len)); var parsed = try parseLogEntry(stream.getWritten()); defer parsed.deinit(); try std.testing.expectEqualStrings("warn", parsed.value.level); try std.testing.expectEqualStrings("guest warning", parsed.value.message); try std.testing.expectEqualStrings("workflow", parsed.value.component.?); try std.testing.expectEqualStrings("guest", parsed.value.source.?); try std.testing.expectEqualStrings("guest_log", parsed.value.event.?); try std.testing.expectEqualStrings("iteration", parsed.value.phase.?); try std.testing.expectEqual(@as(u32, 7), parsed.value.vu_id.?); try std.testing.expectEqual(@as(u64, 11), parsed.value.iteration_no.?); } test "window summary carries transport error counts" { var window = runner_metrics.MetricsWindow{}; defer window.deinit(std.testing.allocator); try window.appendRequest(std.testing.allocator, .{ .duration_nanos = 10, .bytes_sent = 1, .bytes_received = 2, .success = false, .transport_code = .dns, }); try window.appendRequest(std.testing.allocator, .{ .duration_nanos = 20, .bytes_sent = 3, .bytes_received = 4, .success = false, .transport_code = .tls, }); try window.appendRequest(std.testing.allocator, .{ .duration_nanos = 25, .bytes_sent = 4, .bytes_received = 5, .success = false, .transport_code = .timeout, .timeout_phase = .connect, }); try window.appendRequest(std.testing.allocator, .{ .duration_nanos = 26, .bytes_sent = 4, .bytes_received = 5, .success = false, .transport_code = .timeout, .timeout_phase = .io, }); try window.appendRequest(std.testing.allocator, .{ .duration_nanos = 30, .bytes_sent = 5, .bytes_received = 6, .success = true, .transport_code = .none, }); const summary = try runner_metrics.summarizeWindow(std.testing.allocator, &window); try std.testing.expectEqual(@as(u64, 4), summary.transport_errors_total); try std.testing.expectEqual(@as(u64, 1), summary.transport_errors_dns); try std.testing.expectEqual(@as(u64, 1), summary.transport_errors_tls); try std.testing.expectEqual(@as(u64, 2), summary.transport_errors_timeout); try std.testing.expectEqual(@as(u64, 1), summary.transport_errors_connect_timeout); try std.testing.expectEqual(@as(u64, 1), summary.transport_errors_io_timeout); try std.testing.expectEqual(@as(u64, 0), summary.transport_errors_connect); } test "duration histogram percentiles stay within the documented error bound" { const durations = [_]u64{ 1_000, 2_000, 5_000, 10_000, 20_000, 50_000, 100_000, 200_000, 500_000, 1_000_000, 2_000_000, 5_000_000, 10_000_000, 20_000_000, 50_000_000, }; var window = runner_metrics.MetricsWindow{}; defer window.deinit(std.testing.allocator); for (durations) |duration| { try window.appendRequest(std.testing.allocator, .{ .duration_nanos = duration, .bytes_sent = 0, .bytes_received = 0, .success = true, }); } var summary = try runner_metrics.summarizeWindow(std.testing.allocator, &window); defer summary.deinit(std.testing.allocator); try expectHistogramPercentileBounded(summary.duration_p50_nanos, try exactPercentile(&durations, 50)); try expectHistogramPercentileBounded(summary.duration_p90_nanos, try exactPercentile(&durations, 90)); try expectHistogramPercentileBounded(summary.duration_p99_nanos, try exactPercentile(&durations, 99)); } test "window summary carries grpc status and deadline counts" { var window = runner_metrics.MetricsWindow{}; defer window.deinit(std.testing.allocator); try window.appendRequest(std.testing.allocator, .{ .duration_nanos = 10, .bytes_sent = 1, .bytes_received = 2, .success = true, .grpc_status = 0, .request_name = "health", }); try window.appendRequest(std.testing.allocator, .{ .duration_nanos = 20, .bytes_sent = 1, .bytes_received = 0, .success = false, .grpc_status = 4, .grpc_deadline_exceeded = true, .request_name = "health", }); var summary = try runner_metrics.summarizeWindow(std.testing.allocator, &window); defer summary.deinit(std.testing.allocator); try std.testing.expectEqual(@as(u64, 1), summary.grpc_deadline_exceeded_total); try std.testing.expectEqual(@as(usize, 2), summary.grpc_status_counts.len); try std.testing.expectEqual(@as(u8, 0), summary.grpc_status_counts[0].status); try std.testing.expectEqual(@as(u64, 1), summary.grpc_status_counts[0].count); try std.testing.expectEqual(@as(u8, 4), summary.grpc_status_counts[1].status); try std.testing.expectEqual(@as(u64, 1), summary.grpc_status_counts[1].count); try std.testing.expectEqual(@as(usize, 1), summary.request_summaries.len); try std.testing.expectEqual(@as(u64, 1), summary.request_summaries[0].summary.grpc_deadline_exceeded_total); } test "window summary carries built-in check totals" { var window = runner_metrics.MetricsWindow{}; defer window.deinit(std.testing.allocator); try window.appendRequest(std.testing.allocator, .{ .duration_nanos = 10, .bytes_sent = 0, .bytes_received = 0, .success = true, .checks_passed = 2, .request_name = "health", }); try window.appendRequest(std.testing.allocator, .{ .duration_nanos = 20, .bytes_sent = 0, .bytes_received = 0, .success = false, .checks_failed = 1, .request_name = "health", }); var summary = try runner_metrics.summarizeWindow(std.testing.allocator, &window); defer summary.deinit(std.testing.allocator); try std.testing.expectEqual(@as(u64, 2), summary.checks_passed_total); try std.testing.expectEqual(@as(u64, 1), summary.checks_failed_total); try std.testing.expectApproxEqAbs(@as(f64, 2.0 / 3.0), summary.check_pass_rate, 0.0001); try std.testing.expectEqual(@as(u64, 1), summary.request_summaries[0].summary.checks_failed_total); } test "workflow host setup mode only accepts setup namespace writes" { var host = workflow_host.WorkflowHostState{ .allocator = std.testing.allocator, .store = template.VarStore.init(std.testing.allocator), .cookie_jar = CookieJar.init(std.testing.allocator), .prng = std.Random.DefaultPrng.init(1), .set_var_mode = .setup_only, }; defer host.deinit(); try std.testing.expectEqual(@as(i32, 0), workflow_host.workflowHostSetVar(&host, "setup.KEY".ptr, 9, "value".ptr, 5)); try std.testing.expectEqual(@as(i32, -1), workflow_host.workflowHostSetVar(&host, "iter.KEY".ptr, 8, "value".ptr, 5)); try std.testing.expectEqualStrings("value", host.store.get("setup.KEY").?); } test "abort_on_fail trips on measured threshold failure" { var aggregator = runner_metrics.MetricsAggregator{ .allocator = std.testing.allocator }; defer aggregator.deinit(); try aggregator.recordRequest(10, 0, .{ .duration_nanos = 10, .bytes_sent = 0, .bytes_received = 0, .success = false, .transport_code = .none, }); aggregator.recordIteration(10, 0, false); const scenario = config.ScenarioConfig{ .arena = undefined, .scenario = .{ .name = "x", .protocol = .http, .target = "http://example" }, .execution = .{ .mode = .constant_vus, .duration = .{ .nanos = 1 }, .vus = 1 }, .setup = .{ .requests = &.{} }, .http = .{ .defaults = .{}, .requests = &.{} }, .thresholds = &.{ .{ .metric = .http_error_rate, .op = .lte, .value = .{ .ratio = 0.0 }, .abort_on_fail = true, }, }, }; try std.testing.expect(try aggregator.shouldAbortOnFail(&scenario)); } test "workflow action validator rejects inconsistent flags" { var action = abi.HttpRequestActionV1{ .request_id = 1, }; try std.testing.expect(workflow_iteration.actionIsValid(&action, 0)); action.body_len = 10; try std.testing.expect(!workflow_iteration.actionIsValid(&action, 0)); action.flags = @intFromEnum(abi.RequestActionFlag.has_body); action.body_len = 4; try std.testing.expect(!workflow_iteration.actionIsValid(&action, 3)); } test "workflow timing classifier separates cooperative and hard timeouts" { try std.testing.expectEqual( workflow_recovery.ScriptTimeoutOutcome.none, workflow_recovery.classifyScriptCallTiming(50 * std.time.ns_per_ms, config.Duration.fromMillis(100), false, false), ); try std.testing.expectEqual( workflow_recovery.ScriptTimeoutOutcome.cooperative, workflow_recovery.classifyScriptCallTiming(150 * std.time.ns_per_ms, config.Duration.fromMillis(100), true, false), ); try std.testing.expectEqual( workflow_recovery.ScriptTimeoutOutcome.hard, workflow_recovery.classifyScriptCallTiming(150 * std.time.ns_per_ms, config.Duration.fromMillis(100), false, false), ); try std.testing.expectEqual( workflow_recovery.ScriptTimeoutOutcome.none, workflow_recovery.classifyScriptCallTiming(150 * std.time.ns_per_ms, config.Duration.fromMillis(100), false, true), ); } test "recreate policy marks rapid repeats as unhealthy" { var vu = WorkflowVu{ .host = undefined, .instance = undefined, .vu_id = 1, }; const base: i128 = 1_000_000_000; const policy = config.RecoveryPolicy{}; try std.testing.expect(!workflow_vu.noteRecreated(&vu, base, policy)); try std.testing.expect(!workflow_vu.noteRecreated(&vu, base + 1, policy)); try std.testing.expect(!workflow_vu.noteRecreated(&vu, base + 2, policy)); try std.testing.expect(workflow_vu.noteRecreated(&vu, base + 3, policy)); } test "recreate policy respects custom window and limit" { var vu = WorkflowVu{ .host = undefined, .instance = undefined, .vu_id = 1, }; const policy = config.RecoveryPolicy{ .window = config.Duration.fromSeconds(1), .max_recreates = 1, }; const base: i128 = 10 * std.time.ns_per_s; try std.testing.expect(!workflow_vu.noteRecreated(&vu, base, policy)); try std.testing.expect(workflow_vu.noteRecreated(&vu, base + std.time.ns_per_ms, policy)); try std.testing.expect(!workflow_vu.noteRecreated(&vu, base + 2 * std.time.ns_per_s, policy)); } test "metrics window tracks unhealthy and spare counters" { var window = runner_metrics.MetricsWindow{}; defer window.deinit(std.testing.allocator); window.recordScriptTimeout(); window.recordHardTimeout(); window.recordPoisonedWorker(); window.recordUnhealthyVu(); window.recordSpareVuActivated(); window.recordSpareVuActivated(); const summary = try runner_metrics.summarizeWindow(std.testing.allocator, &window); try std.testing.expectEqual(@as(u64, 1), summary.script_timeouts_total); try std.testing.expectEqual(@as(u64, 1), summary.hard_timeouts_total); try std.testing.expectEqual(@as(u64, 1), summary.poisoned_workers_total); try std.testing.expectEqual(@as(u64, 1), summary.unhealthy_vus_total); try std.testing.expectEqual(@as(u64, 2), summary.spare_vus_activated_total); } test "window summary carries scheduler lag metrics" { var aggregator = runner_metrics.MetricsAggregator{ .allocator = std.testing.allocator }; defer aggregator.deinit(); aggregator.recordSchedulerLag(10, 0, 1_000_000); aggregator.recordSchedulerLag(20, 0, 3_000_000); var summary = try aggregator.finalize(std.testing.allocator, &.{ .arena = undefined, .scenario = .{ .name = "x", .protocol = .http, .target = "http://example" }, .execution = .{ .mode = .constant_vus, .duration = .{ .nanos = 1 }, .vus = 1 }, .setup = .{ .requests = &.{} }, .http = .{ .defaults = .{}, .requests = &.{} }, .thresholds = &.{}, }, false); defer summary.deinit(); try std.testing.expectEqual(@as(u64, 2_000_000), summary.output.measured.scheduler_lag_avg_nanos); try std.testing.expectEqual(@as(u64, 3_000_000), summary.output.measured.scheduler_lag_max_nanos); } test "abort_on_fail can use scheduler lag thresholds" { var aggregator = runner_metrics.MetricsAggregator{ .allocator = std.testing.allocator }; defer aggregator.deinit(); aggregator.recordSchedulerLag(10, 0, 5_000_000); const scenario = config.ScenarioConfig{ .arena = undefined, .scenario = .{ .name = "x", .protocol = .http, .target = "http://example" }, .execution = .{ .mode = .constant_vus, .duration = .{ .nanos = 1 }, .vus = 1 }, .setup = .{ .requests = &.{} }, .http = .{ .defaults = .{}, .requests = &.{} }, .thresholds = &.{ .{ .metric = .scheduler_lag_max, .op = .lte, .value = .{ .duration = .{ .nanos = 1_000_000 } }, .abort_on_fail = true, }, }, }; try std.testing.expect(try aggregator.shouldAbortOnFail(&scenario)); } test "abort_on_fail can use request scoped thresholds" { var aggregator = runner_metrics.MetricsAggregator{ .allocator = std.testing.allocator }; defer aggregator.deinit(); try aggregator.recordRequest(10, 0, .{ .duration_nanos = 1_000_000, .bytes_sent = 0, .bytes_received = 0, .success = false, .transport_code = .protocol, .request_name = "login", }); try aggregator.recordRequest(20, 0, .{ .duration_nanos = 1_000_000, .bytes_sent = 0, .bytes_received = 0, .success = true, .transport_code = .none, .request_name = "profile", }); const scenario = config.ScenarioConfig{ .arena = undefined, .scenario = .{ .name = "x", .protocol = .http, .target = "http://example" }, .execution = .{ .mode = .constant_vus, .duration = .{ .nanos = 1 }, .vus = 1 }, .setup = .{ .requests = &.{} }, .http = .{ .defaults = .{}, .requests = &.{ .{ .id = 1, .name = "login", .method = .GET, .path = "/login", .headers = &.{} }, .{ .id = 2, .name = "profile", .method = .GET, .path = "/profile", .headers = &.{} }, } }, .thresholds = &.{ .{ .metric = .http_error_rate, .scope = .{ .request = "login" }, .op = .lte, .value = .{ .ratio = 0.0 }, .abort_on_fail = true, }, }, }; try std.testing.expect(try aggregator.shouldAbortOnFail(&scenario)); } test "abort_on_fail can use built-in check thresholds" { var aggregator = runner_metrics.MetricsAggregator{ .allocator = std.testing.allocator }; defer aggregator.deinit(); try aggregator.recordRequest(10, 0, .{ .duration_nanos = 1, .bytes_sent = 0, .bytes_received = 0, .success = false, .checks_failed = 1, .request_name = "health", }); const scenario = config.ScenarioConfig{ .arena = undefined, .scenario = .{ .name = "x", .protocol = .http, .target = "http://example" }, .recovery = .{}, .execution = .{ .mode = .constant_vus, .duration = .{ .nanos = 1 }, .vus = 1 }, .setup = .{ .requests = &.{} }, .http = .{ .defaults = .{}, .requests = &.{ .{ .id = 1, .name = "health", .method = .GET, .path = "/health", .headers = &.{} }, } }, .thresholds = &.{ .{ .metric = .check_pass_rate, .op = .gte, .value = .{ .ratio = 1.0 }, .abort_on_fail = true, }, }, }; try std.testing.expect(try aggregator.shouldAbortOnFail(&scenario)); } test "abort_on_fail supports all-of threshold expressions" { var aggregator = runner_metrics.MetricsAggregator{ .allocator = std.testing.allocator }; defer aggregator.deinit(); try aggregator.recordRequest(10, 0, .{ .duration_nanos = 2_000_000, .bytes_sent = 0, .bytes_received = 0, .success = false, .checks_failed = 1, .request_name = "health", }); const scenario = config.ScenarioConfig{ .arena = undefined, .scenario = .{ .name = "x", .protocol = .http, .target = "http://example" }, .recovery = .{}, .execution = .{ .mode = .constant_vus, .duration = .{ .nanos = 1 }, .vus = 1 }, .setup = .{ .requests = &.{} }, .http = .{ .defaults = .{}, .requests = &.{ .{ .id = 1, .name = "health", .method = .GET, .path = "/health", .headers = &.{} }, } }, .thresholds = &.{ .{ .metric = .check_pass_rate, .op = .gte, .value = .{ .ratio = 1.0 }, .scope = .{ .request = "health" }, .abort_on_fail = true, .expression_mode = .all_of, .expression_clauses = &.{ .{ .metric = .check_pass_rate, .op = .gte, .value = .{ .ratio = 1.0 } }, .{ .metric = .http_request_duration_p99, .op = .lte, .value = .{ .duration = .{ .nanos = 1_000_000 } } }, }, }, }, }; try std.testing.expect(try aggregator.shouldAbortOnFail(&scenario)); } test "abort_on_fail supports any-of threshold expressions" { var aggregator = runner_metrics.MetricsAggregator{ .allocator = std.testing.allocator }; defer aggregator.deinit(); try aggregator.recordRequest(10, 0, .{ .duration_nanos = 500_000, .bytes_sent = 0, .bytes_received = 0, .success = false, .checks_failed = 1, .request_name = "health", }); const scenario = config.ScenarioConfig{ .arena = undefined, .scenario = .{ .name = "x", .protocol = .http, .target = "http://example" }, .recovery = .{}, .execution = .{ .mode = .constant_vus, .duration = .{ .nanos = 1 }, .vus = 1 }, .setup = .{ .requests = &.{} }, .http = .{ .defaults = .{}, .requests = &.{ .{ .id = 1, .name = "health", .method = .GET, .path = "/health", .headers = &.{} }, } }, .thresholds = &.{ .{ .metric = .check_pass_rate, .op = .gte, .value = .{ .ratio = 1.0 }, .scope = .{ .request = "health" }, .abort_on_fail = true, .expression_mode = .any_of, .expression_clauses = &.{ .{ .metric = .check_pass_rate, .op = .gte, .value = .{ .ratio = 1.0 } }, .{ .metric = .http_request_duration_p99, .op = .lte, .value = .{ .duration = .{ .nanos = 1_000_000 } } }, }, }, }, }; try std.testing.expect(!try aggregator.shouldAbortOnFail(&scenario)); } test "window summary includes sorted request summaries" { var aggregator = runner_metrics.MetricsAggregator{ .allocator = std.testing.allocator }; defer aggregator.deinit(); try aggregator.recordRequest(10, 0, .{ .duration_nanos = 1_000_000, .bytes_sent = 0, .bytes_received = 0, .success = true, .request_name = "zeta", }); try aggregator.recordRequest(20, 0, .{ .duration_nanos = 2_000_000, .bytes_sent = 0, .bytes_received = 0, .success = false, .transport_code = .timeout, .timeout_phase = .io, .request_name = "alpha", }); var summary = try aggregator.finalize(std.testing.allocator, &.{ .arena = undefined, .scenario = .{ .name = "x", .protocol = .http, .target = "http://example" }, .execution = .{ .mode = .constant_vus, .duration = .{ .nanos = 1 }, .vus = 1 }, .setup = .{ .requests = &.{} }, .http = .{ .defaults = .{}, .requests = &.{} }, .thresholds = &.{}, }, false); defer summary.deinit(); try std.testing.expectEqual(@as(usize, 2), summary.output.measured.request_summaries.len); try std.testing.expectEqualStrings("alpha", summary.output.measured.request_summaries[0].name); try std.testing.expectEqual(@as(u64, 1), summary.output.measured.request_summaries[0].summary.transport_errors_timeout); try std.testing.expectEqual(@as(u64, 0), summary.output.measured.request_summaries[0].summary.transport_errors_connect_timeout); try std.testing.expectEqual(@as(u64, 1), summary.output.measured.request_summaries[0].summary.transport_errors_io_timeout); try std.testing.expectEqual(@as(f64, 1), summary.output.measured.request_summaries[0].summary.transport_timeout_rate); try std.testing.expectEqual(@as(f64, 0), summary.output.measured.request_summaries[0].summary.transport_connect_timeout_rate); try std.testing.expectEqual(@as(f64, 1), summary.output.measured.request_summaries[0].summary.transport_io_timeout_rate); try std.testing.expectEqualStrings("zeta", summary.output.measured.request_summaries[1].name); } test "metrics window merge combines counters and request summaries" { var left = runner_metrics.MetricsWindow{}; defer left.deinit(std.testing.allocator); try left.appendRequest(std.testing.allocator, .{ .duration_nanos = 1_000_000, .bytes_sent = 10, .bytes_received = 20, .success = true, .checks_passed = 1, .request_name = "alpha", }); left.recordIteration(true); left.recordSchedulerLag(1_000); var right = runner_metrics.MetricsWindow{}; defer right.deinit(std.testing.allocator); try right.appendRequest(std.testing.allocator, .{ .duration_nanos = 2_000_000, .bytes_sent = 30, .bytes_received = 40, .success = false, .checks_failed = 2, .transport_code = .timeout, .timeout_phase = .io, .grpc_status = 4, .grpc_deadline_exceeded = true, .request_name = "alpha", }); try right.appendRequest(std.testing.allocator, .{ .duration_nanos = 3_000_000, .bytes_sent = 50, .bytes_received = 60, .success = false, .transport_code = .protocol, .request_name = "beta", }); right.recordIteration(false); right.addDropped(3); right.recordSchedulerLag(3_000); right.recordScriptTimeout(); try left.mergeFrom(std.testing.allocator, &right); var summary = try runner_metrics.summarizeWindow(std.testing.allocator, &left); defer summary.deinit(std.testing.allocator); try std.testing.expectEqual(@as(u64, 3), summary.requests_total); try std.testing.expectEqual(@as(u64, 2), summary.requests_failed); try std.testing.expectEqual(@as(u64, 2), summary.iterations_total); try std.testing.expectEqual(@as(u64, 1), summary.iterations_failed); try std.testing.expectEqual(@as(u64, 3), summary.dropped_iterations); try std.testing.expectEqual(@as(u64, 90), summary.bytes_sent); try std.testing.expectEqual(@as(u64, 120), summary.bytes_received); try std.testing.expectEqual(@as(u64, 2), summary.request_summaries.len); try std.testing.expectEqualStrings("alpha", summary.request_summaries[0].name); try std.testing.expectEqual(@as(u64, 2), summary.request_summaries[0].summary.requests_total); try std.testing.expectEqual(@as(u64, 1), summary.request_summaries[0].summary.requests_failed); try std.testing.expectEqual(@as(u64, 1), summary.request_summaries[0].summary.transport_errors_timeout); try std.testing.expectEqual(@as(u64, 1), summary.request_summaries[0].summary.transport_errors_io_timeout); try std.testing.expectEqual(@as(u64, 1), summary.request_summaries[0].summary.checks_passed_total); try std.testing.expectEqual(@as(u64, 2), summary.request_summaries[0].summary.checks_failed_total); try std.testing.expectEqualStrings("beta", summary.request_summaries[1].name); try std.testing.expectEqual(@as(u64, 1), summary.request_summaries[1].summary.transport_errors_total); try std.testing.expectEqual(@as(u64, 1), summary.transport_errors_timeout); try std.testing.expectEqual(@as(u64, 1), summary.transport_errors_protocol); try std.testing.expectEqual(@as(u64, 1), summary.grpc_deadline_exceeded_total); try std.testing.expectEqual(@as(usize, 1), summary.grpc_status_counts.len); try std.testing.expectEqual(@as(u8, 4), summary.grpc_status_counts[0].status); try std.testing.expectEqual(@as(u64, 1), summary.grpc_status_counts[0].count); try std.testing.expectEqual(@as(u64, 1), summary.script_timeouts_total); try std.testing.expectEqual(@as(u64, 2_000), summary.scheduler_lag_avg_nanos); try std.testing.expectEqual(@as(u64, 3_000), summary.scheduler_lag_max_nanos); try std.testing.expectEqual(@as(u64, 1_000_000), summary.duration_min_nanos); try std.testing.expectEqual(@as(u64, 3_000_000), summary.duration_max_nanos); } test "metrics aggregator snapshot round-trips through merge" { var original = runner_metrics.MetricsAggregator{ .allocator = std.testing.allocator }; defer original.deinit(); try original.recordRequest(10, 0, .{ .duration_nanos = 1_500_000, .bytes_sent = 12, .bytes_received = 34, .success = false, .transport_code = .timeout, .timeout_phase = .connect, .request_name = "login", }); original.recordIteration(10, 0, false); original.recordScriptError(10, 0); var snapshot = try original.snapshot(std.testing.allocator, false); defer snapshot.deinit(std.testing.allocator); var restored = runner_metrics.MetricsAggregator{ .allocator = std.testing.allocator }; defer restored.deinit(); try restored.mergeSnapshot(&snapshot); var summary = try restored.finalize(std.testing.allocator, &.{ .arena = undefined, .scenario = .{ .name = "x", .protocol = .http, .target = "http://example" }, .execution = .{ .mode = .constant_vus, .duration = .{ .nanos = 1 }, .vus = 1 }, .setup = .{ .requests = &.{} }, .http = .{ .defaults = .{}, .requests = &.{ .{ .id = 1, .name = "login", .method = .GET, .path = "/login", .headers = &.{} }, } }, .thresholds = &.{}, }, false); defer summary.deinit(); try std.testing.expectEqual(@as(u64, 1), summary.output.measured.requests_total); try std.testing.expectEqual(@as(u64, 1), summary.output.measured.iterations_failed); try std.testing.expectEqual(@as(u64, 1), summary.output.measured.transport_errors_timeout); try std.testing.expectEqual(@as(u64, 1), summary.output.measured.transport_errors_connect_timeout); try std.testing.expectEqual(@as(u64, 1), summary.output.measured.script_errors_total); try std.testing.expectEqual(@as(usize, 1), summary.output.measured.request_summaries.len); try std.testing.expectEqualStrings("login", summary.output.measured.request_summaries[0].name); }