|  | 
|  | 1 | +use std::{collections::HashMap, net::SocketAddr, str::FromStr}; | 
|  | 2 | + | 
|  | 3 | +use bytes::Bytes; | 
|  | 4 | +use criterion::{criterion_group, criterion_main, Criterion, Throughput}; | 
|  | 5 | +use itertools::Itertools as _; | 
|  | 6 | +use monad_crypto::certificate_signature::{CertificateSignature, CertificateSignaturePubKey}; | 
|  | 7 | +use monad_dataplane::udp::DEFAULT_SEGMENT_SIZE; | 
|  | 8 | +use monad_raptorcast::{ | 
|  | 9 | +    packet, udp, | 
|  | 10 | +    util::{BuildTarget, EpochValidators, Redundancy}, | 
|  | 11 | +}; | 
|  | 12 | +use monad_secp::SecpSignature; | 
|  | 13 | +use monad_testutil::signing::get_key; | 
|  | 14 | +use monad_types::{NodeId, Stake}; | 
|  | 15 | + | 
|  | 16 | +pub fn bench(c: &mut Criterion) { | 
|  | 17 | +    bench_raptorcast(c, "Raptorcast 128K", 128 * 1024, "raptorcast"); | 
|  | 18 | +    bench_raptorcast(c, "Raptorcast 2M", 2 * 1024 * 1024, "raptorcast"); | 
|  | 19 | + | 
|  | 20 | +    bench_raptorcast(c, "Broadcast 128K", 128 * 1024, "broadcast"); | 
|  | 21 | +    // 2M broadcast yields more than 65535 packets, so we only | 
|  | 22 | +    // benchmark for 128K. | 
|  | 23 | +} | 
|  | 24 | + | 
|  | 25 | +pub fn bench_raptorcast(c: &mut Criterion, name: &str, message_size: usize, target: &str) { | 
|  | 26 | +    let message: Bytes = vec![123_u8; message_size].into(); | 
|  | 27 | + | 
|  | 28 | +    let mut group = c.benchmark_group(name); | 
|  | 29 | +    group.throughput(Throughput::Bytes(message_size as u64)); | 
|  | 30 | + | 
|  | 31 | +    let (author, build_target, known_addrs) = match target { | 
|  | 32 | +        "raptorcast" => setup_raptorcast(), | 
|  | 33 | +        "broadcast" => setup_broadcast(), | 
|  | 34 | +        _ => panic!("unsupported target"), | 
|  | 35 | +    }; | 
|  | 36 | + | 
|  | 37 | +    group.bench_function("udp::build_messages", |b| { | 
|  | 38 | +        b.iter(|| { | 
|  | 39 | +            let _ = udp::build_messages( | 
|  | 40 | +                &author, | 
|  | 41 | +                DEFAULT_SEGMENT_SIZE, // segment_size | 
|  | 42 | +                message.clone(), | 
|  | 43 | +                Redundancy::from_u8(2), | 
|  | 44 | +                0, // epoch_no | 
|  | 45 | +                0, // unix_ts_ms | 
|  | 46 | +                build_target.clone(), | 
|  | 47 | +                &known_addrs, | 
|  | 48 | +            ); | 
|  | 49 | +        }); | 
|  | 50 | +    }); | 
|  | 51 | + | 
|  | 52 | +    group.bench_function("packet::build_messages", |b| { | 
|  | 53 | +        let (author, build_target, known_addrs) = setup_raptorcast(); | 
|  | 54 | + | 
|  | 55 | +        b.iter(|| { | 
|  | 56 | +            let _ = packet::build_messages( | 
|  | 57 | +                &author, | 
|  | 58 | +                DEFAULT_SEGMENT_SIZE, // segment_size | 
|  | 59 | +                message.clone(), | 
|  | 60 | +                Redundancy::from_u8(2), | 
|  | 61 | +                0, // epoch_no | 
|  | 62 | +                0, // unix_ts_ms | 
|  | 63 | +                build_target.clone(), | 
|  | 64 | +                &known_addrs, | 
|  | 65 | +                &mut rand::thread_rng(), | 
|  | 66 | +            ); | 
|  | 67 | +        }); | 
|  | 68 | +    }); | 
|  | 69 | +} | 
|  | 70 | + | 
|  | 71 | +type ST = SecpSignature; | 
|  | 72 | +type PT = CertificateSignaturePubKey<ST>; | 
|  | 73 | +type KeyPair = <ST as CertificateSignature>::KeyPairType; | 
|  | 74 | + | 
|  | 75 | +fn setup_raptorcast() -> ( | 
|  | 76 | +    KeyPair, | 
|  | 77 | +    BuildTarget<'static, ST>, | 
|  | 78 | +    HashMap<NodeId<PT>, SocketAddr>, | 
|  | 79 | +) { | 
|  | 80 | +    let mut keys = (0..100).map(get_key::<ST>).collect_vec(); | 
|  | 81 | + | 
|  | 82 | +    // leak the value to get a 'static reference | 
|  | 83 | +    let validators = Box::leak(Box::new(EpochValidators { | 
|  | 84 | +        validators: keys | 
|  | 85 | +            .iter() | 
|  | 86 | +            .map(|key| (NodeId::new(key.pubkey()), Stake::ONE)) | 
|  | 87 | +            .collect(), | 
|  | 88 | +    })); | 
|  | 89 | + | 
|  | 90 | +    let addr = SocketAddr::from_str("127.0.0.1:9999").unwrap(); | 
|  | 91 | +    let known_addresses = keys | 
|  | 92 | +        .iter() | 
|  | 93 | +        .map(|key| (NodeId::new(key.pubkey()), addr)) | 
|  | 94 | +        .collect(); | 
|  | 95 | + | 
|  | 96 | +    let author = keys.pop().unwrap(); | 
|  | 97 | +    let epoch_validators = validators.view_without(vec![&NodeId::new(author.pubkey())]); | 
|  | 98 | + | 
|  | 99 | +    ( | 
|  | 100 | +        author, | 
|  | 101 | +        BuildTarget::Raptorcast(epoch_validators), | 
|  | 102 | +        known_addresses, | 
|  | 103 | +    ) | 
|  | 104 | +} | 
|  | 105 | + | 
|  | 106 | +fn setup_broadcast() -> ( | 
|  | 107 | +    KeyPair, | 
|  | 108 | +    BuildTarget<'static, ST>, | 
|  | 109 | +    HashMap<NodeId<PT>, SocketAddr>, | 
|  | 110 | +) { | 
|  | 111 | +    let mut keys = (0..100).map(get_key::<ST>).collect_vec(); | 
|  | 112 | + | 
|  | 113 | +    // leak the value to get a 'static reference | 
|  | 114 | +    let validators = Box::leak(Box::new(EpochValidators { | 
|  | 115 | +        validators: keys | 
|  | 116 | +            .iter() | 
|  | 117 | +            .map(|key| (NodeId::new(key.pubkey()), Stake::ONE)) | 
|  | 118 | +            .collect(), | 
|  | 119 | +    })); | 
|  | 120 | + | 
|  | 121 | +    let addr = SocketAddr::from_str("127.0.0.1:9999").unwrap(); | 
|  | 122 | +    let known_addresses = keys | 
|  | 123 | +        .iter() | 
|  | 124 | +        .map(|key| (NodeId::new(key.pubkey()), addr)) | 
|  | 125 | +        .collect(); | 
|  | 126 | + | 
|  | 127 | +    let author = keys.pop().unwrap(); | 
|  | 128 | +    let epoch_validators = validators.view_without(vec![&NodeId::new(author.pubkey())]); | 
|  | 129 | + | 
|  | 130 | +    ( | 
|  | 131 | +        author, | 
|  | 132 | +        BuildTarget::Broadcast(epoch_validators.into()), | 
|  | 133 | +        known_addresses, | 
|  | 134 | +    ) | 
|  | 135 | +} | 
|  | 136 | + | 
|  | 137 | +criterion_group!(benches, bench); | 
|  | 138 | +criterion_main!(benches); | 
0 commit comments