|
| 1 | +const std = @import("std"); |
| 2 | +const bench = @import("bench"); |
| 3 | + |
| 4 | +// Import the advanced features |
| 5 | +const export_mod = @import("export"); |
| 6 | +const comparison_mod = @import("comparison"); |
| 7 | +const memory_profiler = @import("memory_profiler"); |
| 8 | +const ci = @import("ci"); |
| 9 | + |
| 10 | +var global_sum: u64 = 0; |
| 11 | + |
| 12 | +fn fastBenchmark() void { |
| 13 | + var sum: u64 = 0; |
| 14 | + var i: u32 = 0; |
| 15 | + while (i < 100) : (i += 1) { |
| 16 | + sum += i; |
| 17 | + } |
| 18 | + global_sum = sum; |
| 19 | +} |
| 20 | + |
| 21 | +fn mediumBenchmark() void { |
| 22 | + var sum: u64 = 0; |
| 23 | + var i: u32 = 0; |
| 24 | + while (i < 1_000) : (i += 1) { |
| 25 | + sum += i; |
| 26 | + } |
| 27 | + global_sum = sum; |
| 28 | +} |
| 29 | + |
| 30 | +fn slowBenchmark() void { |
| 31 | + var sum: u64 = 0; |
| 32 | + var i: u32 = 0; |
| 33 | + while (i < 10_000) : (i += 1) { |
| 34 | + sum += i; |
| 35 | + } |
| 36 | + global_sum = sum; |
| 37 | +} |
| 38 | + |
| 39 | +fn memoryIntensiveBenchmark(allocator: std.mem.Allocator) void { |
| 40 | + var list = std.ArrayList(u64){}; |
| 41 | + defer list.deinit(allocator); |
| 42 | + |
| 43 | + var i: usize = 0; |
| 44 | + while (i < 1000) : (i += 1) { |
| 45 | + list.append(allocator, i) catch unreachable; |
| 46 | + } |
| 47 | + global_sum = list.items.len; |
| 48 | +} |
| 49 | + |
| 50 | +pub fn main() !void { |
| 51 | + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; |
| 52 | + defer _ = gpa.deinit(); |
| 53 | + const allocator = gpa.allocator(); |
| 54 | + |
| 55 | + // Run benchmarks |
| 56 | + var suite = bench.BenchmarkSuite.init(allocator); |
| 57 | + defer suite.deinit(); |
| 58 | + |
| 59 | + try suite.add("Fast Benchmark", fastBenchmark); |
| 60 | + try suite.add("Medium Benchmark", mediumBenchmark); |
| 61 | + try suite.add("Slow Benchmark", slowBenchmark); |
| 62 | + |
| 63 | + std.debug.print("\n{s}=== Running Benchmarks ==={s}\n", .{ bench.Formatter.BOLD, bench.Formatter.RESET }); |
| 64 | + try suite.run(); |
| 65 | + |
| 66 | + // Note: We can't easily access the results from suite.run(), so we'll run them again |
| 67 | + // In a real implementation, you'd modify BenchmarkSuite.run() to return results |
| 68 | + var results = std.ArrayList(bench.BenchmarkResult){}; |
| 69 | + defer { |
| 70 | + for (results.items) |*result| { |
| 71 | + result.deinit(); |
| 72 | + } |
| 73 | + results.deinit(allocator); |
| 74 | + } |
| 75 | + |
| 76 | + for (suite.benchmarks.items) |*benchmark| { |
| 77 | + const result = try benchmark.run(allocator); |
| 78 | + try results.append(allocator, result); |
| 79 | + } |
| 80 | + |
| 81 | + // 1. Export to JSON |
| 82 | + std.debug.print("\n{s}=== Exporting Results ==={s}\n", .{ bench.Formatter.BOLD, bench.Formatter.RESET }); |
| 83 | + const exporter = export_mod.Exporter.init(allocator); |
| 84 | + try exporter.exportToFile(results.items, "benchmark_results.json", .json); |
| 85 | + std.debug.print("✓ Exported to JSON: benchmark_results.json\n", .{}); |
| 86 | + |
| 87 | + // 2. Export to CSV |
| 88 | + try exporter.exportToFile(results.items, "benchmark_results.csv", .csv); |
| 89 | + std.debug.print("✓ Exported to CSV: benchmark_results.csv\n", .{}); |
| 90 | + |
| 91 | + // 3. Compare with baseline (if exists) |
| 92 | + std.debug.print("\n{s}=== Baseline Comparison ==={s}\n", .{ bench.Formatter.BOLD, bench.Formatter.RESET }); |
| 93 | + |
| 94 | + const baseline_exists = blk: { |
| 95 | + std.fs.cwd().access("baseline.json", .{}) catch break :blk false; |
| 96 | + break :blk true; |
| 97 | + }; |
| 98 | + |
| 99 | + if (baseline_exists) { |
| 100 | + const comparator = comparison_mod.Comparator.init(allocator, 10.0); |
| 101 | + const comparisons = try comparator.compare(results.items, "baseline.json"); |
| 102 | + defer allocator.free(comparisons); |
| 103 | + |
| 104 | + const stdout = std.fs.File.stdout(); |
| 105 | + try comparator.printComparison(stdout, comparisons); |
| 106 | + } else { |
| 107 | + std.debug.print("No baseline found. Run with baseline.json to enable comparison.\n", .{}); |
| 108 | + std.debug.print("Creating baseline from current results...\n", .{}); |
| 109 | + try exporter.exportToFile(results.items, "baseline.json", .json); |
| 110 | + std.debug.print("✓ Baseline created: baseline.json\n", .{}); |
| 111 | + } |
| 112 | + |
| 113 | + // 4. Memory Profiling Demo |
| 114 | + std.debug.print("\n{s}=== Memory Profiling Demo ==={s}\n", .{ bench.Formatter.BOLD, bench.Formatter.RESET }); |
| 115 | + |
| 116 | + var profiling_allocator = memory_profiler.ProfilingAllocator.init(allocator); |
| 117 | + const tracked_allocator = profiling_allocator.allocator(); |
| 118 | + |
| 119 | + // Run a memory-intensive benchmark |
| 120 | + var timer = try std.time.Timer.start(); |
| 121 | + memoryIntensiveBenchmark(tracked_allocator); |
| 122 | + const elapsed = timer.read(); |
| 123 | + |
| 124 | + const mem_result = memory_profiler.MemoryBenchmarkResult{ |
| 125 | + .name = "Memory Intensive Benchmark", |
| 126 | + .time_ns = @floatFromInt(elapsed), |
| 127 | + .memory_stats = profiling_allocator.getStats(), |
| 128 | + }; |
| 129 | + |
| 130 | + const stdout = std.fs.File.stdout(); |
| 131 | + try mem_result.print(stdout); |
| 132 | + |
| 133 | + // 5. CI/CD Integration Demo |
| 134 | + std.debug.print("\n{s}=== CI/CD Integration Demo ==={s}\n", .{ bench.Formatter.BOLD, bench.Formatter.RESET }); |
| 135 | + |
| 136 | + const ci_format = ci.detectCIEnvironment(); |
| 137 | + std.debug.print("Detected CI environment: {s}\n", .{@tagName(ci_format)}); |
| 138 | + |
| 139 | + var ci_helper = ci.CIHelper.init(allocator, .{ |
| 140 | + .fail_on_regression = true, |
| 141 | + .regression_threshold = 10.0, |
| 142 | + .baseline_path = if (baseline_exists) "baseline.json" else null, |
| 143 | + .output_format = ci_format, |
| 144 | + }); |
| 145 | + |
| 146 | + try ci_helper.generateSummary(results.items); |
| 147 | + |
| 148 | + if (baseline_exists) { |
| 149 | + const has_regression = try ci_helper.checkRegressions(results.items); |
| 150 | + if (has_regression and ci_helper.shouldFailBuild(has_regression)) { |
| 151 | + std.debug.print("\n{s}⚠️ Build would fail due to performance regressions{s}\n", .{ bench.Formatter.YELLOW, bench.Formatter.RESET }); |
| 152 | + // In CI, you would: std.process.exit(1); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + std.debug.print("\n{s}✓ Advanced features demo complete!{s}\n", .{ bench.Formatter.GREEN, bench.Formatter.RESET }); |
| 157 | +} |
0 commit comments