const std = @import("std"); const literals = @import("literals.zig"); const tables = @import("tables.zig"); const types = @import("types.zig"); pub const SchemaError = error{ MissingScenarioName, MissingScenarioProtocol, MissingScenarioTarget, MissingExecutionMode, MissingExecutionDuration, MissingExecutionVus, MissingExecutionRate, MissingExecutionPreAllocatedVus, MissingExecutionMaxVus, MissingExecutionStageTargetVus, MissingExecutionStageDuration, MissingRequestId, MissingRequestName, MissingRequestMethod, MissingRequestPath, MissingSetupRequestName, MissingSetupRequestMethod, MissingSetupRequestPath, MissingThresholdMetric, MissingThresholdOperator, MissingThresholdValue, MissingExporterUrl, MissingPluginExporterName, InvalidRequestPath, InvalidThresholdValue, InvalidThresholdExpression, MixedThresholdExpressionOperators, HttpMaxConnectionsMustBePositive, InvalidCheckStatusCode, OtlpRetryMaxAttemptsMustBePositive, OtlpRetryMaxElapsedMustBePositive, OtlpBufferMaxExportsMustBePositive, MaxStepsMustBePositive, ScriptTimeoutMustBePositive, ExecutionThreadCountMustBePositive, RecoveryWindowMustBePositive, RecoveryMaxRecreatesMustBePositive, MissingExecutionStages, ExecutionStageDurationMustBePositive, }; pub const PluginsConfigBuilder = struct { dir: ?[]const u8 = null, pub fn build(self: PluginsConfigBuilder) types.PluginsConfig { return .{ .dir = self.dir }; } }; pub const ScenarioBuilder = struct { name: ?[]const u8 = null, protocol: ?types.Protocol = null, target: ?[]const u8 = null, wasm: ?[]const u8 = null, wasm_backend: types.WasmBackend = .wamr, pub fn build(self: ScenarioBuilder) SchemaError!types.Scenario { return .{ .name = self.name orelse return error.MissingScenarioName, .protocol = self.protocol orelse return error.MissingScenarioProtocol, .target = self.target orelse return error.MissingScenarioTarget, .wasm = self.wasm, .wasm_backend = self.wasm_backend, }; } }; pub const RecoveryPolicyBuilder = struct { window: types.Duration = types.Duration.fromSeconds(5), max_recreates: u32 = 3, pub fn build(self: RecoveryPolicyBuilder) SchemaError!types.RecoveryPolicy { if (self.window.nanos == 0) return error.RecoveryWindowMustBePositive; if (self.max_recreates == 0) return error.RecoveryMaxRecreatesMustBePositive; return .{ .window = self.window, .max_recreates = self.max_recreates, }; } }; pub const ExecutionBuilder = struct { mode: ?types.Execution.Mode = null, duration: ?types.Duration = null, warmup: types.Duration = .zero, script_timeout: types.Duration = types.Duration.fromMillis(100), setup_timeout: types.Duration = types.Duration.fromSeconds(5), shutdown_timeout: types.Duration = types.Duration.fromSeconds(30), stages: []const types.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 fn build(self: ExecutionBuilder) SchemaError!types.Execution { const mode = self.mode orelse return error.MissingExecutionMode; const duration = self.duration orelse return error.MissingExecutionDuration; if (self.script_timeout.nanos == 0) return error.ScriptTimeoutMustBePositive; if (self.thread_count) |thread_count| { if (thread_count == 0) return error.ExecutionThreadCountMustBePositive; } const execution = types.Execution{ .mode = mode, .duration = duration, .warmup = self.warmup, .script_timeout = self.script_timeout, .setup_timeout = self.setup_timeout, .shutdown_timeout = self.shutdown_timeout, .stages = self.stages, .vus = self.vus, .rate = self.rate, .pre_allocated_vus = self.pre_allocated_vus, .max_vus = self.max_vus, .max_poisoned_workers = self.max_poisoned_workers, .thread_count = self.thread_count, }; switch (mode) { .constant_vus => { if (execution.vus == null) return error.MissingExecutionVus; }, .constant_arrival_rate => { if (execution.rate == null) return error.MissingExecutionRate; if (execution.pre_allocated_vus == null) return error.MissingExecutionPreAllocatedVus; if (execution.max_vus == null) return error.MissingExecutionMaxVus; }, .ramping_vus => { if (execution.stages.len == 0) return error.MissingExecutionStages; }, } return execution; } }; pub const RampStageBuilder = struct { target_vus: ?u32 = null, duration: ?types.Duration = null, pub fn build(self: RampStageBuilder) SchemaError!types.RampStage { const duration = self.duration orelse return error.MissingExecutionStageDuration; if (duration.nanos == 0) return error.ExecutionStageDurationMustBePositive; return .{ .target_vus = self.target_vus orelse return error.MissingExecutionStageTargetVus, .duration = duration, }; } }; pub const TlsConfigBuilder = struct { ca_file: ?[]const u8 = null, insecure_skip_verify: bool = false, server_name: ?[]const u8 = null, pub fn build(self: TlsConfigBuilder) types.TlsConfig { return .{ .ca_file = self.ca_file, .insecure_skip_verify = self.insecure_skip_verify, .server_name = self.server_name, }; } }; pub const HttpDefaultsBuilder = struct { use_tls: bool = false, cookie_jar: bool = true, cookie_jar_scope: types.CookieJarScope = .vu, max_steps_per_iteration: u32 = 16, headers: []const types.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: TlsConfigBuilder = .{}, pub fn build(self: HttpDefaultsBuilder) SchemaError!types.HttpDefaults { if (self.max_steps_per_iteration == 0) return error.MaxStepsMustBePositive; return .{ .use_tls = self.use_tls, .cookie_jar = self.cookie_jar, .cookie_jar_scope = self.cookie_jar_scope, .max_steps_per_iteration = self.max_steps_per_iteration, .headers = self.headers, .connect_timeout_ms = self.connect_timeout_ms, .timeout_ms = self.timeout_ms, .delay_before_ms = self.delay_before_ms, .pass_response_body = self.pass_response_body, .capture_body_max_bytes = self.capture_body_max_bytes, .tls = self.tls.build(), }; } }; pub const HttpConfigBuilder = struct { version: types.HttpVersion = .http11, max_connections: ?u32 = null, transport_plugin: ?[]const u8 = null, pub fn build( self: HttpConfigBuilder, defaults: types.HttpDefaults, requests: []const types.HttpRequestTemplate, ) SchemaError!types.HttpConfig { if (self.max_connections) |limit| { if (limit == 0) return error.HttpMaxConnectionsMustBePositive; } return .{ .version = self.version, .max_connections = self.max_connections, .transport_plugin = self.transport_plugin, .defaults = defaults, .requests = requests, }; } }; pub const SetupRequestBuilder = struct { name: ?[]const u8 = null, method: ?types.HttpMethod = null, path: ?[]const u8 = null, headers: []const types.HeaderTemplate = &.{}, body: ?[]const u8 = null, store_body_as: ?[]const u8 = null, timeout_ms: u32 = 0, capture_body_max_bytes: u32 = 0, pub fn build(self: SetupRequestBuilder) SchemaError!types.SetupRequest { const path = self.path orelse return error.MissingSetupRequestPath; if (path.len == 0 or path[0] != '/') return error.InvalidRequestPath; return .{ .name = self.name orelse return error.MissingSetupRequestName, .method = self.method orelse return error.MissingSetupRequestMethod, .path = path, .headers = self.headers, .body = self.body, .store_body_as = self.store_body_as, .timeout_ms = self.timeout_ms, .capture_body_max_bytes = self.capture_body_max_bytes, }; } }; pub const RequestBuilder = struct { id: ?u32 = null, name: ?[]const u8 = null, method: ?types.HttpMethod = null, path: ?[]const u8 = null, headers: []const types.HeaderTemplate = &.{}, body: ?[]const u8 = null, connect_timeout_ms: ?u32 = null, timeout_ms: ?u32 = null, delay_before_ms: ?u32 = null, pass_response_body: ?bool = null, capture_body_max_bytes: ?u32 = null, check_status_code: ?u32 = null, check_body_contains: ?[]const u8 = null, check_response_header: ?[]const u8 = null, pub fn build( self: RequestBuilder, allocator: std.mem.Allocator, defaults: types.HttpDefaults, ) (std.mem.Allocator.Error || SchemaError)!types.HttpRequestTemplate { const path = self.path orelse return error.MissingRequestPath; if (path.len == 0 or path[0] != '/') return error.InvalidRequestPath; return .{ .id = self.id orelse return error.MissingRequestId, .name = self.name orelse return error.MissingRequestName, .method = self.method orelse return error.MissingRequestMethod, .path = path, .headers = try tables.mergeDefaultHeaders(allocator, defaults.headers, self.headers), .body = self.body, .connect_timeout_ms = self.connect_timeout_ms orelse defaults.connect_timeout_ms, .timeout_ms = self.timeout_ms orelse defaults.timeout_ms, .delay_before_ms = self.delay_before_ms orelse defaults.delay_before_ms, .pass_response_body = self.pass_response_body orelse defaults.pass_response_body, .capture_body_max_bytes = self.capture_body_max_bytes orelse defaults.capture_body_max_bytes, .check_status_code = if (self.check_status_code) |status_code| std.math.cast(u16, status_code) orelse return error.InvalidCheckStatusCode else null, .check_body_contains = self.check_body_contains, .check_response_header = self.check_response_header, }; } }; pub const ThresholdBuilder = struct { metric: ?types.ThresholdMetric = null, scope: types.ThresholdScope = .{}, op: ?types.ThresholdOperator = null, value: ?[]const u8 = null, expr: ?[]const u8 = null, abort_on_fail: bool = false, pub fn build(self: ThresholdBuilder, allocator: std.mem.Allocator) (std.mem.Allocator.Error || SchemaError)!types.Threshold { if (self.expr) |expression| { const parsed = literals.parseThresholdExpression(allocator, expression) catch |err| switch (err) { error.InvalidThresholdExpression, error.MixedThresholdExpressionOperators, => return @errorCast(err), else => return error.InvalidThresholdValue, }; const first_clause = parsed.clauses[0]; return .{ .metric = first_clause.metric, .scope = self.scope, .op = first_clause.op, .value = first_clause.value, .abort_on_fail = self.abort_on_fail, .expression_mode = parsed.mode, .expression_clauses = parsed.clauses, }; } const metric = self.metric orelse return error.MissingThresholdMetric; const op = self.op orelse return error.MissingThresholdOperator; const raw_value = self.value orelse return error.MissingThresholdValue; return .{ .metric = metric, .scope = self.scope, .op = op, .value = literals.parseThresholdValue(metric, raw_value) catch return error.InvalidThresholdValue, .abort_on_fail = self.abort_on_fail, }; } }; pub const OtlpExporterBuilder = struct { url: ?[]const u8 = null, retry_max_attempts: u32 = 4, retry_max_elapsed: types.Duration = types.Duration.fromSeconds(30), buffer_max_exports: u32 = 8, pub fn build(self: OtlpExporterBuilder) SchemaError!types.OtlpExporter { if (self.retry_max_attempts == 0) return error.OtlpRetryMaxAttemptsMustBePositive; if (self.retry_max_elapsed.nanos == 0) return error.OtlpRetryMaxElapsedMustBePositive; if (self.buffer_max_exports == 0) return error.OtlpBufferMaxExportsMustBePositive; return .{ .url = self.url orelse return error.MissingExporterUrl, .retry_max_attempts = self.retry_max_attempts, .retry_max_elapsed = self.retry_max_elapsed, .buffer_max_exports = self.buffer_max_exports, }; } }; pub const PluginExporterBuilder = struct { name: ?[]const u8 = null, config: []const types.HeaderTemplate = &.{}, pub fn build(self: PluginExporterBuilder) SchemaError!types.PluginExporter { return .{ .name = self.name orelse return error.MissingPluginExporterName, .config = self.config, }; } };