|
| 1 | +use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
| 2 | +use bitcell_governance::*; |
| 3 | + |
| 4 | +const CELL: u64 = 100_000_000; |
| 5 | + |
| 6 | +fn bench_submit_proposal(c: &mut Criterion) { |
| 7 | + c.bench_function("submit_proposal", |b| { |
| 8 | + let mut gov = GovernanceManager::new(); |
| 9 | + let mut counter = 0u64; |
| 10 | + |
| 11 | + b.iter(|| { |
| 12 | + counter += 1; |
| 13 | + gov.submit_proposal( |
| 14 | + black_box([1u8; 33]), |
| 15 | + black_box(ProposalType::ParameterChange { |
| 16 | + parameter: format!("param_{}", counter), |
| 17 | + new_value: "value".to_string(), |
| 18 | + }), |
| 19 | + black_box(format!("Proposal {}", counter)), |
| 20 | + black_box(counter), |
| 21 | + ).unwrap() |
| 22 | + }); |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +fn bench_vote_linear(c: &mut Criterion) { |
| 27 | + c.bench_function("vote_linear", |b| { |
| 28 | + let mut gov = GovernanceManager::new(); |
| 29 | + let proposal_id = gov.submit_proposal( |
| 30 | + [1u8; 33], |
| 31 | + ProposalType::ParameterChange { |
| 32 | + parameter: "test".to_string(), |
| 33 | + new_value: "value".to_string(), |
| 34 | + }, |
| 35 | + "Test".to_string(), |
| 36 | + 1000, |
| 37 | + ).unwrap(); |
| 38 | + |
| 39 | + let mut voter_counter = 2u8; |
| 40 | + |
| 41 | + b.iter(|| { |
| 42 | + voter_counter = voter_counter.wrapping_add(1); |
| 43 | + let voter = [voter_counter; 33]; |
| 44 | + gov.vote( |
| 45 | + black_box(proposal_id), |
| 46 | + black_box(voter), |
| 47 | + black_box(true), |
| 48 | + black_box(1000 * CELL), |
| 49 | + black_box(1100), |
| 50 | + ).unwrap() |
| 51 | + }); |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +fn bench_vote_quadratic(c: &mut Criterion) { |
| 56 | + c.bench_function("vote_quadratic", |b| { |
| 57 | + let config = GovernanceConfig { |
| 58 | + voting_method: VotingMethod::Quadratic, |
| 59 | + ..Default::default() |
| 60 | + }; |
| 61 | + let mut gov = GovernanceManager::with_config(config, GuardianSet::new()); |
| 62 | + |
| 63 | + let proposal_id = gov.submit_proposal( |
| 64 | + [1u8; 33], |
| 65 | + ProposalType::ParameterChange { |
| 66 | + parameter: "test".to_string(), |
| 67 | + new_value: "value".to_string(), |
| 68 | + }, |
| 69 | + "Test".to_string(), |
| 70 | + 1000, |
| 71 | + ).unwrap(); |
| 72 | + |
| 73 | + let mut voter_counter = 2u8; |
| 74 | + |
| 75 | + b.iter(|| { |
| 76 | + voter_counter = voter_counter.wrapping_add(1); |
| 77 | + let voter = [voter_counter; 33]; |
| 78 | + gov.vote( |
| 79 | + black_box(proposal_id), |
| 80 | + black_box(voter), |
| 81 | + black_box(true), |
| 82 | + black_box(10000 * CELL), |
| 83 | + black_box(1100), |
| 84 | + ).unwrap() |
| 85 | + }); |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +fn bench_delegation(c: &mut Criterion) { |
| 90 | + c.bench_function("delegate", |b| { |
| 91 | + let mut gov = GovernanceManager::new(); |
| 92 | + let delegatee = [2u8; 33]; |
| 93 | + let mut delegator_counter = 3u8; |
| 94 | + |
| 95 | + b.iter(|| { |
| 96 | + delegator_counter = delegator_counter.wrapping_add(1); |
| 97 | + let delegator = [delegator_counter; 33]; |
| 98 | + gov.delegate( |
| 99 | + black_box(delegator), |
| 100 | + black_box(delegatee), |
| 101 | + black_box(1000 * CELL), |
| 102 | + ).unwrap() |
| 103 | + }); |
| 104 | + }); |
| 105 | +} |
| 106 | + |
| 107 | +fn bench_get_voting_power(c: &mut Criterion) { |
| 108 | + c.bench_function("get_voting_power", |b| { |
| 109 | + let mut gov = GovernanceManager::new(); |
| 110 | + let delegatee = [2u8; 33]; |
| 111 | + |
| 112 | + // Add some delegations |
| 113 | + for i in 0..10 { |
| 114 | + gov.delegate([i; 33], delegatee, 1000 * CELL).unwrap(); |
| 115 | + } |
| 116 | + |
| 117 | + b.iter(|| { |
| 118 | + gov.get_voting_power( |
| 119 | + black_box(&delegatee), |
| 120 | + black_box(5000 * CELL), |
| 121 | + ) |
| 122 | + }); |
| 123 | + }); |
| 124 | +} |
| 125 | + |
| 126 | +fn bench_finalize_proposal(c: &mut Criterion) { |
| 127 | + c.bench_function("finalize_proposal", |b| { |
| 128 | + b.iter_batched( |
| 129 | + || { |
| 130 | + let mut gov = GovernanceManager::new(); |
| 131 | + let proposal_id = gov.submit_proposal( |
| 132 | + [1u8; 33], |
| 133 | + ProposalType::TreasurySpending { |
| 134 | + recipient: [2u8; 33], |
| 135 | + amount: 1000 * CELL, |
| 136 | + reason: "Test".to_string(), |
| 137 | + }, |
| 138 | + "Test".to_string(), |
| 139 | + 1000, |
| 140 | + ).unwrap(); |
| 141 | + |
| 142 | + // Vote with quorum |
| 143 | + gov.vote(proposal_id, [2u8; 33], true, 15000 * CELL, 1100).unwrap(); |
| 144 | + |
| 145 | + (gov, proposal_id) |
| 146 | + }, |
| 147 | + |(mut gov, proposal_id)| { |
| 148 | + gov.finalize_proposal( |
| 149 | + black_box(proposal_id), |
| 150 | + black_box(1000 + 6 * 60 * 60 + 1), |
| 151 | + ).unwrap() |
| 152 | + }, |
| 153 | + criterion::BatchSize::SmallInput, |
| 154 | + ); |
| 155 | + }); |
| 156 | +} |
| 157 | + |
| 158 | +fn bench_integer_sqrt(c: &mut Criterion) { |
| 159 | + c.bench_function("integer_sqrt", |b| { |
| 160 | + b.iter(|| { |
| 161 | + // Test with various values |
| 162 | + for n in [100, 10000, 1000000, 100000000u64] { |
| 163 | + black_box(bitcell_governance::integer_sqrt(n)); |
| 164 | + } |
| 165 | + }); |
| 166 | + }); |
| 167 | +} |
| 168 | + |
| 169 | +criterion_group!( |
| 170 | + benches, |
| 171 | + bench_submit_proposal, |
| 172 | + bench_vote_linear, |
| 173 | + bench_vote_quadratic, |
| 174 | + bench_delegation, |
| 175 | + bench_get_voting_power, |
| 176 | + bench_finalize_proposal, |
| 177 | + bench_integer_sqrt, |
| 178 | +); |
| 179 | +criterion_main!(benches); |
0 commit comments