hendriknielaender/zBench

zbench.run causes zig build test to hang

Closed this issue · 9 comments

Zig Version

0.12.0

Steps to Reproduce and Observed Behavior

in build.zig.zon

.dependencies = .{
        .zbench = .{
            .url = "https://github.com/hendriknielaender/zbench/archive/refs/tags/v0.8.0.tar.gz",
            .hash = "1220a7330b49a17f0fd0dcf40f76f015db5180b3f1a6f52d7043801846795233c490",
        },
    },

std test set up in build.zig

 const unit_tests = b.addTest(.{
        .root_source_file = b.path("src/root.zig"),
        .target = target,
        .optimize = optimize,
    });

  // adding zbench here
    unit_tests.root_module.addImport("zbench", b.dependency(
        "zbench",
        .{
            .target = target,
            .optimize = optimize,
        },
    ).module("zbench"));

    const run_unit_tests = b.addRunArtifact(unit_tests);

    const test_step = b.step("test", "Run unit tests");
    test_step.dependOn(&run_unit_tests.step);

in root.zig

fn exampleBenchmark(allocator: std.mem.Allocator) void {
    for (0..1000) |_| {
        const buf = allocator.alloc(u8, 512) catch @panic("Out of memory");
        defer allocator.free(buf);
    }
}

test "bench example" {
    const zbench = @import("zbench");
    const stdout = std.io.getStdOut().writer();
    var bench = zbench.Benchmark.init(std.testing.allocator, .{});
    defer bench.deinit();

    try bench.add("My Benchmark", exampleBenchmark, .{});

    try stdout.writeAll("\n");
    try bench.run(stdout);
}

when running zig build test zig just hangs after bench.run. when removing bench.run all other tests pass quickly

Expected Behavior

Runs with feedback and completes

note I also tried the latest master version with the same outcome. In case it's worth noting, I'm running this on OSX in case there's anything linux specific about this benchmarking library

zig fetch https://github.com/hendriknielaender/zbench/archive/refs/heads/master.tar.gz --save=zbench

I’ve switched to https://github.com/silversquirl/benchmark.zig/ for the time being

I pulled the example with zbench directly from

var bench = zbench.Benchmark.init(std.testing.allocator, .{});

Note that the zBench examples are install-artifacts, not run-artifacts (see e.g. https://github.com/hendriknielaender/zBench/blob/main/build.zig#L80). TBH, I normally don't use zBench as part of test but run them in a main fn instead. We had a discussion about this here: #5.

Anyways, I still don't understand why the Zig compiler / test runner just hangs, it doesn't crash or tell me anything useful...

Thank. Ill give that a try when I have more time

Closing for now and will reopen if I see the same happening when run as an exe

bens commented

I've had confusing issues like this with sending output to stdout, IIRC zig's build system assumes it can use stdout to communicate in tests and dumping other things to stdout can hang the build. I'd try using stderr instead and seeing how that goes.

and so on...

ah, that actually does sound somewhat familiar. I saw something like that pop up on zig's discord the other day. taking note. thanks! a work around might be something parameterizing in an io.Writer to write the output to with some sensible default

bens commented

You only need to pass std.io.getStdErr().writer() to bench.run() instead of std.io.getStdOut().writer().

FWIW this is still happening on the updated version for Zig 0.13.0

@bens fix works: std.io.getStdErr().writer().