|
| 1 | +// Copyright (C) 2025 Category Labs, Inc. |
| 2 | +// |
| 3 | +// This program is free software: you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU General Public License as published by |
| 5 | +// the Free Software Foundation, either version 3 of the License, or |
| 6 | +// (at your option) any later version. |
| 7 | +// |
| 8 | +// This program is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU General Public License |
| 14 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +use std::{collections::HashMap, net::SocketAddr, str::FromStr}; |
| 17 | + |
| 18 | +use bytes::Bytes; |
| 19 | +use criterion::{criterion_group, criterion_main, Criterion, Throughput}; |
| 20 | +use itertools::Itertools as _; |
| 21 | +use monad_crypto::certificate_signature::{CertificateSignature, CertificateSignaturePubKey}; |
| 22 | +use monad_dataplane::udp::DEFAULT_SEGMENT_SIZE; |
| 23 | +use monad_raptorcast::{ |
| 24 | + packet, udp, |
| 25 | + util::{BuildTarget, EpochValidators, Redundancy}, |
| 26 | +}; |
| 27 | +use monad_secp::SecpSignature; |
| 28 | +use monad_testutil::signing::get_key; |
| 29 | +use monad_types::{NodeId, Stake}; |
| 30 | + |
| 31 | +const NUM_NODES: usize = 100; |
| 32 | + |
| 33 | +pub fn bench(c: &mut Criterion) { |
| 34 | + bench_build_messages(c, "Raptorcast 128K", 128 * 1024, "raptorcast"); |
| 35 | + bench_build_messages(c, "Raptorcast 2M", 2 * 1024 * 1024, "raptorcast"); |
| 36 | + |
| 37 | + bench_build_messages(c, "Broadcast 128K", 128 * 1024, "broadcast"); |
| 38 | + // 2M broadcast yields more than 65535 packets, so we only |
| 39 | + // benchmark for 128K. |
| 40 | +} |
| 41 | + |
| 42 | +pub fn bench_build_messages(c: &mut Criterion, name: &str, message_size: usize, target: &str) { |
| 43 | + let message: Bytes = vec![123_u8; message_size].into(); |
| 44 | + |
| 45 | + let mut group = c.benchmark_group(name); |
| 46 | + |
| 47 | + let (author, build_target, known_addrs) = match target { |
| 48 | + "raptorcast" => { |
| 49 | + group.throughput(Throughput::Bytes(message_size as u64)); |
| 50 | + setup_raptorcast() |
| 51 | + } |
| 52 | + "broadcast" => { |
| 53 | + group.throughput(Throughput::Bytes((message_size * NUM_NODES) as u64)); |
| 54 | + setup_broadcast() |
| 55 | + } |
| 56 | + _ => panic!("unsupported target"), |
| 57 | + }; |
| 58 | + |
| 59 | + group.bench_function("udp::build_messages", |b| { |
| 60 | + b.iter(|| { |
| 61 | + let _ = udp::build_messages( |
| 62 | + &author, |
| 63 | + DEFAULT_SEGMENT_SIZE, // segment_size |
| 64 | + message.clone(), |
| 65 | + Redundancy::from_u8(2), |
| 66 | + 0, // epoch_no |
| 67 | + 0, // unix_ts_ms |
| 68 | + build_target.clone(), |
| 69 | + &known_addrs, |
| 70 | + ); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + group.bench_function("packet::build_messages", |b| { |
| 75 | + b.iter(|| { |
| 76 | + let _ = packet::build_messages( |
| 77 | + &author, |
| 78 | + DEFAULT_SEGMENT_SIZE, // segment_size |
| 79 | + message.clone(), |
| 80 | + Redundancy::from_u8(2), |
| 81 | + 0, // epoch_no |
| 82 | + 0, // unix_ts_ms |
| 83 | + build_target.clone(), |
| 84 | + &known_addrs, |
| 85 | + &mut rand::thread_rng(), |
| 86 | + ); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + group.finish(); |
| 91 | +} |
| 92 | + |
| 93 | +type ST = SecpSignature; |
| 94 | +type PT = CertificateSignaturePubKey<ST>; |
| 95 | +type KeyPair = <ST as CertificateSignature>::KeyPairType; |
| 96 | + |
| 97 | +fn setup_raptorcast() -> ( |
| 98 | + KeyPair, |
| 99 | + BuildTarget<'static, ST>, |
| 100 | + HashMap<NodeId<PT>, SocketAddr>, |
| 101 | +) { |
| 102 | + let mut keys = (0..(NUM_NODES as u64)).map(get_key::<ST>).collect_vec(); |
| 103 | + |
| 104 | + // leak the value to get a 'static reference |
| 105 | + let validators = Box::leak(Box::new(EpochValidators { |
| 106 | + validators: keys |
| 107 | + .iter() |
| 108 | + .map(|key| (NodeId::new(key.pubkey()), Stake::ONE)) |
| 109 | + .collect(), |
| 110 | + })); |
| 111 | + |
| 112 | + let addr = SocketAddr::from_str("127.0.0.1:9999").unwrap(); |
| 113 | + let known_addresses = keys |
| 114 | + .iter() |
| 115 | + .map(|key| (NodeId::new(key.pubkey()), addr)) |
| 116 | + .collect(); |
| 117 | + |
| 118 | + let author = keys.pop().unwrap(); |
| 119 | + let epoch_validators = validators.view_without(vec![&NodeId::new(author.pubkey())]); |
| 120 | + |
| 121 | + ( |
| 122 | + author, |
| 123 | + BuildTarget::Raptorcast(epoch_validators), |
| 124 | + known_addresses, |
| 125 | + ) |
| 126 | +} |
| 127 | + |
| 128 | +fn setup_broadcast() -> ( |
| 129 | + KeyPair, |
| 130 | + BuildTarget<'static, ST>, |
| 131 | + HashMap<NodeId<PT>, SocketAddr>, |
| 132 | +) { |
| 133 | + let mut keys = (0..100).map(get_key::<ST>).collect_vec(); |
| 134 | + |
| 135 | + // leak the value to get a 'static reference |
| 136 | + let validators = Box::leak(Box::new(EpochValidators { |
| 137 | + validators: keys |
| 138 | + .iter() |
| 139 | + .map(|key| (NodeId::new(key.pubkey()), Stake::ONE)) |
| 140 | + .collect(), |
| 141 | + })); |
| 142 | + |
| 143 | + let addr = SocketAddr::from_str("127.0.0.1:9999").unwrap(); |
| 144 | + let known_addresses = keys |
| 145 | + .iter() |
| 146 | + .map(|key| (NodeId::new(key.pubkey()), addr)) |
| 147 | + .collect(); |
| 148 | + |
| 149 | + let author = keys.pop().unwrap(); |
| 150 | + let epoch_validators = validators.view_without(vec![&NodeId::new(author.pubkey())]); |
| 151 | + |
| 152 | + ( |
| 153 | + author, |
| 154 | + BuildTarget::Broadcast(epoch_validators.into()), |
| 155 | + known_addresses, |
| 156 | + ) |
| 157 | +} |
| 158 | + |
| 159 | +criterion_group!(benches, bench); |
| 160 | +criterion_main!(benches); |
0 commit comments