|
| 1 | +// ═══════════════════════════════════════════════════════════════════════════════ |
| 2 | +// DePIN NETWORK BENCHMARKS (Standalone) |
| 3 | +// Run: zig run bench-depin.zig |
| 4 | +// Order #100-1 — REAL DEPIN NETWORK TRANSCENDENCE |
| 5 | +// ═══════════════════════════════════════════════════════════════════════════════ |
| 6 | + |
| 7 | +const std = @import("std"); |
| 8 | + |
| 9 | +// Tier multipliers (from Firebird) |
| 10 | +const TIER_MULTIPLIER_FREE: f64 = 1.0; |
| 11 | +const TIER_MULTIPLIER_STAKER: f64 = 1.5; |
| 12 | +const TIER_MULTIPLIER_POWER: f64 = 2.0; |
| 13 | +const TIER_MULTIPLIER_WHALE: f64 = 3.0; |
| 14 | + |
| 15 | +const NodeTier = enum(u8) { |
| 16 | + free = 0, |
| 17 | + staker = 1, |
| 18 | + power = 2, |
| 19 | + whale = 3, |
| 20 | + |
| 21 | + pub fn getMultiplier(self: NodeTier) f64 { |
| 22 | + return switch (self) { |
| 23 | + .free => TIER_MULTIPLIER_FREE, |
| 24 | + .staker => TIER_MULTIPLIER_STAKER, |
| 25 | + .power => TIER_MULTIPLIER_POWER, |
| 26 | + .whale => TIER_MULTIPLIER_WHALE, |
| 27 | + }; |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +pub fn main() !void { |
| 32 | + std.debug.print( |
| 33 | + \\═══════════════════════════════════════════════════════════════ |
| 34 | + \\ DePIN NETWORK BENCHMARKS |
| 35 | + \\ Order #100-1 — REAL DEPIN NETWORK TRANSCENDENCE |
| 36 | + \\═══════════════════════════════════════════════════════════════ |
| 37 | + \\ |
| 38 | + , .{}); |
| 39 | + |
| 40 | + // Benchmark 1: Tier Multiplier |
| 41 | + try benchmarkTierMultiplier(); |
| 42 | + |
| 43 | + // Benchmark 2: Reward Calculation |
| 44 | + try benchmarkRewardCalculation(); |
| 45 | + |
| 46 | + // Benchmark 3: Node Discovery (simulated) |
| 47 | + try benchmarkNodeDiscovery(); |
| 48 | + |
| 49 | + // Benchmark 4: Job Packet JSON |
| 50 | + try benchmarkJobPacketJson(); |
| 51 | + |
| 52 | + std.debug.print( |
| 53 | + \\ |
| 54 | + \\═══════════════════════════════════════════════════════════════ |
| 55 | + \\ SUMMARY |
| 56 | + \\═══════════════════════════════════════════════════════════════ |
| 57 | + \\ |
| 58 | + \\ UDP Port: 9333 (Discovery) |
| 59 | + \\ TCP Port: 9334 (Jobs) |
| 60 | + \\ HTTP Port: 8080 (API) |
| 61 | + \\ |
| 62 | + \\ Target Metrics: |
| 63 | + \\ - UDP Latency: < 10ms |
| 64 | + \\ - TCP Job Dist: < 50ms |
| 65 | + \\ - Reward Calc: > 100M ops/s |
| 66 | + \\ - Cluster Save: > 100 ops/s |
| 67 | + \\ |
| 68 | + \\═══════════════════════════════════════════════════════════════ |
| 69 | + \\ |
| 70 | + , .{}); |
| 71 | +} |
| 72 | + |
| 73 | +fn benchmarkTierMultiplier() !void { |
| 74 | + const iterations = 100_000_000; |
| 75 | + |
| 76 | + const start = std.time.nanoTimestamp(); |
| 77 | + var total: f64 = 0; |
| 78 | + var i: usize = 0; |
| 79 | + while (i < iterations) : (i += 1) { |
| 80 | + const tier: NodeTier = @enumFromInt(@as(u8, @intCast(i % 4))); |
| 81 | + total += tier.getMultiplier(); |
| 82 | + } |
| 83 | + const elapsed_ns = std.time.nanoTimestamp() - start; |
| 84 | + const elapsed_ms = @as(f64, @floatFromInt(elapsed_ns)) / 1_000_000.0; |
| 85 | + const throughput = @as(f64, @floatFromInt(iterations)) / (elapsed_ms / 1000.0); |
| 86 | + |
| 87 | + std.debug.print( |
| 88 | + \\[1/4] Tier Multiplier Lookup |
| 89 | + \\ Iterations: {d} |
| 90 | + \\ Time: {d:.2} ms |
| 91 | + \\ Throughput: {d:.0} ops/s |
| 92 | + \\ Avg Multiplier: {d:.3}x |
| 93 | + \\ |
| 94 | + , .{ iterations, elapsed_ms, throughput, total / @as(f64, @floatFromInt(iterations)) }); |
| 95 | + |
| 96 | + if (throughput > 50_000_000) { |
| 97 | + std.debug.print(" Status: ✓ FAST\n\n", .{}); |
| 98 | + } else { |
| 99 | + std.debug.print(" Status: ✗ SLOW\n\n", .{}); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +fn benchmarkRewardCalculation() !void { |
| 104 | + const iterations = 10_000_000; |
| 105 | + |
| 106 | + const start = std.time.nanoTimestamp(); |
| 107 | + var total: f64 = 0; |
| 108 | + var i: usize = 0; |
| 109 | + while (i < iterations) : (i += 1) { |
| 110 | + const tier: NodeTier = @enumFromInt(@as(u8, @intCast(i % 4))); |
| 111 | + const base_reward: f64 = 0.001; |
| 112 | + total += base_reward * tier.getMultiplier(); |
| 113 | + } |
| 114 | + const elapsed_ns = std.time.nanoTimestamp() - start; |
| 115 | + const elapsed_ms = @as(f64, @floatFromInt(elapsed_ns)) / 1_000_000.0; |
| 116 | + const throughput = @as(f64, @floatFromInt(iterations)) / (elapsed_ms / 1000.0); |
| 117 | + |
| 118 | + std.debug.print( |
| 119 | + \\[2/4] Reward Calculation |
| 120 | + \\ Iterations: {d} |
| 121 | + \\ Time: {d:.2} ms |
| 122 | + \\ Throughput: {d:.0} ops/s |
| 123 | + \\ Avg Reward: {d:.6} TRI |
| 124 | + \\ |
| 125 | + , .{ iterations, elapsed_ms, throughput, total / @as(f64, @floatFromInt(iterations)) }); |
| 126 | + |
| 127 | + if (throughput > 100_000_000) { |
| 128 | + std.debug.print(" Status: ✓ EXCELLENT\n\n", .{}); |
| 129 | + } else { |
| 130 | + std.debug.print(" Status: ✗ NEEDS OPTIMIZATION\n\n", .{}); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +fn benchmarkNodeDiscovery() !void { |
| 135 | + const iterations = 1_000_000; |
| 136 | + |
| 137 | + const start = std.time.nanoTimestamp(); |
| 138 | + var i: usize = 0; |
| 139 | + while (i < iterations) : (i += 1) { |
| 140 | + // Simulate node discovery packet creation |
| 141 | + const node_id = i; |
| 142 | + const tier: NodeTier = @enumFromInt(@as(u8, @intCast(i % 4))); |
| 143 | + const multiplier = tier.getMultiplier(); |
| 144 | + _ = node_id; |
| 145 | + _ = multiplier; |
| 146 | + } |
| 147 | + const elapsed_ns = std.time.nanoTimestamp() - start; |
| 148 | + const elapsed_ms = @as(f64, @floatFromInt(elapsed_ns)) / 1_000_000.0; |
| 149 | + const throughput = @as(f64, @floatFromInt(iterations)) / (elapsed_ms / 1000.0); |
| 150 | + |
| 151 | + std.debug.print( |
| 152 | + \\[3/4] Node Discovery (Simulated) |
| 153 | + \\ Iterations: {d} |
| 154 | + \\ Time: {d:.2} ms |
| 155 | + \\ Throughput: {d:.0} nodes/s |
| 156 | + \\ |
| 157 | + , .{ iterations, elapsed_ms, throughput }); |
| 158 | + |
| 159 | + if (throughput > 10_000_000) { |
| 160 | + std.debug.print(" Status: ✓ FAST\n\n", .{}); |
| 161 | + } else { |
| 162 | + std.debug.print(" Status: ✗ SLOW\n\n", .{}); |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +fn benchmarkJobPacketJson() !void { |
| 167 | + const iterations = 100_000; |
| 168 | + const allocator = std.heap.page_allocator; |
| 169 | + |
| 170 | + var i: usize = 0; |
| 171 | + const start = std.time.nanoTimestamp(); |
| 172 | + var total_size: usize = 0; |
| 173 | + while (i < iterations) : (i += 1) { |
| 174 | + const json = try std.fmt.allocPrint(allocator, |
| 175 | + \\{{"job_id":"job-{d}","payload":"compute","reward":{d:.6}}} |
| 176 | + , .{ i, 0.001 }); |
| 177 | + defer allocator.free(json); |
| 178 | + total_size += json.len; |
| 179 | + } |
| 180 | + const elapsed_ns = std.time.nanoTimestamp() - start; |
| 181 | + const elapsed_ms = @as(f64, @floatFromInt(elapsed_ns)) / 1_000_000.0; |
| 182 | + const throughput = @as(f64, @floatFromInt(iterations)) / (elapsed_ms / 1000.0); |
| 183 | + const avg_size = @as(f64, @floatFromInt(total_size)) / @as(f64, @floatFromInt(iterations)); |
| 184 | + |
| 185 | + std.debug.print( |
| 186 | + \\[4/4] Job Packet JSON Serialization |
| 187 | + \\ Iterations: {d} |
| 188 | + \\ Time: {d:.2} ms |
| 189 | + \\ Throughput: {d:.0} packets/s |
| 190 | + \\ Avg Size: {d:.0} bytes |
| 191 | + \\ |
| 192 | + , .{ iterations, elapsed_ms, throughput, avg_size }); |
| 193 | + |
| 194 | + if (throughput > 100_000) { |
| 195 | + std.debug.print(" Status: ✓ FAST\n\n", .{}); |
| 196 | + } else { |
| 197 | + std.debug.print(" Status: ✗ SLOW\n\n", .{}); |
| 198 | + } |
| 199 | +} |
0 commit comments