|
| 1 | +use benchlib::benchmark::run_benchmark_group; |
| 2 | +use std::time::Instant; |
| 3 | +use tokio::runtime::Runtime; |
| 4 | +use std::fs::File; |
| 5 | +use std::io::{self, BufRead, BufReader}; |
| 6 | +use flate2::read::GzDecoder; |
| 7 | + |
| 8 | +async fn total_char_count(reader: BufReader<GzDecoder<File>>,x: &mut usize) { |
| 9 | + for line in reader.lines() { |
| 10 | + *x += line_char_count(line.expect("invalid character")).await; |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +async fn line_char_count(line: String) -> usize { |
| 15 | + let line_count = line.chars().count(); |
| 16 | + line_count |
| 17 | +} |
| 18 | + |
| 19 | +async fn async_operation() -> (usize, u128) { |
| 20 | + let start_time = Instant::now(); |
| 21 | + |
| 22 | + let file = File::open("./collector/runtime-benchmarks/data/sherlock.txt.gz").expect("can't read a file"); |
| 23 | + let decoder = GzDecoder::new(file); |
| 24 | + let reader2 = BufReader::new(decoder); |
| 25 | + let mut total_char = 0; |
| 26 | + total_char_count(reader2, &mut total_char).await; |
| 27 | + |
| 28 | + let end_time = Instant::now(); |
| 29 | + let duration = end_time - start_time; |
| 30 | + (total_char,duration.as_millis()) |
| 31 | +} |
| 32 | + |
| 33 | +fn main() { |
| 34 | + run_benchmark_group(|group| { |
| 35 | + group.register_benchmark("Async", || { |
| 36 | + // This closure should prepare data that will be needed for the benchmark (if any), |
| 37 | + // and then return a closure that will actually be benchmarked/profiled. |
| 38 | + // Create a Tokio runtime |
| 39 | + let rt = Runtime::new().unwrap(); |
| 40 | + move || { |
| 41 | + rt.block_on(async_operation()); |
| 42 | + } |
| 43 | + }); |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | + |
0 commit comments