diff --git a/bench/bench.zig b/bench/bench.zig new file mode 100644 index 0000000..83afc8b --- /dev/null +++ b/bench/bench.zig @@ -0,0 +1,62 @@ +const std = @import("std"); +const vaxis = @import("vaxis"); + +fn parseIterations(allocator: std.mem.Allocator) !usize { + var args = try std.process.argsWithAllocator(allocator); + defer args.deinit(); + _ = args.next(); + if (args.next()) |val| { + return std.fmt.parseUnsigned(usize, val, 10); + } + return 200; +} + +fn printResults(writer: anytype, label: []const u8, iterations: usize, elapsed_ns: u64, total_bytes: usize) !void { + const ns_per_frame = elapsed_ns / @as(u64, @intCast(iterations)); + const bytes_per_frame = total_bytes / iterations; + try writer.print( + "{s}: frames={d} total_ns={d} ns/frame={d} bytes={d} bytes/frame={d}\n", + .{ label, iterations, elapsed_ns, ns_per_frame, total_bytes, bytes_per_frame }, + ); +} + +pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const allocator = gpa.allocator(); + + const iterations = try parseIterations(allocator); + + var vx = try vaxis.init(allocator, .{}); + var init_writer = std.io.Writer.Allocating.init(allocator); + defer init_writer.deinit(); + defer vx.deinit(allocator, &init_writer.writer); + + const winsize = vaxis.Winsize{ .rows = 24, .cols = 80, .x_pixel = 0, .y_pixel = 0 }; + try vx.resize(allocator, &init_writer.writer, winsize); + + const stdout = std.fs.File.stdout().deprecatedWriter(); + + var idle_writer = std.io.Writer.Allocating.init(allocator); + defer idle_writer.deinit(); + var timer = try std.time.Timer.start(); + var i: usize = 0; + while (i < iterations) : (i += 1) { + try vx.render(&idle_writer.writer); + } + const idle_ns = timer.read(); + const idle_bytes: usize = idle_writer.writer.end; + try printResults(stdout, "idle", iterations, idle_ns, idle_bytes); + + var dirty_writer = std.io.Writer.Allocating.init(allocator); + defer dirty_writer.deinit(); + timer.reset(); + i = 0; + while (i < iterations) : (i += 1) { + vx.queueRefresh(); + try vx.render(&dirty_writer.writer); + } + const dirty_ns = timer.read(); + const dirty_bytes: usize = dirty_writer.writer.end; + try printResults(stdout, "dirty", iterations, dirty_ns, dirty_bytes); +} diff --git a/build.zig b/build.zig index 8175193..2265ac5 100644 --- a/build.zig +++ b/build.zig @@ -66,6 +66,25 @@ pub fn build(b: *std.Build) void { const example_run = b.addRunArtifact(example); example_step.dependOn(&example_run.step); + // Benchmarks + const bench_step = b.step("bench", "Run benchmarks"); + const bench = b.addExecutable(.{ + .name = "bench", + .root_module = b.createModule(.{ + .root_source_file = b.path("bench/bench.zig"), + .target = target, + .optimize = optimize, + .imports = &.{ + .{ .name = "vaxis", .module = vaxis_mod }, + }, + }), + }); + const bench_run = b.addRunArtifact(bench); + if (b.args) |args| { + bench_run.addArgs(args); + } + bench_step.dependOn(&bench_run.step); + // Tests const tests_step = b.step("test", "Run tests");