Created
November 8, 2024 01:17
-
-
Save jmcph4/f6a4fd0676be2f96e7f75b3ce9faa6d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use arbitrary::{Arbitrary, Unstructured}; | |
| use criterion::{black_box, criterion_group, criterion_main, Criterion}; | |
| use ironlobe::{ | |
| book::{ | |
| btree_book::{BTreeBook, Metadata}, | |
| Book, | |
| }, | |
| order::PlainOrder, | |
| }; | |
| use rand::{thread_rng, RngCore, SeedableRng}; | |
| use rand::rngs::StdRng; | |
| const SAMPLE_SECS: u64 = 5; | |
| const BUFLEN: usize = 256; | |
| fn make_orders(n: usize) -> Vec<PlainOrder> { | |
| let mut rng = StdRng::seed_from_u64(42); // Deterministic RNG for reproducibility | |
| (0..n) | |
| .map(|_| { | |
| let mut bytes = vec![0u8; BUFLEN]; | |
| rng.fill_bytes(&mut bytes); | |
| let mut unstructured = Unstructured::new(&bytes); | |
| let mut order = PlainOrder::arbitrary(&mut unstructured) | |
| .expect("Failed to generate instance"); | |
| order.price = rng.gen_range(10.0..100.0); // Set realistic price ranges | |
| order | |
| }) | |
| .collect() | |
| } | |
| fn insert_into_book(orders: &Vec<PlainOrder>, book: &mut BTreeBook<PlainOrder>) { | |
| orders.iter().for_each(|x| book.add(x.clone())); // Ensure add can handle references to avoid cloning | |
| } | |
| fn criterion_benchmark(c: &mut Criterion) { | |
| let orders = make_orders(black_box(1000)); | |
| c.bench_function("insert 1000", |b| { | |
| b.iter(|| { | |
| let mut book = BTreeBook::meta(Metadata { | |
| id: 1, | |
| name: "Benchmark Book".to_string(), | |
| ticker: "BENCH".to_string(), | |
| }); | |
| insert_into_book(&orders, &mut book) | |
| }) | |
| }); | |
| } | |
| fn configure_criterion() -> Criterion { | |
| Criterion::default().measurement_time(std::time::Duration::from_secs(SAMPLE_SECS)) | |
| } | |
| criterion_group! { | |
| name = benches; | |
| config = configure_criterion(); | |
| targets = criterion_benchmark, | |
| } | |
| criterion_main!(benches); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment