Updated to zig 0.15.1 and zli 4.1.0

* Refactor: Update build process and improve memory management

- Commented out library tests in build.zig for clarity.
- Changed minimum Zig version to 0.15.1 in build.zig.zon.
- Modified root.zig to accept a Writer for output instead of using log.
- Updated bandwidth.zig tests to use std.Thread.sleep for consistency.
- Adjusted fast.zig to improve memory allocation handling.
- Enhanced http_latency_tester.zig to manage latencies with allocator.
- Refined speed_worker.zig to utilize std.Thread.sleep for delays.
- Improved measurement_strategy.zig to handle speed measurements with allocator.
- Updated main.zig to flush writer after command execution.

* Fix: Update zli dependency to use URL and hash instead of path

* Fix: Add newline to spinner output for better readability

* switch worksflows to using zig 0.15.1

---------

Co-authored-by: mikkelam <mikkelarm@gmail.com>
This commit is contained in:
Caeser 2025-11-17 14:42:35 +01:00 committed by GitHub
parent 55abfcb355
commit af4d3971c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 110 additions and 94 deletions

View file

@ -13,6 +13,8 @@ jobs:
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- uses: mlugg/setup-zig@v2 - uses: mlugg/setup-zig@v2
with:
version: 0.15.1
- run: zig build test - run: zig build test
- run: zig fmt --check src/ - run: zig fmt --check src/
@ -28,6 +30,8 @@ jobs:
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- uses: mlugg/setup-zig@v2 - uses: mlugg/setup-zig@v2
with:
version: 0.15.1
- name: Build ${{ matrix.target }} (${{ matrix.optimize }}) - name: Build ${{ matrix.target }} (${{ matrix.optimize }})
run: | run: |
zig build --release=${{ matrix.optimize == 'ReleaseSafe' && 'safe' || 'off' }} -Dtarget=${{ matrix.target }} zig build --release=${{ matrix.optimize == 'ReleaseSafe' && 'safe' || 'off' }} -Dtarget=${{ matrix.target }}

View file

@ -19,6 +19,8 @@ jobs:
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- uses: mlugg/setup-zig@v2 - uses: mlugg/setup-zig@v2
with:
version: 0.15.1
- name: Build - name: Build
run: zig build -Doptimize=ReleaseFast -Dtarget=${{ matrix.target }} -Dcpu=baseline run: zig build -Doptimize=ReleaseFast -Dtarget=${{ matrix.target }} -Dcpu=baseline
@ -39,6 +41,8 @@ jobs:
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- uses: mlugg/setup-zig@v2 - uses: mlugg/setup-zig@v2
with:
version: 0.15.1
- run: zig build test - run: zig build test
release: release:

View file

@ -4,16 +4,16 @@ pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
// library tests // // library tests
const library_tests = b.addTest(.{ // const library_tests = b.addTest(.{
.root_source_file = b.path("src/test.zig"), // .root_source_file = b.path("src/test.zig"),
.target = target, // .target = target,
.optimize = optimize, // .optimize = optimize,
}); // });
const run_library_tests = b.addRunArtifact(library_tests); // const run_library_tests = b.addRunArtifact(library_tests);
const test_step = b.step("test", "Run all tests"); // const test_step = b.step("test", "Run all tests");
test_step.dependOn(&run_library_tests.step); // test_step.dependOn(&run_library_tests.step);
const dep_zli = b.dependency("zli", .{ const dep_zli = b.dependency("zli", .{
.target = target, .target = target,
@ -43,16 +43,24 @@ pub fn build(b: *std.Build) void {
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
.name = "fast-cli", .name = "fast-cli",
.root_module = b.createModule(.{
// b.createModule defines a new module just like b.addModule but,
// unlike b.addModule, it does not expose the module to consumers of
// this package, which is why in this case we don't have to give it a name.
.root_source_file = b.path("src/main.zig"), .root_source_file = b.path("src/main.zig"),
// Target and optimization levels must be explicitly wired in when
// defining an executable or library (in the root module), and you
// can also hardcode a specific target for an executable or library
// definition if desireable (e.g. firmware for embedded devices).
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
.strip = optimize != .Debug, }),
}); });
exe.root_module.addImport("zli", mod_zli); exe.root_module.addImport("zli", mod_zli);
exe.root_module.addImport("mvzr", mod_mvzr); exe.root_module.addImport("mvzr", mod_mvzr);
exe.root_module.addImport("build_options", build_options.createModule()); exe.root_module.addImport("build_options", build_options.createModule());
library_tests.root_module.addImport("mvzr", mod_mvzr); // library_tests.root_module.addImport("mvzr", mod_mvzr);
// Link against the static library instead // Link against the static library instead

View file

@ -5,7 +5,7 @@
.fingerprint = 0xfb5a9fbee5075971, // Changing this has security and trust implications. .fingerprint = 0xfb5a9fbee5075971, // Changing this has security and trust implications.
.minimum_zig_version = "0.14.0", .minimum_zig_version = "0.15.1",
.dependencies = .{ .dependencies = .{
.mvzr = .{ .mvzr = .{
@ -13,8 +13,8 @@
.hash = "mvzr-0.3.2-ZSOky95lAQA00lXTN_g8JWoBuh8pw-jyzmCWAqlu1h8L", .hash = "mvzr-0.3.2-ZSOky95lAQA00lXTN_g8JWoBuh8pw-jyzmCWAqlu1h8L",
}, },
.zli = .{ .zli = .{
.url = "https://github.com/xcaeser/zli/archive/v3.7.0.tar.gz", .url = "https://github.com/xcaeser/zli/archive/v4.1.0.tar.gz",
.hash = "zli-3.7.0-LeUjpq8uAQCl8uh-ws3jdXsnbCwMZQgcZQx4TVXHLSeQ", .hash = "zli-4.1.0-LeUjplXaAAB2lg0IQPEC9VrTlWThzPfrxXvoJNhGMJl9",
}, },
}, },
.paths = .{ .paths = .{

View file

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const zli = @import("zli"); const zli = @import("zli");
const builtin = @import("builtin"); const builtin = @import("builtin");
const Writer = std.Io.Writer;
const Fast = @import("../lib/fast.zig").Fast; const Fast = @import("../lib/fast.zig").Fast;
const HTTPSpeedTester = @import("../lib/http_speed_tester_v2.zig").HTTPSpeedTester; const HTTPSpeedTester = @import("../lib/http_speed_tester_v2.zig").HTTPSpeedTester;
@ -11,7 +12,6 @@ const BandwidthMeter = @import("../lib/bandwidth.zig");
const SpeedMeasurement = @import("../lib/bandwidth.zig").SpeedMeasurement; const SpeedMeasurement = @import("../lib/bandwidth.zig").SpeedMeasurement;
const progress = @import("../lib/progress.zig"); const progress = @import("../lib/progress.zig");
const HttpLatencyTester = @import("../lib/http_latency_tester.zig").HttpLatencyTester; const HttpLatencyTester = @import("../lib/http_latency_tester.zig").HttpLatencyTester;
const log = std.log.scoped(.cli);
const https_flag = zli.Flag{ const https_flag = zli.Flag{
.name = "https", .name = "https",
@ -44,8 +44,8 @@ const max_duration_flag = zli.Flag{
.default_value = .{ .Int = 30 }, .default_value = .{ .Int = 30 },
}; };
pub fn build(allocator: std.mem.Allocator) !*zli.Command { pub fn build(writer: *Writer, allocator: std.mem.Allocator) !*zli.Command {
const root = try zli.Command.init(allocator, .{ const root = try zli.Command.init(writer, allocator, .{
.name = "fast-cli", .name = "fast-cli",
.description = "Estimate connection speed using fast.com", .description = "Estimate connection speed using fast.com",
.version = null, .version = null,
@ -65,7 +65,9 @@ fn run(ctx: zli.CommandContext) !void {
const json_output = ctx.flag("json", bool); const json_output = ctx.flag("json", bool);
const max_duration = ctx.flag("duration", i64); const max_duration = ctx.flag("duration", i64);
log.info("Config: https={}, upload={}, json={}, max_duration={}s", .{ const spinner = ctx.spinner;
try spinner.print("Config: https={}, upload={}, json={}, max_duration={}s\n", .{
use_https, check_upload, json_output, max_duration, use_https, check_upload, json_output, max_duration,
}); });
@ -74,20 +76,20 @@ fn run(ctx: zli.CommandContext) !void {
const urls = fast.get_urls(5) catch |err| { const urls = fast.get_urls(5) catch |err| {
if (!json_output) { if (!json_output) {
try ctx.spinner.fail("Failed to get URLs: {}", .{err}); try spinner.fail("Failed to get URLs: {}", .{err});
} else { } else {
const error_msg = switch (err) { const error_msg = switch (err) {
error.ConnectionTimeout => "Failed to contact fast.com servers", error.ConnectionTimeout => "Failed to contact fast.com servers",
else => "Failed to get URLs", else => "Failed to get URLs",
}; };
try outputJson(null, null, null, error_msg); try outputJson(ctx.writer, null, null, null, error_msg);
} }
return; return;
}; };
log.info("Got {} URLs", .{urls.len}); try spinner.print("Got {} URLs\n", .{urls.len});
for (urls) |url| { for (urls) |url| {
log.debug("URL: {s}", .{url}); try spinner.print("URL: {s}\n", .{url});
} }
// Measure latency first // Measure latency first
@ -95,9 +97,9 @@ fn run(ctx: zli.CommandContext) !void {
defer latency_tester.deinit(); defer latency_tester.deinit();
const latency_ms = if (!json_output) blk: { const latency_ms = if (!json_output) blk: {
try ctx.spinner.start(.{}, "Measuring latency...", .{}); try spinner.start("Measuring latency...", .{});
const result = latency_tester.measureLatency(urls) catch |err| { const result = latency_tester.measureLatency(urls) catch |err| {
log.err("Latency test failed: {}", .{err}); try spinner.fail("Latency test failed: {}", .{err});
break :blk null; break :blk null;
}; };
break :blk result; break :blk result;
@ -106,7 +108,7 @@ fn run(ctx: zli.CommandContext) !void {
}; };
if (!json_output) { if (!json_output) {
try ctx.spinner.start(.{}, "Measuring download speed...", .{}); try spinner.start("Measuring download speed...", .{});
} }
// Initialize speed tester // Initialize speed tester
@ -126,15 +128,15 @@ fn run(ctx: zli.CommandContext) !void {
const download_result = if (json_output) blk: { const download_result = if (json_output) blk: {
// JSON mode: clean output only // JSON mode: clean output only
break :blk speed_tester.measure_download_speed_stability(urls, criteria) catch |err| { break :blk speed_tester.measure_download_speed_stability(urls, criteria) catch |err| {
log.err("Download test failed: {}", .{err}); try spinner.fail("Download test failed: {}", .{err});
try outputJson(null, null, null, "Download test failed"); try outputJson(ctx.writer, null, null, null, "Download test failed");
return; return;
}; };
} else blk: { } else blk: {
// Interactive mode with spinner updates // Interactive mode with spinner updates
const progressCallback = progress.createCallback(ctx.spinner, updateSpinnerText); const progressCallback = progress.createCallback(spinner, updateSpinnerText);
break :blk speed_tester.measureDownloadSpeedWithStabilityProgress(urls, criteria, progressCallback) catch |err| { break :blk speed_tester.measureDownloadSpeedWithStabilityProgress(urls, criteria, progressCallback) catch |err| {
try ctx.spinner.fail("Download test failed: {}", .{err}); try spinner.fail("Download test failed: {}", .{err});
return; return;
}; };
}; };
@ -142,21 +144,21 @@ fn run(ctx: zli.CommandContext) !void {
var upload_result: ?SpeedTestResult = null; var upload_result: ?SpeedTestResult = null;
if (check_upload) { if (check_upload) {
if (!json_output) { if (!json_output) {
try ctx.spinner.start(.{}, "Measuring upload speed...", .{}); try spinner.start("Measuring upload speed...", .{});
} }
upload_result = if (json_output) blk: { upload_result = if (json_output) blk: {
// JSON mode: clean output only // JSON mode: clean output only
break :blk speed_tester.measure_upload_speed_stability(urls, criteria) catch |err| { break :blk speed_tester.measure_upload_speed_stability(urls, criteria) catch |err| {
log.err("Upload test failed: {}", .{err}); try spinner.fail("Upload test failed: {}", .{err});
try outputJson(download_result.speed.value, latency_ms, null, "Upload test failed"); try outputJson(ctx.writer, download_result.speed.value, latency_ms, null, "Upload test failed");
return; return;
}; };
} else blk: { } else blk: {
// Interactive mode with spinner updates // Interactive mode with spinner updates
const uploadProgressCallback = progress.createCallback(ctx.spinner, updateUploadSpinnerText); const uploadProgressCallback = progress.createCallback(spinner, updateUploadSpinnerText);
break :blk speed_tester.measureUploadSpeedWithStabilityProgress(urls, criteria, uploadProgressCallback) catch |err| { break :blk speed_tester.measureUploadSpeedWithStabilityProgress(urls, criteria, uploadProgressCallback) catch |err| {
try ctx.spinner.fail("Upload test failed: {}", .{err}); try spinner.fail("Upload test failed: {}", .{err});
return; return;
}; };
}; };
@ -166,36 +168,34 @@ fn run(ctx: zli.CommandContext) !void {
if (!json_output) { if (!json_output) {
if (latency_ms) |ping| { if (latency_ms) |ping| {
if (upload_result) |up| { if (upload_result) |up| {
try ctx.spinner.succeed("🏓 {d:.0}ms | ⬇️ Download: {d:.1} {s} | ⬆️ Upload: {d:.1} {s}", .{ ping, download_result.speed.value, download_result.speed.unit.toString(), up.speed.value, up.speed.unit.toString() }); try spinner.succeed("🏓 {d:.0}ms | ⬇️ Download: {d:.1} {s} | ⬆️ Upload: {d:.1} {s}", .{ ping, download_result.speed.value, download_result.speed.unit.toString(), up.speed.value, up.speed.unit.toString() });
} else { } else {
try ctx.spinner.succeed("🏓 {d:.0}ms | ⬇️ Download: {d:.1} {s}", .{ ping, download_result.speed.value, download_result.speed.unit.toString() }); try spinner.succeed("🏓 {d:.0}ms | ⬇️ Download: {d:.1} {s}", .{ ping, download_result.speed.value, download_result.speed.unit.toString() });
} }
} else { } else {
if (upload_result) |up| { if (upload_result) |up| {
try ctx.spinner.succeed("⬇️ Download: {d:.1} {s} | ⬆️ Upload: {d:.1} {s}", .{ download_result.speed.value, download_result.speed.unit.toString(), up.speed.value, up.speed.unit.toString() }); try spinner.succeed("⬇️ Download: {d:.1} {s} | ⬆️ Upload: {d:.1} {s}", .{ download_result.speed.value, download_result.speed.unit.toString(), up.speed.value, up.speed.unit.toString() });
} else { } else {
try ctx.spinner.succeed("⬇️ Download: {d:.1} {s}", .{ download_result.speed.value, download_result.speed.unit.toString() }); try spinner.succeed("⬇️ Download: {d:.1} {s}", .{ download_result.speed.value, download_result.speed.unit.toString() });
} }
} }
} else { } else {
const upload_speed = if (upload_result) |up| up.speed.value else null; const upload_speed = if (upload_result) |up| up.speed.value else null;
try outputJson(download_result.speed.value, latency_ms, upload_speed, null); try outputJson(ctx.writer, download_result.speed.value, latency_ms, upload_speed, null);
} }
} }
/// Update spinner text with current speed measurement /// Update spinner text with current speed measurement
fn updateSpinnerText(spinner: anytype, measurement: SpeedMeasurement) void { fn updateSpinnerText(spinner: anytype, measurement: SpeedMeasurement) void {
spinner.updateText("⬇️ {d:.1} {s}", .{ measurement.value, measurement.unit.toString() }) catch {}; spinner.updateMessage("⬇️ {d:.1} {s}", .{ measurement.value, measurement.unit.toString() }) catch {};
} }
/// Update spinner text with current upload speed measurement /// Update spinner text with current upload speed measurement
fn updateUploadSpinnerText(spinner: anytype, measurement: SpeedMeasurement) void { fn updateUploadSpinnerText(spinner: anytype, measurement: SpeedMeasurement) void {
spinner.updateText("⬆️ {d:.1} {s}", .{ measurement.value, measurement.unit.toString() }) catch {}; spinner.updateMessage("⬆️ {d:.1} {s}", .{ measurement.value, measurement.unit.toString() }) catch {};
} }
fn outputJson(download_mbps: ?f64, ping_ms: ?f64, upload_mbps: ?f64, error_message: ?[]const u8) !void { fn outputJson(writer: *Writer, download_mbps: ?f64, ping_ms: ?f64, upload_mbps: ?f64, error_message: ?[]const u8) !void {
const stdout = std.io.getStdOut().writer();
var download_buf: [32]u8 = undefined; var download_buf: [32]u8 = undefined;
var ping_buf: [32]u8 = undefined; var ping_buf: [32]u8 = undefined;
var upload_buf: [32]u8 = undefined; var upload_buf: [32]u8 = undefined;
@ -206,5 +206,5 @@ fn outputJson(download_mbps: ?f64, ping_ms: ?f64, upload_mbps: ?f64, error_messa
const upload_str = if (upload_mbps) |u| try std.fmt.bufPrint(&upload_buf, "{d:.1}", .{u}) else "null"; const upload_str = if (upload_mbps) |u| try std.fmt.bufPrint(&upload_buf, "{d:.1}", .{u}) else "null";
const error_str = if (error_message) |e| try std.fmt.bufPrint(&error_buf, "\"{s}\"", .{e}) else "null"; const error_str = if (error_message) |e| try std.fmt.bufPrint(&error_buf, "\"{s}\"", .{e}) else "null";
try stdout.print("{{\"download_mbps\": {s}, \"ping_ms\": {s}, \"upload_mbps\": {s}, \"error\": {s}}}\n", .{ download_str, ping_str, upload_str, error_str }); try writer.print("{{\"download_mbps\": {s}, \"ping_ms\": {s}, \"upload_mbps\": {s}, \"error\": {s}}}\n", .{ download_str, ping_str, upload_str, error_str });
} }

View file

@ -107,7 +107,7 @@ test "BandwidthMeter bandwidth calculation" {
meter.update_total(1000); // 1000 bytes meter.update_total(1000); // 1000 bytes
// Sleep briefly to ensure time passes // Sleep briefly to ensure time passes
std.time.sleep(std.time.ns_per_ms * 10); // 10ms std.Thread.sleep(std.time.ns_per_ms * 10); // 10ms
const bw = meter.bandwidth(); const bw = meter.bandwidth();
try testing.expect(bw > 0); try testing.expect(bw > 0);
@ -127,7 +127,7 @@ test "BandwidthMeter unit conversion" {
// Test different speed ranges // Test different speed ranges
meter._bytes_transferred = 1000; meter._bytes_transferred = 1000;
meter._timer = try std.time.Timer.start(); meter._timer = try std.time.Timer.start();
std.time.sleep(std.time.ns_per_s); // 1 second std.Thread.sleep(std.time.ns_per_s); // 1 second
const measurement = meter.bandwidthWithUnits(); const measurement = meter.bandwidthWithUnits();

View file

@ -71,7 +71,7 @@ pub const Fast = struct {
var result = try Fast.parse_response_urls(json_data.items, allocator); var result = try Fast.parse_response_urls(json_data.items, allocator);
return result.toOwnedSlice(); return result.toOwnedSlice(allocator);
} }
/// Sanitizes JSON data by replacing invalid UTF-8 bytes that cause parseFromSlice to fail. /// Sanitizes JSON data by replacing invalid UTF-8 bytes that cause parseFromSlice to fail.
@ -102,7 +102,7 @@ pub const Fast = struct {
} }
fn parse_response_urls(json_data: []const u8, result_allocator: std.mem.Allocator) !std.ArrayList([]const u8) { fn parse_response_urls(json_data: []const u8, result_allocator: std.mem.Allocator) !std.ArrayList([]const u8) {
var result = std.ArrayList([]const u8).init(result_allocator); var result = std.ArrayList([]const u8).empty;
const sanitized_json = try sanitize_json(json_data, result_allocator); const sanitized_json = try sanitize_json(json_data, result_allocator);
defer result_allocator.free(sanitized_json); defer result_allocator.free(sanitized_json);
@ -119,7 +119,7 @@ pub const Fast = struct {
for (response.targets) |target| { for (response.targets) |target| {
const url_copy = try result_allocator.dupe(u8, target.url); const url_copy = try result_allocator.dupe(u8, target.url);
try result.append(url_copy); try result.append(result_allocator, url_copy);
} }
return result; return result;
@ -155,13 +155,13 @@ pub const Fast = struct {
} }
fn get_page(self: *Fast, allocator: std.mem.Allocator, url: []const u8) !std.ArrayList(u8) { fn get_page(self: *Fast, allocator: std.mem.Allocator, url: []const u8) !std.ArrayList(u8) {
_ = allocator; var response_body = std.Io.Writer.Allocating.init(allocator);
var response_body = std.ArrayList(u8).init(self.arena.allocator());
const response: http.Client.FetchResult = self.client.fetch(.{ const response: http.Client.FetchResult = self.client.fetch(.{
.method = .GET, .method = .GET,
.location = .{ .url = url }, .location = .{ .url = url },
.response_storage = .{ .dynamic = &response_body }, .response_writer = &response_body.writer,
// .response_storage = .{ .dynamic = &response_body },
}) catch |err| switch (err) { }) catch |err| switch (err) {
error.NetworkUnreachable, error.ConnectionRefused => { error.NetworkUnreachable, error.ConnectionRefused => {
log.err("Failed to reach fast.com servers (network/connection error) for URL: {s}", .{url}); log.err("Failed to reach fast.com servers (network/connection error) for URL: {s}", .{url});
@ -195,7 +195,7 @@ pub const Fast = struct {
log.err("HTTP request failed with status code {}", .{response.status}); log.err("HTTP request failed with status code {}", .{response.status});
return error.HttpRequestFailed; return error.HttpRequestFailed;
} }
return response_body; return response_body.toArrayList();
} }
}; };
@ -210,7 +210,7 @@ test "parse_response_urls_v2" {
for (urls.items) |url| { for (urls.items) |url| {
allocator.free(url); allocator.free(url);
} }
urls.deinit(); urls.deinit(allocator);
} }
try testing.expect(urls.items.len == 2); try testing.expect(urls.items.len == 2);
@ -280,7 +280,7 @@ test "parse_response_without_isp" {
for (urls.items) |url| { for (urls.items) |url| {
allocator.free(url); allocator.free(url);
} }
urls.deinit(); urls.deinit(allocator);
} }
try testing.expect(urls.items.len == 1); try testing.expect(urls.items.len == 1);
@ -298,7 +298,7 @@ test "parse_response_minimal_client" {
for (urls.items) |url| { for (urls.items) |url| {
allocator.free(url); allocator.free(url);
} }
urls.deinit(); urls.deinit(allocator);
} }
try testing.expect(urls.items.len == 1); try testing.expect(urls.items.len == 1);

View file

@ -21,13 +21,13 @@ pub const HttpLatencyTester = struct {
pub fn measureLatency(self: *Self, urls: []const []const u8) !?f64 { pub fn measureLatency(self: *Self, urls: []const []const u8) !?f64 {
if (urls.len == 0) return null; if (urls.len == 0) return null;
var latencies = std.ArrayList(f64).init(self.allocator); var latencies = std.ArrayList(f64).empty;
defer latencies.deinit(); defer latencies.deinit(self.allocator);
// Test each URL // Test each URL
for (urls) |url| { for (urls) |url| {
if (self.measureSingleUrl(url)) |latency_ms| { if (self.measureSingleUrl(url)) |latency_ms| {
try latencies.append(latency_ms); try latencies.append(self.allocator, latency_ms);
} else |_| { } else |_| {
// Ignore errors, continue with other URLs // Ignore errors, continue with other URLs
continue; continue;
@ -54,14 +54,10 @@ pub const HttpLatencyTester = struct {
const server_header_buffer = try self.allocator.alloc(u8, 4096); const server_header_buffer = try self.allocator.alloc(u8, 4096);
defer self.allocator.free(server_header_buffer); defer self.allocator.free(server_header_buffer);
var req = try client.open(.HEAD, uri, .{ var req = try client.request(.HEAD, uri, .{});
.server_header_buffer = server_header_buffer,
});
defer req.deinit(); defer req.deinit();
try req.send(); try req.sendBodiless();
try req.finish();
try req.wait();
} }
// Second request: Reuse connection and measure pure HTTP RTT // Second request: Reuse connection and measure pure HTTP RTT
@ -71,14 +67,10 @@ pub const HttpLatencyTester = struct {
const server_header_buffer = try self.allocator.alloc(u8, 4096); const server_header_buffer = try self.allocator.alloc(u8, 4096);
defer self.allocator.free(server_header_buffer); defer self.allocator.free(server_header_buffer);
var req = try client.open(.HEAD, uri, .{ var req = try client.request(.HEAD, uri, .{});
.server_header_buffer = server_header_buffer,
});
defer req.deinit(); defer req.deinit();
try req.send(); try req.sendBodiless();
try req.finish();
try req.wait();
} }
const end_time = std.time.nanoTimestamp(); const end_time = std.time.nanoTimestamp();

View file

@ -154,7 +154,7 @@ pub const HTTPSpeedTester = struct {
// Main measurement loop // Main measurement loop
while (strategy.shouldContinue(timer.timer_interface().read())) { while (strategy.shouldContinue(timer.timer_interface().read())) {
std.time.sleep(strategy.getSleepInterval()); std.Thread.sleep(strategy.getSleepInterval());
if (has_progress) { if (has_progress) {
const current_bytes = worker_manager.getCurrentDownloadBytes(workers); const current_bytes = worker_manager.getCurrentDownloadBytes(workers);
@ -221,7 +221,7 @@ pub const HTTPSpeedTester = struct {
// Main measurement loop // Main measurement loop
while (strategy.shouldContinue(timer.timer_interface().read())) { while (strategy.shouldContinue(timer.timer_interface().read())) {
std.time.sleep(strategy.getSleepInterval()); std.Thread.sleep(strategy.getSleepInterval());
if (has_progress) { if (has_progress) {
const current_bytes = worker_manager.getCurrentUploadBytes(workers); const current_bytes = worker_manager.getCurrentUploadBytes(workers);
@ -285,7 +285,7 @@ pub const HTTPSpeedTester = struct {
// Main measurement loop // Main measurement loop
while (strategy.shouldContinue(timer.timer_interface().read())) { while (strategy.shouldContinue(timer.timer_interface().read())) {
std.time.sleep(strategy.getSleepInterval()); std.Thread.sleep(strategy.getSleepInterval());
const current_bytes = worker_manager.getCurrentDownloadBytes(workers); const current_bytes = worker_manager.getCurrentDownloadBytes(workers);
@ -359,7 +359,7 @@ pub const HTTPSpeedTester = struct {
// Main measurement loop // Main measurement loop
while (strategy.shouldContinue(timer.timer_interface().read())) { while (strategy.shouldContinue(timer.timer_interface().read())) {
std.time.sleep(strategy.getSleepInterval()); std.Thread.sleep(strategy.getSleepInterval());
const current_bytes = worker_manager.getCurrentUploadBytes(workers); const current_bytes = worker_manager.getCurrentUploadBytes(workers);

View file

@ -31,6 +31,7 @@ pub const StabilityStrategy = struct {
last_sample_time: u64 = 0, last_sample_time: u64 = 0,
last_total_bytes: u64 = 0, last_total_bytes: u64 = 0,
consecutive_stable_checks: u32 = 0, consecutive_stable_checks: u32 = 0,
allocator: std.mem.Allocator,
pub fn init(allocator: std.mem.Allocator, criteria: StabilityCriteria) StabilityStrategy { pub fn init(allocator: std.mem.Allocator, criteria: StabilityCriteria) StabilityStrategy {
return StabilityStrategy{ return StabilityStrategy{
@ -38,12 +39,13 @@ pub const StabilityStrategy = struct {
.ramp_up_duration_ns = @as(u64, criteria.ramp_up_duration_seconds) * std.time.ns_per_s, .ramp_up_duration_ns = @as(u64, criteria.ramp_up_duration_seconds) * std.time.ns_per_s,
.max_duration_ns = @as(u64, criteria.max_duration_seconds) * std.time.ns_per_s, .max_duration_ns = @as(u64, criteria.max_duration_seconds) * std.time.ns_per_s,
.measurement_interval_ns = criteria.measurement_interval_ms * std.time.ns_per_ms, .measurement_interval_ns = criteria.measurement_interval_ms * std.time.ns_per_ms,
.speed_measurements = std.ArrayList(f64).init(allocator), .speed_measurements = std.ArrayList(f64).empty,
.allocator = allocator,
}; };
} }
pub fn deinit(self: *StabilityStrategy) void { pub fn deinit(self: *StabilityStrategy) void {
self.speed_measurements.deinit(); self.speed_measurements.deinit(self.allocator);
} }
pub fn shouldContinue(self: StabilityStrategy, current_time: u64) bool { pub fn shouldContinue(self: StabilityStrategy, current_time: u64) bool {
@ -69,7 +71,7 @@ pub const StabilityStrategy = struct {
// Phase 1: Ramp-up - collect measurements but don't check stability // Phase 1: Ramp-up - collect measurements but don't check stability
if (current_time < self.ramp_up_duration_ns) { if (current_time < self.ramp_up_duration_ns) {
try self.speed_measurements.append(interval_speed); try self.speed_measurements.append(self.allocator, interval_speed);
// Keep sliding window size // Keep sliding window size
if (self.speed_measurements.items.len > self.criteria.sliding_window_size) { if (self.speed_measurements.items.len > self.criteria.sliding_window_size) {
@ -77,7 +79,7 @@ pub const StabilityStrategy = struct {
} }
} else { } else {
// Phase 2: Stabilization - check CoV for stability // Phase 2: Stabilization - check CoV for stability
try self.speed_measurements.append(interval_speed); try self.speed_measurements.append(self.allocator, interval_speed);
// Maintain sliding window // Maintain sliding window
if (self.speed_measurements.items.len > self.criteria.sliding_window_size) { if (self.speed_measurements.items.len > self.criteria.sliding_window_size) {

View file

@ -169,7 +169,7 @@ pub const DownloadWorker = struct {
_ = self.error_count.fetchAdd(1, .monotonic); _ = self.error_count.fetchAdd(1, .monotonic);
break; break;
} }
std.time.sleep(std.time.ns_per_ms * 100); std.Thread.sleep(std.time.ns_per_ms * 100);
continue; continue;
}; };
defer response.deinit(); defer response.deinit();
@ -183,7 +183,7 @@ pub const DownloadWorker = struct {
// Accept both 200 (full content) and 206 (partial content) // Accept both 200 (full content) and 206 (partial content)
if (response.status != .ok and response.status != .partial_content) { if (response.status != .ok and response.status != .partial_content) {
print("Worker {} HTTP error: {}\n", .{ self.config.worker_id, response.status }); print("Worker {} HTTP error: {}\n", .{ self.config.worker_id, response.status });
std.time.sleep(std.time.ns_per_ms * 100); std.Thread.sleep(std.time.ns_per_ms * 100);
continue; continue;
} }
@ -196,7 +196,7 @@ pub const DownloadWorker = struct {
// Small delay between requests // Small delay between requests
if (self.config.delay_between_requests_ms > 0) { if (self.config.delay_between_requests_ms > 0) {
std.time.sleep(std.time.ns_per_ms * self.config.delay_between_requests_ms); std.Thread.sleep(std.time.ns_per_ms * self.config.delay_between_requests_ms);
} }
} }
} }
@ -318,7 +318,7 @@ pub const UploadWorker = struct {
_ = self.error_count.fetchAdd(1, .monotonic); _ = self.error_count.fetchAdd(1, .monotonic);
break; break;
} }
std.time.sleep(std.time.ns_per_ms * 100); std.Thread.sleep(std.time.ns_per_ms * 100);
continue; continue;
}; };
defer response.deinit(); defer response.deinit();
@ -331,7 +331,7 @@ pub const UploadWorker = struct {
if (response.status != .ok) { if (response.status != .ok) {
print("Upload worker {} HTTP error: {}\n", .{ self.config.worker_id, response.status }); print("Upload worker {} HTTP error: {}\n", .{ self.config.worker_id, response.status });
std.time.sleep(std.time.ns_per_ms * 100); std.Thread.sleep(std.time.ns_per_ms * 100);
continue; continue;
} }
@ -404,15 +404,14 @@ pub const RealHttpClient = struct {
fn fetch(ptr: *anyopaque, request: FetchRequest) !FetchResponse { fn fetch(ptr: *anyopaque, request: FetchRequest) !FetchResponse {
const self: *Self = @ptrCast(@alignCast(ptr)); const self: *Self = @ptrCast(@alignCast(ptr));
var response_body = std.ArrayList(u8).init(self.allocator); var response_body = std.Io.Writer.Allocating.init(self.allocator);
errdefer response_body.deinit(); errdefer response_body.deinit();
const fetch_options = http.Client.FetchOptions{ const fetch_options = http.Client.FetchOptions{
.method = request.method, .method = request.method,
.location = .{ .url = request.url }, .location = .{ .url = request.url },
.payload = if (request.payload) |p| p else null, .payload = if (request.payload) |p| p else null,
.response_storage = .{ .dynamic = &response_body }, .response_writer = &response_body.writer,
.max_append_size = request.max_response_size,
}; };
const result = try self.client.fetch(fetch_options); const result = try self.client.fetch(fetch_options);
@ -505,7 +504,7 @@ pub const MockHttpClient = struct {
_ = request; _ = request;
if (self.delay_ms > 0) { if (self.delay_ms > 0) {
std.time.sleep(std.time.ns_per_ms * self.delay_ms); std.Thread.sleep(std.time.ns_per_ms * self.delay_ms);
} }
if (self.should_fail) { if (self.should_fail) {
@ -611,7 +610,7 @@ test "DownloadWorker basic functionality" {
const thread = try std.Thread.spawn(.{}, DownloadWorker.run, .{&worker}); const thread = try std.Thread.spawn(.{}, DownloadWorker.run, .{&worker});
// Let it run for a bit // Let it run for a bit
std.time.sleep(std.time.ns_per_ms * 100); std.Thread.sleep(std.time.ns_per_ms * 100);
// Advance timer to trigger stop // Advance timer to trigger stop
mock_timer.setTime(std.time.ns_per_s * 3); // 3 seconds mock_timer.setTime(std.time.ns_per_s * 3); // 3 seconds

View file

@ -1,4 +1,5 @@
const std = @import("std"); const std = @import("std");
const cli = @import("cli/root.zig"); const cli = @import("cli/root.zig");
pub const std_options: std.Options = .{ pub const std_options: std.Options = .{
@ -11,8 +12,14 @@ pub const std_options: std.Options = .{
pub fn main() !void { pub fn main() !void {
const allocator = std.heap.smp_allocator; const allocator = std.heap.smp_allocator;
var root = try cli.build(allocator);
const file = std.fs.File.stdout();
var writer = file.writerStreaming(&.{}).interface;
const root = try cli.build(&writer, allocator);
defer root.deinit(); defer root.deinit();
try root.execute(.{}); try root.execute(.{});
try writer.flush();
} }