|
| 1 | +/// Benchmarks for numerical operations. |
| 2 | +use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
| 3 | +use reductionist::error::ActiveStorageError; |
| 4 | +use reductionist::models::{DType, RequestData, Response}; |
| 5 | +use reductionist::operation::Operation; |
| 6 | +use reductionist::operations; |
| 7 | +use reductionist::types::Missing; |
| 8 | +use url::Url; |
| 9 | +// Bring trait into scope to use as_bytes method. |
| 10 | +use zerocopy::AsBytes; |
| 11 | + |
| 12 | +fn get_test_request_data() -> RequestData { |
| 13 | + RequestData { |
| 14 | + source: Url::parse("http://example.com").unwrap(), |
| 15 | + bucket: "bar".to_string(), |
| 16 | + object: "baz".to_string(), |
| 17 | + dtype: DType::Int32, |
| 18 | + byte_order: None, |
| 19 | + offset: None, |
| 20 | + size: None, |
| 21 | + shape: None, |
| 22 | + order: None, |
| 23 | + selection: None, |
| 24 | + compression: None, |
| 25 | + filters: None, |
| 26 | + missing: None, |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +type ExecuteFn = dyn Fn(&RequestData, Vec<u8>) -> Result<Response, ActiveStorageError>; |
| 31 | + |
| 32 | +fn criterion_benchmark(c: &mut Criterion) { |
| 33 | + for size_k in [64, 256, 1024, 4096] { |
| 34 | + let size = size_k * 1024; |
| 35 | + let data: Vec<i64> = (0_i64..size).map(|i| i % 256).collect::<Vec<i64>>(); |
| 36 | + let data: Vec<u8> = data.as_bytes().into(); |
| 37 | + let missings = vec![ |
| 38 | + None, |
| 39 | + Some(Missing::MissingValue(42.into())), |
| 40 | + Some(Missing::MissingValues(vec![42.into()])), |
| 41 | + Some(Missing::ValidMax(128.into())), |
| 42 | + Some(Missing::ValidMin(128.into())), |
| 43 | + Some(Missing::ValidRange(5.into(), 250.into())), |
| 44 | + ]; |
| 45 | + let operations: [(&str, Box<ExecuteFn>); 5] = [ |
| 46 | + ("count", Box::new(operations::Count::execute)), |
| 47 | + ("max", Box::new(operations::Max::execute)), |
| 48 | + ("min", Box::new(operations::Min::execute)), |
| 49 | + ("select", Box::new(operations::Select::execute)), |
| 50 | + ("sum", Box::new(operations::Sum::execute)), |
| 51 | + ]; |
| 52 | + for (op_name, execute) in operations { |
| 53 | + for missing in missings.clone() { |
| 54 | + let name = format!("{}({}, {:?})", op_name, size, missing); |
| 55 | + c.bench_function(&name, |b| { |
| 56 | + b.iter(|| { |
| 57 | + let mut request_data = get_test_request_data(); |
| 58 | + request_data.dtype = DType::Int64; |
| 59 | + request_data.missing = missing.clone(); |
| 60 | + execute(&request_data, black_box(data.clone())).unwrap(); |
| 61 | + }) |
| 62 | + }); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +criterion_group!(benches, criterion_benchmark); |
| 69 | +criterion_main!(benches); |
0 commit comments