• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 use capture::pcap;
16 use std::io::Cursor;
17 use std::time::Instant;
18 use tokio;
19 use tokio::io::BufReader;
20 use tokio::runtime::Runtime;
21 
dns_benchmark()22 async fn dns_benchmark() {
23     const DATA: &[u8] = include_bytes!("../../capture/data/dns.cap");
24 
25     let mut reader = BufReader::new(Cursor::new(DATA));
26     let header = pcap::read_file_header(&mut reader).await.unwrap();
27     assert_eq!(header.linktype, pcap::LinkType::Ethernet.into());
28     let mut dns_manager = http_proxy::DnsManager::new();
29     loop {
30         match pcap::read_record(&mut reader).await {
31             Ok((_hdr, record)) => {
32                 dns_manager.add_from_ethernet_slice(&record);
33             }
34             Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
35                 break;
36             }
37             Err(e) => {
38                 println!("Error: {:?}", e);
39                 assert!(false);
40             }
41         }
42     }
43 }
44 
main()45 fn main() {
46     let iterations = 50_000;
47     let rt = Runtime::new().unwrap();
48     let handle = rt.handle();
49     for _ in 0..5 {
50         let time_start = Instant::now();
51         for _ in 0..iterations {
52             handle.block_on(dns_benchmark());
53         }
54         let elapsed_time = time_start.elapsed();
55         println!(
56             "** Time per iteration {}us",
57             (elapsed_time.as_micros() as f64) / (iterations as f64)
58         );
59     }
60 }
61