const std = @import("std"); pub const Duration = struct { nanos: u64, pub const zero: Duration = .{ .nanos = 0 }; pub fn fromSeconds(seconds: u64) Duration { return .{ .nanos = seconds * std.time.ns_per_s }; } pub fn fromMillis(milliseconds: u64) Duration { return .{ .nanos = milliseconds * std.time.ns_per_ms }; } }; pub const ByteSize = struct { bytes: u64, }; pub const Scenario = struct { name: []const u8, protocol: Protocol, target: []const u8, wasm: ?[]const u8 = null, wasm_backend: WasmBackend = .wamr, pub fn mode(self: Scenario) ScenarioMode { return if (self.wasm == null) .simple else .workflow; } }; pub const RecoveryPolicy = struct { window: Duration = Duration.fromSeconds(5), max_recreates: u32 = 3, }; pub const RampStage = struct { target_vus: u32, duration: Duration, }; pub const ScenarioMode = enum { simple, workflow, pub fn label(self: ScenarioMode) []const u8 { return switch (self) { .simple => "simple", .workflow => "workflow", }; } }; pub const Protocol = enum { http, grpc, }; pub const WasmBackend = enum { wamr, wasm3, pub fn label(self: WasmBackend) []const u8 { return switch (self) { .wamr => "wamr", .wasm3 => "wasm3", }; } }; pub const Execution = struct { mode: Mode, duration: Duration, warmup: Duration = .zero, script_timeout: Duration = Duration.fromMillis(100), setup_timeout: Duration = Duration.fromSeconds(5), shutdown_timeout: Duration = Duration.fromSeconds(30), stages: []const RampStage = &.{}, vus: ?u32 = null, rate: ?u32 = null, pre_allocated_vus: ?u32 = null, max_vus: ?u32 = null, max_poisoned_workers: u32 = 0, thread_count: ?u32 = null, pub const Mode = enum { constant_vus, constant_arrival_rate, ramping_vus, }; pub fn maxRampingVus(self: Execution) u32 { var max_vus: u32 = 0; for (self.stages) |stage| { if (stage.target_vus > max_vus) max_vus = stage.target_vus; } return max_vus; } pub fn rampingTargetAt(self: Execution, elapsed_nanos: u64) u32 { var remaining = elapsed_nanos; var previous_target: u32 = 0; for (self.stages) |stage| { if (remaining < stage.duration.nanos) { if (stage.duration.nanos == 0) return stage.target_vus; const delta = if (stage.target_vus >= previous_target) stage.target_vus - previous_target else previous_target - stage.target_vus; const scaled = @divTrunc(@as(u64, delta) * remaining, stage.duration.nanos); return if (stage.target_vus >= previous_target) previous_target + @as(u32, @intCast(scaled)) else previous_target - @as(u32, @intCast(scaled)); } remaining -= stage.duration.nanos; previous_target = stage.target_vus; } return previous_target; } pub fn effectiveThreadCount(self: Execution, default_count: usize) usize { const requested = if (self.thread_count) |count| @as(usize, count) else default_count; return @max(@as(usize, 1), requested); } pub fn resolvedThreadCount(self: Execution) usize { return self.effectiveThreadCount(std.Thread.getCpuCount() catch 1); } }; pub const SetupConfig = struct { requests: []const SetupRequest, }; pub const OtlpExporter = struct { url: []const u8, retry_max_attempts: u32 = 4, retry_max_elapsed: Duration = Duration.fromSeconds(30), buffer_max_exports: u32 = 8, }; pub const HttpVersion = enum { auto, http11, http2, pub fn label(self: HttpVersion) []const u8 { return switch (self) { .auto => "auto", .http11 => "1.1", .http2 => "2", }; } }; pub const ExportersConfig = struct { otlp: ?OtlpExporter = null, plugins: []const PluginExporter = &.{}, }; pub const PluginsConfig = struct { dir: ?[]const u8 = null, }; pub const HttpConfig = struct { version: HttpVersion = .http11, max_connections: ?u32 = null, transport_plugin: ?[]const u8 = null, defaults: HttpDefaults, requests: []const HttpRequestTemplate, }; pub const HttpDefaults = struct { use_tls: bool = false, cookie_jar: bool = true, cookie_jar_scope: CookieJarScope = .vu, max_steps_per_iteration: u32 = 16, headers: []const HeaderTemplate = &.{}, connect_timeout_ms: u32 = 0, timeout_ms: u32 = 0, delay_before_ms: u32 = 0, pass_response_body: bool = false, capture_body_max_bytes: u32 = 0, tls: TlsConfig = .{}, }; pub const CookieJarScope = enum { vu, iteration, }; pub const TlsConfig = struct { ca_file: ?[]const u8 = null, insecure_skip_verify: bool = false, server_name: ?[]const u8 = null, }; pub const HttpMethod = enum { GET, POST, PUT, DELETE, }; pub const HeaderTemplate = struct { key: []const u8, value: []const u8, }; pub const SetupRequest = struct { name: []const u8, method: HttpMethod, path: []const u8, headers: []const HeaderTemplate, body: ?[]const u8 = null, store_body_as: ?[]const u8 = null, timeout_ms: u32 = 0, capture_body_max_bytes: u32 = 0, }; pub const PluginExporter = struct { name: []const u8, config: []const HeaderTemplate = &.{}, }; pub const HttpRequestTemplate = struct { id: u32, name: []const u8, method: HttpMethod, path: []const u8, headers: []const HeaderTemplate, body: ?[]const u8 = null, connect_timeout_ms: u32 = 0, timeout_ms: u32 = 0, delay_before_ms: u32 = 0, pass_response_body: bool = false, capture_body_max_bytes: u32 = 0, check_status_code: ?u16 = null, check_body_contains: ?[]const u8 = null, check_response_header: ?[]const u8 = null, }; pub const ThresholdMetric = enum { http_request_duration_p50, http_request_duration_p90, http_request_duration_p99, http_error_rate, http_transport_error_total, http_transport_error_rate, http_transport_timeout_total, http_transport_timeout_rate, http_transport_connect_timeout_total, http_transport_connect_timeout_rate, http_transport_io_timeout_total, http_transport_io_timeout_rate, check_failed_total, check_pass_rate, scheduler_lag_avg, scheduler_lag_max, iteration_dropped, iteration_success_rate, pub const ValueKind = enum { duration, ratio, integer, float, }; pub fn valueKind(self: ThresholdMetric) ValueKind { return switch (self) { .http_request_duration_p50, .http_request_duration_p90, .http_request_duration_p99, .scheduler_lag_avg, .scheduler_lag_max, => .duration, .http_error_rate, .http_transport_error_rate, .http_transport_timeout_rate, .http_transport_connect_timeout_rate, .http_transport_io_timeout_rate, .check_pass_rate, .iteration_success_rate, => .ratio, .http_transport_error_total, .http_transport_timeout_total, .http_transport_connect_timeout_total, .http_transport_io_timeout_total, .check_failed_total, .iteration_dropped, => .integer, }; } }; pub const ThresholdOperator = enum { lt, lte, gt, gte, eq, }; pub const ThresholdScope = struct { request: ?[]const u8 = null, scenario: ?[]const u8 = null, protocol: ?Protocol = null, }; pub const ThresholdValue = union(enum) { duration: Duration, ratio: f64, integer: u64, float: f64, }; pub const ThresholdClause = struct { metric: ThresholdMetric, op: ThresholdOperator, value: ThresholdValue, }; pub const ThresholdExpressionMode = enum { all_of, any_of, }; pub const Threshold = struct { metric: ThresholdMetric, scope: ThresholdScope = .{}, op: ThresholdOperator, value: ThresholdValue, abort_on_fail: bool = false, expression_mode: ?ThresholdExpressionMode = null, expression_clauses: []const ThresholdClause = &.{}, };