Something went wrong. Try again.
xburner is an experimental HTTP and gRPC load generator written in Zig.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215const std = @import("std");
const abi = @import("../abi.zig");const config = @import("../config.zig");const http_state = @import("http_state.zig");const transport = @import("transport.zig");
const http = std.http;
const OwnedHeader = http_state.OwnedHeader;const clearOwnedHeaders = http_state.clearOwnedHeaders;
pub const TimeoutPhase = transport.TimeoutPhase;
pub const RequestExecution = struct { status_code: u16, duration_nanos: u64, bytes_sent: u64, bytes_received: u64, success: bool, checks_passed: u32 = 0, checks_failed: u32 = 0, transport_code: abi.TransportCode = .none, timeout_phase: TimeoutPhase = .none, grpc_status: ?u8 = null, deadline_exceeded: bool = false, grpc_message: ?[]u8 = null, body_truncated: bool = false, headers: []OwnedHeader = &.{}, captured_body: ?[]u8 = null,
pub fn deinit(self: *RequestExecution, allocator: std.mem.Allocator) void { if (self.captured_body) |body| allocator.free(body); if (self.grpc_message) |message| allocator.free(message); clearOwnedHeaders(allocator, self.headers); self.* = undefined; }};
pub const default_check_body_capture_max_bytes: u32 = 64 * 1024;
pub fn describeRequestError(allocator: std.mem.Allocator, err: anyerror, http_config: *const config.HttpConfig) ![]u8 { if (isFdExhaustionError(err)) { if (http_config.max_connections) |limit| { return try std.fmt.allocPrint( allocator, "{s}; consider lowering http.max_connections (current {d}) to cap pooled TLS connections", .{ @errorName(err), limit }, ); } return try std.fmt.allocPrint( allocator, "{s}; consider setting http.max_connections to cap pooled TLS connections", .{@errorName(err)}, ); }
return try allocator.dupe(u8, @errorName(err));}
pub fn effectiveCaptureBodyMaxBytes(request: *const config.HttpRequestTemplate) u32 { if (request.check_body_contains != null and request.capture_body_max_bytes == 0) { return default_check_body_capture_max_bytes; } return request.capture_body_max_bytes;}
pub fn evaluateBuiltInChecks(request: *const config.HttpRequestTemplate, execution: *RequestExecution) void { var passed: u32 = 0; var failed: u32 = 0;
if (request.check_status_code) |expected_status| { if (execution.status_code == expected_status) { passed += 1; } else { failed += 1; } }
if (request.check_body_contains) |needle| { const body = execution.captured_body orelse ""; if (std.mem.indexOf(u8, body, needle) != null) { passed += 1; } else { failed += 1; } }
if (request.check_response_header) |header_name| { if (hasResponseHeader(execution.headers, header_name)) { passed += 1; } else { failed += 1; } }
execution.checks_passed = passed; execution.checks_failed = failed; if (failed != 0) execution.success = false;}
pub fn configuredCheckCount(request: *const config.HttpRequestTemplate) u32 { var count: u32 = 0; if (request.check_status_code != null) count += 1; if (request.check_body_contains != null) count += 1; if (request.check_response_header != null) count += 1; return count;}
pub fn copyResponseHeaders(allocator: std.mem.Allocator, head: http.Client.Response.Head) ![]OwnedHeader { var iterator = head.iterateHeaders(); var headers = std.ArrayList(OwnedHeader).empty; defer headers.deinit(allocator);
while (iterator.next()) |header| { try headers.append(allocator, .{ .name = try allocator.dupe(u8, header.name), .value = try allocator.dupe(u8, header.value), }); }
return try headers.toOwnedSlice(allocator);}
fn hasResponseHeader(headers: []const OwnedHeader, name: []const u8) bool { for (headers) |header| { if (std.ascii.eqlIgnoreCase(header.name, name)) return true; } return false;}
fn isFdExhaustionError(err: anyerror) bool { return switch (err) { error.ProcessFdQuotaExceeded, error.SystemFdQuotaExceeded, => true, else => false, };}
test "fd exhaustion hint recommends max_connections" { const allocator = std.testing.allocator; const without_limit = try describeRequestError(allocator, error.ProcessFdQuotaExceeded, &.{ .max_connections = null, .defaults = .{}, .requests = &.{}, }); defer allocator.free(without_limit); try std.testing.expect(std.mem.indexOf(u8, without_limit, "http.max_connections") != null);
const with_limit = try describeRequestError(allocator, error.SystemFdQuotaExceeded, &.{ .max_connections = 16, .defaults = .{}, .requests = &.{}, }); defer allocator.free(with_limit); try std.testing.expect(std.mem.indexOf(u8, with_limit, "current 16") != null);}
test "built-in checks update execution success and counters" { var execution = RequestExecution{ .status_code = 200, .duration_nanos = 1, .bytes_sent = 0, .bytes_received = 2, .success = true, .headers = try std.testing.allocator.dupe(OwnedHeader, &.{.{ .name = try std.testing.allocator.dupe(u8, "x-trace-id"), .value = try std.testing.allocator.dupe(u8, "abc"), }}), .captured_body = try std.testing.allocator.dupe(u8, "ok"), }; defer execution.deinit(std.testing.allocator);
const request = config.HttpRequestTemplate{ .id = 1, .name = "health", .method = .GET, .path = "/health", .headers = &.{}, .check_status_code = 200, .check_body_contains = "ok", .check_response_header = "x-trace-id", };
evaluateBuiltInChecks(&request, &execution);
try std.testing.expect(execution.success); try std.testing.expectEqual(@as(u32, 3), execution.checks_passed); try std.testing.expectEqual(@as(u32, 0), execution.checks_failed);}
test "effective capture size promotes body checks" { const request = config.HttpRequestTemplate{ .id = 1, .name = "health", .method = .GET, .path = "/health", .headers = &.{}, .check_body_contains = "ok", };
try std.testing.expectEqual(default_check_body_capture_max_bytes, effectiveCaptureBodyMaxBytes(&request)); try std.testing.expectEqual(@as(u32, 3), configuredCheckCount(&.{ .id = 1, .name = "health", .method = .GET, .path = "/health", .headers = &.{}, .check_status_code = 200, .check_body_contains = "ok", .check_response_header = "x-trace-id", }));}