Something went wrong. Try again.
xburner is an experimental HTTP and gRPC load generator written in Zig.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140const std = @import("std");
const abi = @import("../abi.zig");const http_state = @import("../protocol/http_state.zig");const runner_log = @import("../runner/log.zig");const template = @import("../template.zig");
pub const WorkflowHostState = struct { pub const SetVarMode = enum { vu_iter_only, setup_only, none, };
allocator: std.mem.Allocator, store: template.VarStore, cookie_jar: http_state.CookieJar, response_headers: []http_state.OwnedHeader = &.{}, prng: std.Random.DefaultPrng, script_call_deadline_nanos: i128 = 0, script_timeout_triggered: bool = false, set_var_mode: SetVarMode = .vu_iter_only, allow_stop_abort: bool = true, logger: ?*runner_log.Logger = null, log_phase: ?[]const u8 = null, vu_id: ?u32 = null, iteration_no: ?u64 = null,
pub fn deinit(self: *WorkflowHostState) void { self.store.deinit(); self.cookie_jar.deinit(); http_state.clearOwnedHeaders(self.allocator, self.response_headers); self.* = undefined; }
pub fn replaceResponseHeaders(self: *WorkflowHostState, headers: []http_state.OwnedHeader) void { http_state.clearOwnedHeaders(self.allocator, self.response_headers); self.response_headers = headers; }};
pub fn hostStateFromUserData(user_data: ?*anyopaque) *WorkflowHostState { return @ptrCast(@alignCast(user_data.?));}
pub fn workflowHostRandU64(user_data: ?*anyopaque) callconv(.c) u64 { const host = hostStateFromUserData(user_data); return host.prng.random().int(u64);}
pub fn workflowHostNowNanos(user_data: ?*anyopaque) callconv(.c) u64 { _ = user_data; return @intCast(std.time.nanoTimestamp());}
pub fn workflowHostLog(user_data: ?*anyopaque, level: u32, message_ptr: [*c]const u8, message_len: u32) callconv(.c) i32 { const host = hostStateFromUserData(user_data); const logger = host.logger orelse return 0; const message = if (message_ptr != null and message_len != 0) message_ptr[0..message_len] else ""; logger.log(guestLogLevel(level), message, .{ .component = "workflow", .source = .guest, .event = "guest_log", .phase = host.log_phase, .vu_id = host.vu_id, .iteration_no = host.iteration_no, }) catch return -1; return 0;}
pub fn workflowHostGetVar(user_data: ?*anyopaque, key_ptr: [*c]const u8, key_len: u32, out_ptr: [*c]u8, out_cap: u32) callconv(.c) u64 { const host = hostStateFromUserData(user_data); const key = key_ptr[0..key_len]; const value = host.store.get(key) orelse return abi.packHostGetVarResult(.not_found, 0);
if (value.len > out_cap) { return abi.packHostGetVarResult(.buffer_too_small, @intCast(value.len)); }
@memcpy(out_ptr[0..value.len], value); return abi.packHostGetVarResult(.found_and_copied, @intCast(value.len));}
pub fn workflowHostSetVar(user_data: ?*anyopaque, key_ptr: [*c]const u8, key_len: u32, value_ptr: [*c]const u8, value_len: u32) callconv(.c) i32 { const host = hostStateFromUserData(user_data); const key = key_ptr[0..key_len]; const value = value_ptr[0..value_len];
switch (host.set_var_mode) { .vu_iter_only => { if (!(std.mem.startsWith(u8, key, "iter.") or std.mem.startsWith(u8, key, "vu."))) { return -1; } }, .setup_only => { if (!std.mem.startsWith(u8, key, "setup.")) { return -1; } }, .none => return -1, }
host.store.set(key, value) catch return -1; return 0;}
pub fn workflowHostShouldAbort(host: *WorkflowHostState, stop_requested: bool) bool { if (stop_requested and host.allow_stop_abort) return true; if (host.script_call_deadline_nanos != 0 and std.time.nanoTimestamp() >= host.script_call_deadline_nanos) { host.script_timeout_triggered = true; return true; } return false;}
pub fn workflowHostGetResponseHeader(user_data: ?*anyopaque, key_ptr: [*c]const u8, key_len: u32, out_ptr: [*c]u8, out_cap: u32) callconv(.c) u64 { const host = hostStateFromUserData(user_data); const key = key_ptr[0..key_len];
for (host.response_headers) |header| { if (!std.ascii.eqlIgnoreCase(header.name, key)) continue; if (header.value.len > out_cap) { return abi.packHostGetVarResult(.buffer_too_small, @intCast(header.value.len)); } @memcpy(out_ptr[0..header.value.len], header.value); return abi.packHostGetVarResult(.found_and_copied, @intCast(header.value.len)); }
return abi.packHostGetVarResult(.not_found, 0);}
fn guestLogLevel(level: u32) runner_log.Level { return switch (level) { 0 => .debug, 1 => .info, 2 => .warn, else => .err, };}