const std = @import("std"); const config = @import("../config.zig"); const contracts = @import("../contracts.zig"); const exporter = @import("exporter.zig"); const runner_control = @import("control.zig"); const runner_context = @import("context.zig"); const runner_log = @import("log.zig"); const runner_metrics = @import("metrics.zig"); const runner_observation = @import("observation.zig"); const runner_orchestration = @import("orchestration.zig"); const simple_runner = @import("simple.zig"); const template = @import("../template.zig"); const workflow_scenario = @import("../workflow/scenario.zig"); const workflow_runner = @import("../workflow/runner.zig"); pub const ExecuteOptions = struct { initial_store: ?*const template.VarStore = null, run_setup: bool = true, run_workflow_setup: bool = true, run_workflow_teardown: bool = true, write_exporters: bool = true, abort_on_fail_enabled: bool = true, evaluate_thresholds: bool = true, }; pub const ExecuteResult = struct { allocator: std.mem.Allocator, summary: runner_metrics.FinalSummary, snapshot: runner_metrics.AggregatorSnapshot, exit_code: contracts.ExitCode, pub fn deinit(self: *ExecuteResult) void { self.summary.deinit(); self.snapshot.deinit(self.allocator); self.* = undefined; } pub fn observation(self: *const ExecuteResult) runner_observation.Observation { return runner_observation.Observation.init(&self.summary, &self.snapshot); } }; pub fn run(allocator: std.mem.Allocator, scenario: *const config.ScenarioConfig, stdout: anytype, stderr: anytype) !contracts.ExitCode { var result = try execute(allocator, scenario, stderr, .{}); defer result.deinit(); try writeJsonLine(result.summary.output, stdout); return result.exit_code; } pub fn execute( allocator: std.mem.Allocator, scenario: *const config.ScenarioConfig, stderr: anytype, options: ExecuteOptions, ) !ExecuteResult { var stderr_holder = runner_log.WriterHolder(@TypeOf(stderr)).init(stderr); var logger = runner_log.Logger.init(allocator, stderr_holder.sink()); var signal_guard = try runner_control.installSignalHandlers(); defer signal_guard.restore(); runner_control.reset(); var session = try runner_context.Session.init(allocator, scenario); defer session.deinit(); var scenario_lifecycle: ?workflow_scenario.ScenarioLifecycle = null; defer if (scenario_lifecycle) |*lifecycle| lifecycle.deinit(); var aggregator = runner_metrics.MetricsAggregator{ .allocator = allocator }; defer aggregator.deinit(); try runner_orchestration.applyInitialStore(&session.shared_store, options.initial_store); if (options.run_setup) { try runner_orchestration.runSetupPhase( allocator, &session.client, &session.shared_store, &session.tls_context, scenario, &logger, ); } if (scenario.scenario.mode() == .workflow and options.run_workflow_setup) { scenario_lifecycle = runner_orchestration.runWorkflowSetup( allocator, &session.shared_store, scenario, &logger, ) catch |err| { const context = runner_log.Context{ .component = "workflow", .source = .host, .event = switch (err) { error.WasmValidationFailed, error.UnsupportedAbiVersion, error.MissingRequiredExport, => "workflow_validation_failed", error.ScenarioSetupFailed => "workflow_setup_failed", else => "workflow_setup_init_failed", }, }; switch (err) { error.WasmValidationFailed, error.UnsupportedAbiVersion, error.MissingRequiredExport, => try logger.logf(.err, context, "workflow validation failed: {s}", .{@errorName(err)}), error.ScenarioSetupFailed => try logger.logf(.err, context, "workflow setup failed: {s}", .{@errorName(err)}), else => try logger.logf(.err, context, "workflow setup init failed: {s}", .{@errorName(err)}), } return try earlyExitResult(&aggregator, allocator, scenario, .invalid_config); }; } const measured_start = std.time.nanoTimestamp(); const warmup_end = measured_start + @as(i128, @intCast(scenario.execution.warmup.nanos)); const measured_end = warmup_end + @as(i128, @intCast(scenario.execution.duration.nanos)); var next_progress = measured_start + @as(i128, std.time.ns_per_s); var runtime_failed = false; if (scenario.execution.thread_count) |thread_count| { if (thread_count > 1 and !(scenario.scenario.mode() == .simple and scenario.execution.mode == .constant_vus)) { try logger.log(.warn, "execution.thread_count > 1 currently only applies to simple constant-vus runs", .{ .component = "runner", .source = .host, .event = "thread_count_ignored", }); } } switch (scenario.scenario.mode()) { .simple => try simple_runner.runSimpleMode( allocator, &session.client, &session.grpc_client, &session.shared_store, &session.tls_context, scenario, &aggregator, warmup_end, measured_end, &next_progress, &runner_control.stop_requested, options.abort_on_fail_enabled, &runner_control.request_error_logged, &logger, ), .workflow => runtime_failed = try workflow_runner.run( allocator, &session.client, &session.grpc_client, &session.shared_store, &session.tls_context, scenario, &aggregator, warmup_end, measured_end, &next_progress, options.abort_on_fail_enabled, &logger, ), } if (options.run_workflow_teardown) { runtime_failed = runtime_failed or try runner_orchestration.runWorkflowTeardown( if (scenario_lifecycle) |*lifecycle| lifecycle else null, scenario, runner_control.loadStopRequested(), &logger, ); } const interrupted = runner_control.loadStopRequested(); const snapshot = try aggregator.snapshot(allocator, interrupted); var summary = try aggregator.finalizeWithOptions(allocator, scenario, interrupted, options.evaluate_thresholds); if (options.write_exporters) { exporter.writeConfiguredObservation(allocator, scenario, runner_observation.Observation.init(&summary, &snapshot), &logger) catch |err| { try logger.logf(.err, .{ .component = "exporter", .source = .host, .event = "export_failed", }, "exporter write failed: {s}", .{@errorName(err)}); }; } return .{ .allocator = allocator, .summary = summary, .snapshot = snapshot, .exit_code = if (interrupted) .interrupted else if (runtime_failed) .invalid_config else if (summary.threshold_failed) .threshold_failed else .success, }; } fn earlyExitResult( aggregator: *runner_metrics.MetricsAggregator, allocator: std.mem.Allocator, scenario: *const config.ScenarioConfig, exit_code: contracts.ExitCode, ) !ExecuteResult { const snapshot = try aggregator.snapshot(allocator, false); const summary = try aggregator.finalizeWithOptions(allocator, scenario, false, false); return .{ .allocator = allocator, .summary = summary, .snapshot = snapshot, .exit_code = exit_code, }; } 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(); }