// Benchmark Runner CLI // // This is the main entry point for running performance benchmarks for opentui core components. // // Usage: // zig build bench - Run all benchmarks // zig build bench -- --help - Show help message with available options // // Options: // --mem Show memory statistics after each benchmark // --filter, -f NAME Run only benchmark categories matching NAME (case-insensitive substring) // --bench, -b NAME Run only specific benchmarks matching NAME // --json Output results in JSON format (machine-readable) // --help, -h Display help message and list available benchmarks // // Examples: // zig build bench -- --mem // Run all benchmarks with memory statistics // // zig build bench -- --filter rope // Run only benchmarks with "rope" in their name (Rope Data Structure, Rope Marker Tracking) // // zig build bench -- -f textbuffer --mem // Run TextBuffer benchmarks with memory statistics // // zig build bench -- --filter "edit" // Run EditBuffer Operations benchmarks // // zig build bench -- --bench "ASCII" // Run only benchmarks with "ASCII" in their name // // zig build bench -- --json // Output results in JSON format for CI integration // // Adding New Benchmarks: // 1. Create a new file in bench/ directory (e.g., bench/my_bench.zig) // 2. Export `pub const benchName = "My Benchmark";` // 3. Export `pub fn run(io: std.Io, allocator: std.mem.Allocator, show_mem: bool, bench_filter: ?[]const u8) ![]BenchResult` // 4. Import the module at the top of this file // 5. Add an entry to the `benchmarks` array in main() with your module const std = @import("std"); const bench_utils = @import("bench-utils.zig"); var io_threaded: std.Io.Threaded = .init_single_threaded; pub const io = io_threaded.io(); // Import all benchmark modules const text_buffer_view_bench = @import("bench/text-buffer-view_bench.zig"); const edit_buffer_bench = @import("bench/edit-buffer_bench.zig"); const rope_bench = @import("bench/rope_bench.zig"); const text_buffer_coords_bench = @import("bench/text-buffer-coords_bench.zig"); const text_buffer_tab_width_bench = @import("bench/text-buffer-tab-width_bench.zig"); const styled_text_bench = @import("bench/styled-text_bench.zig"); const buffer_draw_text_buffer_bench = @import("bench/buffer-draw-text-buffer_bench.zig"); const buffer_color_blending_bench = @import("bench/buffer-color-blending_bench.zig"); const buffer_draw_box_bench = @import("bench/buffer-draw-box_bench.zig"); const utf8_bench = @import("bench/utf8_bench.zig"); const text_chunk_render_clusters_bench = @import("bench/text-chunk-render-clusters_bench.zig"); const editor_view_bench = @import("bench/editor-view_bench.zig"); const terminal_image_bench = @import("bench/terminal-image_bench.zig"); const renderer_image_bench = @import("bench/renderer-image_bench.zig"); const buffer_cell_drawing_bench = @import("bench/buffer-cell-drawing_bench.zig"); const renderer_overhead_bench = @import("bench/renderer-overhead_bench.zig"); const buffer_image_overlap_bench = @import("bench/buffer-image-overlap_bench.zig"); const image_operations_bench = @import("bench/image-operations_bench.zig"); const renderer_output_bench = @import("bench/renderer-output_bench.zig"); const scene_frame_bench = @import("bench/scene-frame_bench.zig"); // Only the names of these three are used here; `bench-isolated.zig` compiles their code as its own unit. extern fn opentui_bench_renderer_image(bool, bool, [*]const u8, usize) callconv(.c) bench_utils.UnitStatus; extern fn opentui_bench_buffer_cell_drawing(bool, bool, [*]const u8, usize) callconv(.c) bench_utils.UnitStatus; extern fn opentui_bench_buffer_image_overlap(bool, bool, [*]const u8, usize) callconv(.c) bench_utils.UnitStatus; const BenchModule = struct { name: []const u8, run: ?bench_utils.RunFn = null, unit: ?bench_utils.UnitFn = null, }; fn matchesFilter(bench_name: []const u8, filter: ?[]const u8) bool { if (filter == null) return true; const filter_str = filter.?; if (filter_str.len == 0) return true; var i: usize = 0; while (i + filter_str.len <= bench_name.len) : (i += 1) { var matches = true; for (filter_str, 0..) |filter_char, j| { const bench_char = bench_name[i + j]; const filter_lower = if (filter_char >= 'A' and filter_char <= 'Z') filter_char + 32 else filter_char; const bench_lower = if (bench_char >= 'A' and bench_char <= 'Z') bench_char + 32 else bench_char; if (filter_lower != bench_lower) { matches = false; break; } } if (matches) return true; } return false; } pub fn main(init: std.process.Init) !void { const process_io = init.io; var gpa: std.heap.DebugAllocator(.{}) = .init; defer _ = gpa.deinit(); const allocator = gpa.allocator(); const benchmarks = [_]BenchModule{ .{ .name = text_buffer_view_bench.benchName, .run = text_buffer_view_bench.run }, .{ .name = edit_buffer_bench.benchName, .run = edit_buffer_bench.run }, .{ .name = rope_bench.benchName, .run = rope_bench.run }, .{ .name = text_buffer_coords_bench.benchName, .run = text_buffer_coords_bench.run }, .{ .name = text_buffer_tab_width_bench.benchName, .run = text_buffer_tab_width_bench.run }, .{ .name = styled_text_bench.benchName, .run = styled_text_bench.run }, .{ .name = buffer_draw_text_buffer_bench.benchName, .run = buffer_draw_text_buffer_bench.run }, .{ .name = buffer_color_blending_bench.benchName, .run = buffer_color_blending_bench.run }, .{ .name = buffer_draw_box_bench.benchName, .run = buffer_draw_box_bench.run }, .{ .name = utf8_bench.benchName, .run = utf8_bench.run }, .{ .name = text_chunk_render_clusters_bench.benchName, .run = text_chunk_render_clusters_bench.run }, .{ .name = editor_view_bench.benchName, .run = editor_view_bench.run }, .{ .name = terminal_image_bench.benchName, .run = terminal_image_bench.run }, .{ .name = renderer_image_bench.benchName, .unit = opentui_bench_renderer_image }, .{ .name = buffer_cell_drawing_bench.benchName, .unit = opentui_bench_buffer_cell_drawing }, .{ .name = renderer_overhead_bench.benchName, .run = renderer_overhead_bench.run }, .{ .name = buffer_image_overlap_bench.benchName, .unit = opentui_bench_buffer_image_overlap }, .{ .name = image_operations_bench.benchName, .run = image_operations_bench.run }, .{ .name = renderer_output_bench.benchName, .run = renderer_output_bench.run }, .{ .name = scene_frame_bench.benchName, .run = scene_frame_bench.run }, }; var args_arena = std.heap.ArenaAllocator.init(allocator); defer args_arena.deinit(); const args = try init.minimal.args.toSlice(args_arena.allocator()); var show_mem = false; var json_output = false; var filter: ?[]const u8 = null; var bench_filter: ?[]const u8 = null; var i: usize = 1; while (i < args.len) : (i += 1) { const arg = args[i]; if (std.mem.eql(u8, arg, "--mem")) { show_mem = true; } else if (std.mem.eql(u8, arg, "--json")) { json_output = true; } else if (std.mem.eql(u8, arg, "--filter") or std.mem.eql(u8, arg, "-f")) { if (i + 1 < args.len) { i += 1; filter = args[i]; } } else if (std.mem.eql(u8, arg, "--bench") or std.mem.eql(u8, arg, "-b")) { if (i + 1 < args.len) { i += 1; bench_filter = args[i]; } } else if (std.mem.eql(u8, arg, "--help") or std.mem.eql(u8, arg, "-h")) { var stdout_buffer: [4096]u8 = undefined; var stdout_writer = std.Io.File.stdout().writerStreaming(process_io, &stdout_buffer); const stdout = &stdout_writer.interface; try stdout.print("Usage: bench [options]\n\n", .{}); try stdout.print("Options:\n", .{}); try stdout.print(" --mem Show memory statistics\n", .{}); try stdout.print(" --json Output in JSON format (machine-readable)\n", .{}); try stdout.print(" --filter, -f NAME Run only benchmark categories matching NAME\n", .{}); try stdout.print(" --bench, -b NAME Run only specific benchmarks matching NAME\n", .{}); try stdout.print(" --help, -h Show this help message\n\n", .{}); try stdout.print("Available benchmarks:\n", .{}); for (benchmarks) |bench| { try stdout.print(" - {s}\n", .{bench.name}); } try stdout.flush(); return; } } var stdout_buffer: [4096]u8 = undefined; var stdout_writer = std.Io.File.stdout().writerStreaming(process_io, &stdout_buffer); const stdout = &stdout_writer.interface; if (!json_output and filter != null) { try stdout.print("Filtering benchmarks by: \"{s}\"\n", .{filter.?}); } if (!json_output and bench_filter != null) { try stdout.print("Filtering individual benchmarks by: \"{s}\"\n", .{bench_filter.?}); } var ran_any = false; for (benchmarks) |bench| { if (!matchesFilter(bench.name, filter)) continue; const ran = if (bench.unit) |unit| ran: { try stdout.flush(); const unit_filter = bench_filter orelse ""; break :ran switch (unit(json_output, show_mem, unit_filter.ptr, unit_filter.len)) { .empty => false, .ran => true, .failed => return error.BenchFailed, }; } else try bench_utils.runCategory(process_io, allocator, stdout, bench.name, bench.run.?, json_output, show_mem, bench_filter); if (ran) ran_any = true; } if (!ran_any) { if (!json_output) { if (filter != null and bench_filter != null) { try stdout.print( "\nNo benchmarks matched filters: category=\"{s}\", bench=\"{s}\"\n", .{ filter.?, bench_filter.? }, ); } else if (bench_filter != null) { try stdout.print("\nNo benchmarks matched bench filter: \"{s}\"\n", .{bench_filter.?}); } else if (filter != null) { try stdout.print("\nNo benchmarks matched filter: \"{s}\"\n", .{filter.?}); } else { try stdout.print("\nNo benchmarks ran.\n", .{}); } try stdout.print("Use --help to see available benchmarks.\n", .{}); } try stdout.flush(); return; } if (!json_output) { try stdout.print("\n✓ Benchmarks complete\n", .{}); } try stdout.flush(); }