Skip to content

Commit 5912a18

Browse files
committed
fix: fixed workflow
1 parent 4853bf6 commit 5912a18

File tree

1 file changed

+126
-4
lines changed

1 file changed

+126
-4
lines changed

.github/workflows/performance-benchmark.yml

Lines changed: 126 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,69 @@ jobs:
3636
id: current_benchmark
3737
run: |
3838
echo "Running performance benchmark for current version..."
39-
zig build benchmark -Doptimize=ReleaseFast
40-
./zig-out/bin/hash-zig-benchmark > current_results.txt 2>&1
39+
if zig build benchmark -Doptimize=ReleaseFast; then
40+
./zig-out/bin/hash-zig-benchmark > current_results.txt 2>&1
41+
else
42+
echo "No 'benchmark' step detected; using inline fallback"
43+
cat > benchmark_current.zig << 'EOF'
44+
const std = @import("std");
45+
const hash_zig = @import("hash-zig");
46+
47+
pub fn main() !void {
48+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
49+
defer _ = gpa.deinit();
50+
const allocator = gpa.allocator();
51+
52+
const lifetimes = [_]struct { name: []const u8, lifetime: hash_zig.params.KeyLifetime, expected_time_sec: f64 }{
53+
.{ .name = "2^10", .lifetime = .lifetime_2_10, .expected_time_sec = 30.0 },
54+
.{ .name = "2^16", .lifetime = .lifetime_2_16, .expected_time_sec = 300.0 },
55+
};
56+
57+
for (lifetimes) |config| {
58+
const params = hash_zig.Parameters.init(config.lifetime);
59+
var sig_scheme = try hash_zig.HashSignature.init(allocator, params);
60+
defer sig_scheme.deinit();
61+
62+
const seed: [32]u8 = .{42} ** 32;
63+
const keygen_start = std.time.nanoTimestamp();
64+
var keypair = try sig_scheme.generateKeyPair(allocator, &seed);
65+
const keygen_end = std.time.nanoTimestamp();
66+
defer keypair.deinit(allocator);
67+
68+
const keygen_duration_ns = keygen_end - keygen_start;
69+
const keygen_duration_sec = @as(f64, @floatFromInt(keygen_duration_ns)) / 1_000_000_000.0;
70+
71+
const message = "Performance test message";
72+
const sign_start = std.time.nanoTimestamp();
73+
var signature = try sig_scheme.sign(allocator, message, keypair.secret_key, 0);
74+
const sign_end = std.time.nanoTimestamp();
75+
defer signature.deinit(allocator);
76+
const sign_duration_ns = sign_end - sign_start;
77+
const sign_duration_sec = @as(f64, @floatFromInt(sign_duration_ns)) / 1_000_000_000.0;
78+
79+
const verify_start = std.time.nanoTimestamp();
80+
const is_valid = try sig_scheme.verify(allocator, message, signature, keypair.public_key);
81+
const verify_end = std.time.nanoTimestamp();
82+
_ = is_valid;
83+
const verify_duration_ns = verify_end - verify_start;
84+
const verify_duration_sec = @as(f64, @floatFromInt(verify_duration_ns)) / 1_000_000_000.0;
85+
86+
const tree_height: u32 = config.lifetime.treeHeight();
87+
const num_signatures = @as(usize, 1) << @intCast(tree_height);
88+
const throughput = @as(f64, @floatFromInt(num_signatures)) / keygen_duration_sec;
89+
const performance_ratio = keygen_duration_sec / config.expected_time_sec;
90+
91+
std.debug.print("BENCHMARK_RESULT: {s}:keygen:{d:.6}\n", .{ config.name, keygen_duration_sec });
92+
std.debug.print("BENCHMARK_RESULT: {s}:sign:{d:.6}\n", .{ config.name, sign_duration_sec });
93+
std.debug.print("BENCHMARK_RESULT: {s}:verify:{d:.6}\n", .{ config.name, verify_duration_sec });
94+
std.debug.print("BENCHMARK_RESULT: {s}:throughput:{d:.1}\n", .{ config.name, throughput });
95+
std.debug.print("BENCHMARK_RESULT: {s}:performance_ratio:{d:.2}\n", .{ config.name, performance_ratio });
96+
}
97+
}
98+
EOF
99+
zig build-exe benchmark_current.zig -OReleaseFast --dep hash-zig -Mhash-zig=src/root.zig
100+
./benchmark_current > current_results.txt 2>&1
101+
fi
41102
42103
# Extract key metrics
43104
KEYGEN_2_10=$(grep "BENCHMARK_RESULT: 2^10:keygen:" current_results.txt | cut -d: -f3)
@@ -90,8 +151,69 @@ jobs:
90151
id: base_benchmark
91152
run: |
92153
echo "Running performance benchmark for base version..."
93-
zig build benchmark -Doptimize=ReleaseFast
94-
./zig-out/bin/hash-zig-benchmark > base_results.txt 2>&1
154+
if zig build benchmark -Doptimize=ReleaseFast; then
155+
./zig-out/bin/hash-zig-benchmark > base_results.txt 2>&1
156+
else
157+
echo "No 'benchmark' step detected on base; using inline fallback"
158+
cat > benchmark_base.zig << 'EOF'
159+
const std = @import("std");
160+
const hash_zig = @import("hash-zig");
161+
162+
pub fn main() !void {
163+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
164+
defer _ = gpa.deinit();
165+
const allocator = gpa.allocator();
166+
167+
const lifetimes = [_]struct { name: []const u8, lifetime: hash_zig.params.KeyLifetime, expected_time_sec: f64 }{
168+
.{ .name = "2^10", .lifetime = .lifetime_2_10, .expected_time_sec = 30.0 },
169+
.{ .name = "2^16", .lifetime = .lifetime_2_16, .expected_time_sec = 300.0 },
170+
};
171+
172+
for (lifetimes) |config| {
173+
const params = hash_zig.Parameters.init(config.lifetime);
174+
var sig_scheme = try hash_zig.HashSignature.init(allocator, params);
175+
defer sig_scheme.deinit();
176+
177+
const seed: [32]u8 = .{42} ** 32;
178+
const keygen_start = std.time.nanoTimestamp();
179+
var keypair = try sig_scheme.generateKeyPair(allocator, &seed);
180+
const keygen_end = std.time.nanoTimestamp();
181+
defer keypair.deinit(allocator);
182+
183+
const keygen_duration_ns = keygen_end - keygen_start;
184+
const keygen_duration_sec = @as(f64, @floatFromInt(keygen_duration_ns)) / 1_000_000_000.0;
185+
186+
const message = "Performance test message";
187+
const sign_start = std.time.nanoTimestamp();
188+
var signature = try sig_scheme.sign(allocator, message, keypair.secret_key, 0);
189+
const sign_end = std.time.nanoTimestamp();
190+
defer signature.deinit(allocator);
191+
const sign_duration_ns = sign_end - sign_start;
192+
const sign_duration_sec = @as(f64, @floatFromInt(sign_duration_ns)) / 1_000_000_000.0;
193+
194+
const verify_start = std.time.nanoTimestamp();
195+
const is_valid = try sig_scheme.verify(allocator, message, signature, keypair.public_key);
196+
const verify_end = std.time.nanoTimestamp();
197+
_ = is_valid;
198+
const verify_duration_ns = verify_end - verify_start;
199+
const verify_duration_sec = @as(f64, @floatFromInt(verify_duration_ns)) / 1_000_000_000.0;
200+
201+
const tree_height: u32 = config.lifetime.treeHeight();
202+
const num_signatures = @as(usize, 1) << @intCast(tree_height);
203+
const throughput = @as(f64, @floatFromInt(num_signatures)) / keygen_duration_sec;
204+
const performance_ratio = keygen_duration_sec / config.expected_time_sec;
205+
206+
std.debug.print("BENCHMARK_RESULT: {s}:keygen:{d:.6}\n", .{ config.name, keygen_duration_sec });
207+
std.debug.print("BENCHMARK_RESULT: {s}:sign:{d:.6}\n", .{ config.name, sign_duration_sec });
208+
std.debug.print("BENCHMARK_RESULT: {s}:verify:{d:.6}\n", .{ config.name, verify_duration_sec });
209+
std.debug.print("BENCHMARK_RESULT: {s}:throughput:{d:.1}\n", .{ config.name, throughput });
210+
std.debug.print("BENCHMARK_RESULT: {s}:performance_ratio:{d:.2}\n", .{ config.name, performance_ratio });
211+
}
212+
}
213+
EOF
214+
zig build-exe benchmark_base.zig -OReleaseFast --dep hash-zig -Mhash-zig=src/root.zig
215+
./benchmark_base > base_results.txt 2>&1
216+
fi
95217
96218
# Extract key metrics
97219
KEYGEN_2_10=$(grep "BENCHMARK_RESULT: 2^10:keygen:" base_results.txt | cut -d: -f3)

0 commit comments

Comments
 (0)