|
| 1 | +use commonware_coding::{Config, Scheme}; |
| 2 | +use criterion::{criterion_main, BatchSize, Criterion}; |
| 3 | +use rand::{seq::SliceRandom, RngCore, SeedableRng as _}; |
| 4 | +use rand_chacha::ChaCha8Rng; |
| 5 | + |
| 6 | +mod no_coding; |
| 7 | +mod reed_solomon; |
| 8 | + |
| 9 | +pub(crate) fn benchmark_encode_generic<S: Scheme>(name: &str, c: &mut Criterion) { |
| 10 | + let mut rng = ChaCha8Rng::seed_from_u64(0); |
| 11 | + let cases = [8, 12, 16, 19, 20, 24].map(|i| 2usize.pow(i)); |
| 12 | + for data_length in cases.into_iter() { |
| 13 | + for chunks in [10, 25, 50, 100, 250] { |
| 14 | + let min = chunks / 3; |
| 15 | + let config = Config { |
| 16 | + minimum_shards: min as u16, |
| 17 | + extra_shards: (chunks - min) as u16, |
| 18 | + }; |
| 19 | + c.bench_function( |
| 20 | + &format!("{}/msg_len={} chunks={}", name, data_length, chunks), |
| 21 | + |b| { |
| 22 | + b.iter_batched( |
| 23 | + || { |
| 24 | + // Generate random data |
| 25 | + let mut data = vec![0u8; data_length]; |
| 26 | + rng.fill_bytes(&mut data); |
| 27 | + data |
| 28 | + }, |
| 29 | + |data| S::encode(&config, data.as_slice()), |
| 30 | + BatchSize::SmallInput, |
| 31 | + ); |
| 32 | + }, |
| 33 | + ); |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +pub(crate) fn benchmark_decode_generic<S: Scheme>(name: &str, c: &mut Criterion) { |
| 39 | + let mut rng = ChaCha8Rng::seed_from_u64(0); |
| 40 | + let cases = [8, 12, 16, 19, 20, 24].map(|i| 2usize.pow(i)); |
| 41 | + for data_length in cases.into_iter() { |
| 42 | + for chunks in [10, 25, 50, 100, 250] { |
| 43 | + let min = chunks / 3; |
| 44 | + let config = Config { |
| 45 | + minimum_shards: min as u16, |
| 46 | + extra_shards: (chunks - min) as u16, |
| 47 | + }; |
| 48 | + c.bench_function( |
| 49 | + &format!("{}/msg_len={} chunks={}", name, data_length, chunks), |
| 50 | + |b| { |
| 51 | + b.iter_batched( |
| 52 | + || { |
| 53 | + // Generate random data |
| 54 | + let mut data = vec![0u8; data_length]; |
| 55 | + rng.fill_bytes(&mut data); |
| 56 | + |
| 57 | + // Encode data |
| 58 | + let (commitment, mut shards) = |
| 59 | + S::encode(&config, data.as_slice()).unwrap(); |
| 60 | + |
| 61 | + shards.shuffle(&mut rng); |
| 62 | + let my_shard_and_proof = shards.pop().unwrap(); |
| 63 | + let reshards = shards |
| 64 | + .iter() |
| 65 | + .take(min) |
| 66 | + .map(|(shard, proof)| S::check(&commitment, proof, shard).unwrap()) |
| 67 | + .collect::<Vec<_>>(); |
| 68 | + |
| 69 | + (commitment, my_shard_and_proof, reshards) |
| 70 | + }, |
| 71 | + // We include the cost of checking your shard as part of decoding |
| 72 | + |(commitment, (my_shard, my_proof), reshards)| { |
| 73 | + S::check(&commitment, &my_proof, &my_shard).unwrap(); |
| 74 | + S::decode(&config, &commitment, my_shard, &reshards).unwrap(); |
| 75 | + }, |
| 76 | + BatchSize::SmallInput, |
| 77 | + ); |
| 78 | + }, |
| 79 | + ); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +criterion_main!(reed_solomon::benches, no_coding::benches); |
0 commit comments