| 
 | 1 | +#![deny(unused_must_use, unused_imports, bare_trait_objects)]  | 
 | 2 | +use criterion::{criterion_group, criterion_main, Bencher, Criterion};  | 
 | 3 | +use futures::StreamExt;  | 
 | 4 | +use log::*;  | 
 | 5 | +use pcap_async::{Config, Handle, PacketStream};  | 
 | 6 | +use std::path::PathBuf;  | 
 | 7 | + | 
 | 8 | +fn bench_stream_from_large_file(b: &mut Bencher) {  | 
 | 9 | +    let _ = env_logger::try_init();  | 
 | 10 | + | 
 | 11 | +    let pcap_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))  | 
 | 12 | +        .join("resources")  | 
 | 13 | +        .join("4SICS-GeekLounge-151020.pcap");  | 
 | 14 | + | 
 | 15 | +    info!("Benchmarking against {:?}", pcap_path.clone());  | 
 | 16 | + | 
 | 17 | +    b.iter(|| {  | 
 | 18 | +        let rt = tokio::runtime::Runtime::new().expect("Failed to create runtime");  | 
 | 19 | + | 
 | 20 | +        let clone_path = pcap_path.clone();  | 
 | 21 | + | 
 | 22 | +        let handle = Handle::file_capture(clone_path.to_str().expect("No path found"))  | 
 | 23 | +            .expect("No handle created");  | 
 | 24 | + | 
 | 25 | +        let mut cfg = Config::default();  | 
 | 26 | +        cfg.with_max_packets_read(5000);  | 
 | 27 | + | 
 | 28 | +        let packet_provider = PacketStream::new(Config::default(), std::sync::Arc::clone(&handle))  | 
 | 29 | +            .expect("Failed to build");  | 
 | 30 | +        let packets = rt.block_on(packet_provider.collect::<Vec<_>>());  | 
 | 31 | +        let packets: Result<Vec<_>, pcap_async::Error> = packets.into_iter().collect();  | 
 | 32 | +        let packets = packets  | 
 | 33 | +            .expect("Failed to get packets")  | 
 | 34 | +            .iter()  | 
 | 35 | +            .flatten()  | 
 | 36 | +            .count();  | 
 | 37 | + | 
 | 38 | +        handle.interrupt();  | 
 | 39 | + | 
 | 40 | +        assert_eq!(packets, 246137);  | 
 | 41 | +    });  | 
 | 42 | +}  | 
 | 43 | + | 
 | 44 | +fn bench_stream(c: &mut Criterion) {  | 
 | 45 | +    let benchmark = criterion::Benchmark::new("4sics", bench_stream_from_large_file);  | 
 | 46 | + | 
 | 47 | +    c.bench(  | 
 | 48 | +        "stream",  | 
 | 49 | +        benchmark  | 
 | 50 | +            .sample_size(20)  | 
 | 51 | +            .nresamples(1)  | 
 | 52 | +            .measurement_time(std::time::Duration::from_secs(15)),  | 
 | 53 | +    );  | 
 | 54 | +}  | 
 | 55 | + | 
 | 56 | +fn bench_stream_next_from_large_file(b: &mut Bencher) {  | 
 | 57 | +    let _ = env_logger::try_init();  | 
 | 58 | + | 
 | 59 | +    let pcap_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))  | 
 | 60 | +        .join("resources")  | 
 | 61 | +        .join("4SICS-GeekLounge-151020.pcap");  | 
 | 62 | + | 
 | 63 | +    info!("Benchmarking against {:?}", pcap_path.clone());  | 
 | 64 | + | 
 | 65 | +    b.iter(|| {  | 
 | 66 | +        let rt = tokio::runtime::Runtime::new().expect("Failed to create runtime");  | 
 | 67 | + | 
 | 68 | +        let clone_path = pcap_path.clone();  | 
 | 69 | + | 
 | 70 | +        let handle = Handle::file_capture(clone_path.to_str().expect("No path found"))  | 
 | 71 | +            .expect("No handle created");  | 
 | 72 | + | 
 | 73 | +        let mut cfg = Config::default();  | 
 | 74 | +        cfg.with_max_packets_read(5000);  | 
 | 75 | + | 
 | 76 | +        let packet_provider = PacketStream::new(Config::default(), std::sync::Arc::clone(&handle))  | 
 | 77 | +            .expect("Failed to build");  | 
 | 78 | +        let fut_packets = async move {  | 
 | 79 | +            let mut packet_provider = packet_provider.boxed();  | 
 | 80 | +            let mut packets = vec![];  | 
 | 81 | +            while let Some(p) = packet_provider.next().await {  | 
 | 82 | +                let p = p.expect("Could not get packets");  | 
 | 83 | +                packets.extend(p);  | 
 | 84 | +            }  | 
 | 85 | +            packets  | 
 | 86 | +        };  | 
 | 87 | +        let packets = rt.block_on(fut_packets).len();  | 
 | 88 | + | 
 | 89 | +        handle.interrupt();  | 
 | 90 | + | 
 | 91 | +        assert_eq!(packets, 246137);  | 
 | 92 | +    });  | 
 | 93 | +}  | 
 | 94 | + | 
 | 95 | +fn bench_stream_next(c: &mut Criterion) {  | 
 | 96 | +    let benchmark = criterion::Benchmark::new("4sics", bench_stream_next_from_large_file);  | 
 | 97 | + | 
 | 98 | +    c.bench(  | 
 | 99 | +        "stream_next",  | 
 | 100 | +        benchmark  | 
 | 101 | +            .sample_size(20)  | 
 | 102 | +            .nresamples(1)  | 
 | 103 | +            .measurement_time(std::time::Duration::from_secs(15)),  | 
 | 104 | +    );  | 
 | 105 | +}  | 
 | 106 | + | 
 | 107 | +criterion_group!(benches, bench_stream, bench_stream_next);  | 
 | 108 | + | 
 | 109 | +// Benchmark: cargo bench --verbose  | 
 | 110 | + | 
 | 111 | +criterion_main!(benches);  | 
0 commit comments