Stream auction price discovery and benchmark matching workloads
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
//! Single-thread benchmarks. Use the identical file on both revisions.
|
||||
use matching::{Order, OrderBook, Side};
|
||||
use std::{hint::black_box, time::Instant};
|
||||
|
||||
fn order(seq: i64, side: Side, price: i64, remaining: i64) -> Order {
|
||||
Order {
|
||||
seq,
|
||||
side,
|
||||
price,
|
||||
remaining,
|
||||
}
|
||||
}
|
||||
|
||||
fn measure(name: &str, iterations: u64, mut run: impl FnMut() -> usize) {
|
||||
for _ in 0..100 {
|
||||
black_box(run());
|
||||
}
|
||||
let mut samples = Vec::new();
|
||||
let mut fills = 0;
|
||||
for _ in 0..7 {
|
||||
let start = Instant::now();
|
||||
fills = 0;
|
||||
for _ in 0..iterations {
|
||||
fills += black_box(run());
|
||||
}
|
||||
samples.push(start.elapsed().as_secs_f64());
|
||||
}
|
||||
samples.sort_by(f64::total_cmp);
|
||||
println!(
|
||||
"{name}: iterations={iterations} fills/sample={fills} median={:.0} ops/s best={:.0} ops/s",
|
||||
iterations as f64 / samples[3],
|
||||
iterations as f64 / samples[0]
|
||||
);
|
||||
}
|
||||
fn main() {
|
||||
let mut book = OrderBook::default();
|
||||
for seq in 1..=4096 {
|
||||
book.update(order(seq, Side::Sell, 50 + seq % 10, 1));
|
||||
}
|
||||
for (label, price, qty) in [
|
||||
("miss/plan", 49, 1),
|
||||
("one/plan", 50, 1),
|
||||
("sweep32/plan", 55, 32),
|
||||
] {
|
||||
let bid = order(5000, Side::Buy, price, qty);
|
||||
measure(label, 100_000, || {
|
||||
black_box(&book).match_incoming(black_box(&bid)).len()
|
||||
});
|
||||
}
|
||||
let mut seq = 5000;
|
||||
let mut live = OrderBook::default();
|
||||
measure("maker-insert + taker-fill + maker-remove", 100_000, || {
|
||||
seq += 2;
|
||||
live.update(order(seq, Side::Sell, 50, 1));
|
||||
let fills = live.match_incoming(black_box(&order(seq + 1, Side::Buy, 50, 1)));
|
||||
live.update(order(fills[0].ask, Side::Sell, 50, 0));
|
||||
fills.len()
|
||||
});
|
||||
for seq in 4097..=8192 {
|
||||
book.update(order(seq, Side::Buy, 50 + seq % 10, 1));
|
||||
}
|
||||
measure("opening/8192-orders", 100, || {
|
||||
black_box(&book).opening().len()
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user