const std = @import("std"); const abi = @import("../abi.zig"); const config = @import("../config.zig"); const contracts = @import("../contracts.zig"); const http_state = @import("../protocol/http_state.zig"); const runner_control = @import("control.zig"); const runner_context = @import("context.zig"); const runner_log = @import("log.zig"); const runner_setup = @import("setup.zig"); const simple_runner = @import("simple.zig"); const template = @import("../template.zig"); const workflow_host = @import("../workflow/host.zig"); const workflow_scenario = @import("../workflow/scenario.zig"); const runtime = @import("../runtime.zig"); const CookieJar = http_state.CookieJar; const WorkflowHostState = workflow_host.WorkflowHostState; const ProbeError = error{ DryRunFailed, }; const ProbeOutput = struct { schema_version: u32, kind: []const u8, scenario_mode: []const u8, wasm_backend: []const u8, setup_requests: usize, measured_requests: usize, wasm: ?[]const u8 = null, rendered_path: ?[]const u8 = null, rendered_request_id: ?u32 = null, rendered_method: ?[]const u8 = null, rendered_body_bytes: ?usize = null, }; pub fn probeSetup(allocator: std.mem.Allocator, scenario: *const config.ScenarioConfig, stdout: anytype, stderr: anytype) !void { var stderr_holder = runner_log.WriterHolder(@TypeOf(stderr)).init(stderr); var logger = runner_log.Logger.init(allocator, stderr_holder.sink()); var session = try runner_context.Session.init(allocator, scenario); defer session.deinit(); try runner_setup.runSetupPhase(allocator, &session.client, &session.shared_store, &session.tls_context, scenario, &logger); if (scenario.scenario.mode() == .workflow) { var lifecycle = try workflow_scenario.ScenarioLifecycle.init(allocator, &session.shared_store, scenario, &logger); defer lifecycle.deinit(); try lifecycle.runSetup(&session.shared_store, scenario); } const output = ProbeOutput{ .schema_version = contracts.probe_schema_version, .kind = "with-setup", .scenario_mode = scenario.scenario.mode().label(), .wasm_backend = scenario.scenario.wasm_backend.label(), .setup_requests = scenario.setup.requests.len, .measured_requests = scenario.http.requests.len, }; try writeJsonLine(output, stdout); } pub fn probeDryRun(allocator: std.mem.Allocator, scenario: *const config.ScenarioConfig, stdout: anytype, stderr: anytype) !void { var stderr_holder = runner_log.WriterHolder(@TypeOf(stderr)).init(stderr); var logger = runner_log.Logger.init(allocator, stderr_holder.sink()); var session = try runner_context.Session.init(allocator, scenario); defer session.deinit(); switch (scenario.scenario.mode()) { .simple => { const request = &scenario.http.requests[0]; const rendered_path = try template.render(allocator, request.path, &session.shared_store, .path); defer allocator.free(rendered_path); const output = ProbeOutput{ .schema_version = contracts.probe_schema_version, .kind = "dry-run", .scenario_mode = scenario.scenario.mode().label(), .wasm_backend = scenario.scenario.wasm_backend.label(), .setup_requests = scenario.setup.requests.len, .measured_requests = scenario.http.requests.len, .wasm = scenario.scenario.wasm, .rendered_path = rendered_path, .rendered_request_id = request.id, .rendered_method = @tagName(request.method), .rendered_body_bytes = if (request.body) |body| body.len else 0, }; try writeJsonLine(output, stdout); }, .workflow => { const backend = runtime.fromConfigBackend(scenario.scenario.wasm_backend); var host_store = template.VarStore.init(allocator); defer host_store.deinit(); try host_store.cloneFrom(&session.shared_store); var host = WorkflowHostState{ .allocator = allocator, .store = host_store, .cookie_jar = CookieJar.init(allocator), .prng = std.Random.DefaultPrng.init(0xA11CE), .logger = &logger, .log_phase = "dry-run", .vu_id = 1, .iteration_no = 1, }; defer host.deinit(); var instance = try runtime.WorkflowInstance.create(allocator, scenario.scenario.wasm.?, backend, .{ .user_data = &host, .rand_u64 = workflow_host.workflowHostRandU64, .now_nanos = workflow_host.workflowHostNowNanos, .log = workflow_host.workflowHostLog, .get_var = workflow_host.workflowHostGetVar, .set_var = workflow_host.workflowHostSetVar, .get_response_header = workflow_host.workflowHostGetResponseHeader, .check_abort = runner_control.workflowHostCheckAbort, }); defer instance.deinit(); _ = try instance.init(1, 0, 0xA11CE); const input = abi.IterationInputV1{ .scenario_id = 1, .vu_id = 1, .iteration_no = 1, .monotonic_nanos = @intCast(std.time.nanoTimestamp()), .random_u64 = host.prng.random().int(u64), }; _ = try instance.startIteration(input); const maybe_action = try instance.nextRequest(allocator); if (maybe_action) |next| { defer allocator.free(next.body); const request = scenario.findRequestById(next.action.request_id) orelse { try logger.log(.err, "dry-run probe failed: unknown request id", .{ .component = "runner", .source = .host, .event = "dry_run_failed", }); return error.DryRunFailed; }; const rendered_path = try template.render(allocator, request.path, &host.store, .path); defer allocator.free(rendered_path); const output = ProbeOutput{ .schema_version = contracts.probe_schema_version, .kind = "dry-run", .scenario_mode = scenario.scenario.mode().label(), .wasm_backend = scenario.scenario.wasm_backend.label(), .setup_requests = scenario.setup.requests.len, .measured_requests = scenario.http.requests.len, .wasm = scenario.scenario.wasm, .rendered_path = rendered_path, .rendered_request_id = request.id, .rendered_method = @tagName(request.method), .rendered_body_bytes = next.body.len, }; try writeJsonLine(output, stdout); } else { const output = ProbeOutput{ .schema_version = contracts.probe_schema_version, .kind = "dry-run", .scenario_mode = scenario.scenario.mode().label(), .wasm_backend = scenario.scenario.wasm_backend.label(), .setup_requests = scenario.setup.requests.len, .measured_requests = scenario.http.requests.len, .wasm = scenario.scenario.wasm, }; try writeJsonLine(output, stdout); } }, } } fn writeJsonLine(value: anytype, stdout: anytype) !void { if (@typeInfo(@TypeOf(stdout)) == .pointer) { try std.json.Stringify.value(value, .{}, stdout); try stdout.writeAll("\n"); try stdout.flush(); return; } var writer = stdout; var adapter_buffer: [256]u8 = undefined; var adapter = writer.adaptToNewApi(&adapter_buffer); try std.json.Stringify.value(value, .{}, &adapter.new_interface); try adapter.new_interface.writeAll("\n"); try adapter.new_interface.flush(); }