2023-09-01 00:26:13 -07:00
|
|
|
#![allow(clippy::arithmetic_side_effects)]
|
2022-01-19 13:58:20 -08:00
|
|
|
#![feature(test)]
|
|
|
|
|
|
|
|
extern crate test;
|
|
|
|
|
|
|
|
use {
|
2022-01-20 12:59:16 -08:00
|
|
|
rand::prelude::*,
|
|
|
|
solana_perf::{
|
2023-03-20 11:04:46 -07:00
|
|
|
deduper::{self, Deduper},
|
2022-01-20 12:59:16 -08:00
|
|
|
packet::{to_packet_batches, PacketBatch},
|
|
|
|
},
|
2022-01-21 19:23:48 -08:00
|
|
|
std::time::Duration,
|
2022-01-19 13:58:20 -08:00
|
|
|
test::Bencher,
|
|
|
|
};
|
|
|
|
|
2022-01-21 19:23:48 -08:00
|
|
|
const NUM: usize = 4096;
|
|
|
|
|
2022-01-20 12:59:16 -08:00
|
|
|
fn test_packet_with_size(size: usize, rng: &mut ThreadRng) -> Vec<u8> {
|
|
|
|
// subtract 8 bytes because the length will get serialized as well
|
|
|
|
(0..size.checked_sub(8).unwrap())
|
|
|
|
.map(|_| rng.gen())
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn do_bench_dedup_packets(bencher: &mut Bencher, mut batches: Vec<PacketBatch>) {
|
|
|
|
// verify packets
|
2023-03-16 09:45:42 -07:00
|
|
|
let mut rng = rand::thread_rng();
|
2023-03-20 11:04:46 -07:00
|
|
|
let mut deduper = Deduper::<2, [u8]>::new(&mut rng, /*num_bits:*/ 63_999_979);
|
2022-01-20 12:59:16 -08:00
|
|
|
bencher.iter(|| {
|
2023-03-20 11:04:46 -07:00
|
|
|
let _ans = deduper::dedup_packets_and_count_discards(&deduper, &mut batches, |_, _, _| ());
|
2023-03-20 06:16:52 -07:00
|
|
|
deduper.maybe_reset(
|
|
|
|
&mut rng,
|
|
|
|
0.001, // false_positive_rate
|
|
|
|
Duration::from_secs(2), // reset_cycle
|
|
|
|
);
|
2022-01-21 19:23:48 -08:00
|
|
|
batches
|
|
|
|
.iter_mut()
|
2022-12-06 03:54:49 -08:00
|
|
|
.for_each(|b| b.iter_mut().for_each(|p| p.meta_mut().set_discard(false)));
|
2022-01-21 19:23:48 -08:00
|
|
|
});
|
2022-01-20 12:59:16 -08:00
|
|
|
}
|
|
|
|
|
2022-01-19 13:58:20 -08:00
|
|
|
#[bench]
|
2022-01-20 12:59:16 -08:00
|
|
|
#[ignore]
|
|
|
|
fn bench_dedup_same_small_packets(bencher: &mut Bencher) {
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let small_packet = test_packet_with_size(128, &mut rng);
|
2022-01-19 13:58:20 -08:00
|
|
|
|
2022-01-20 12:59:16 -08:00
|
|
|
let batches = to_packet_batches(
|
|
|
|
&std::iter::repeat(small_packet)
|
2022-01-21 19:23:48 -08:00
|
|
|
.take(NUM)
|
2022-01-20 12:59:16 -08:00
|
|
|
.collect::<Vec<_>>(),
|
2022-01-19 13:58:20 -08:00
|
|
|
128,
|
|
|
|
);
|
|
|
|
|
2022-01-20 12:59:16 -08:00
|
|
|
do_bench_dedup_packets(bencher, batches);
|
|
|
|
}
|
2022-01-19 13:58:20 -08:00
|
|
|
|
2022-01-20 12:59:16 -08:00
|
|
|
#[bench]
|
|
|
|
#[ignore]
|
|
|
|
fn bench_dedup_same_big_packets(bencher: &mut Bencher) {
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let big_packet = test_packet_with_size(1024, &mut rng);
|
|
|
|
|
|
|
|
let batches = to_packet_batches(
|
2022-01-21 19:23:48 -08:00
|
|
|
&std::iter::repeat(big_packet).take(NUM).collect::<Vec<_>>(),
|
2022-01-20 12:59:16 -08:00
|
|
|
128,
|
|
|
|
);
|
|
|
|
|
|
|
|
do_bench_dedup_packets(bencher, batches);
|
2022-01-19 13:58:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2022-01-20 12:59:16 -08:00
|
|
|
#[ignore]
|
|
|
|
fn bench_dedup_diff_small_packets(bencher: &mut Bencher) {
|
|
|
|
let mut rng = rand::thread_rng();
|
2022-01-19 13:58:20 -08:00
|
|
|
|
2022-01-20 12:59:16 -08:00
|
|
|
let batches = to_packet_batches(
|
2022-01-21 19:23:48 -08:00
|
|
|
&(0..NUM)
|
2022-01-20 12:59:16 -08:00
|
|
|
.map(|_| test_packet_with_size(128, &mut rng))
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
128,
|
|
|
|
);
|
2022-01-19 13:58:20 -08:00
|
|
|
|
2022-01-20 12:59:16 -08:00
|
|
|
do_bench_dedup_packets(bencher, batches);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
#[ignore]
|
|
|
|
fn bench_dedup_diff_big_packets(bencher: &mut Bencher) {
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
|
|
|
|
let batches = to_packet_batches(
|
2022-01-21 19:23:48 -08:00
|
|
|
&(0..NUM)
|
2022-01-20 12:59:16 -08:00
|
|
|
.map(|_| test_packet_with_size(1024, &mut rng))
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
128,
|
|
|
|
);
|
|
|
|
|
|
|
|
do_bench_dedup_packets(bencher, batches);
|
2022-01-19 13:58:20 -08:00
|
|
|
}
|
2022-01-21 19:23:48 -08:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
#[ignore]
|
|
|
|
fn bench_dedup_baseline(bencher: &mut Bencher) {
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
|
|
|
|
let batches = to_packet_batches(
|
|
|
|
&(0..0)
|
|
|
|
.map(|_| test_packet_with_size(128, &mut rng))
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
128,
|
|
|
|
);
|
|
|
|
|
|
|
|
do_bench_dedup_packets(bencher, batches);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
#[ignore]
|
|
|
|
fn bench_dedup_reset(bencher: &mut Bencher) {
|
2023-03-16 09:45:42 -07:00
|
|
|
let mut rng = rand::thread_rng();
|
2023-03-20 11:04:46 -07:00
|
|
|
let mut deduper = Deduper::<2, [u8]>::new(&mut rng, /*num_bits:*/ 63_999_979);
|
2022-01-21 19:23:48 -08:00
|
|
|
bencher.iter(|| {
|
2023-03-20 06:16:52 -07:00
|
|
|
deduper.maybe_reset(
|
|
|
|
&mut rng,
|
|
|
|
0.001, // false_positive_rate
|
|
|
|
Duration::from_millis(0), // reset_cycle
|
|
|
|
);
|
2022-01-21 19:23:48 -08:00
|
|
|
});
|
|
|
|
}
|