Repository navigation
benchmark improvements - #34
Merged
Merged
Conversation
`cargo bench --no-run` (which CI runs) failed for two targets: - `order_margin` passed a `usize` to `OrderId`, but `OrderId` only implements `From<u64>`, so the metas could not be built. - `submit_limit_order` passed a `usize` to `Throughput::Elements`, which takes a `u64`. Without this the benchmark suite cannot be built at all, let alone run.
Every benchmark id cost ~9s with criterion's defaults (3s warm up, 5s measurement, 100 samples). The suite had 204 ids, so a full run took ~30 minutes, and 145 of those ids came from linear sweeps whose steps are smaller than the timer noise. Add `benches/common/mod.rs` with: - `group()`: 20 samples, 1s warm up, 2s measurement. 20 samples is twice the minimum criterion allows and is still enough to estimate the mean of a microbenchmark. - `sweep(max)`: log-spaced values 1, 4, 16, ... to `max`. Four to six points show whether a routine is constant, logarithmic or linear; 49 or 99 points only cost wall time. Apply both to every benchmark. The sweeps shrink as follows: order_margin 19 -> 5 per family, check_active_orders 49 -> 5, submit_limit_order 20 -> 5, sorted_orders 9 -> 3 per operation.
Several ids measured something other than their name: - `order_margin`: the neutral/long/short families all inserted the same buy-only book and then called `account.order_margin()`, so all three ids measured the identical call on identical state. The side of the open orders is what those cases are about, so pass it explicitly (buy/sell/mixed). `order_margin` also only reads the cached notional sums, so it is O(1) in the number of active orders; drop its N sweep instead of measuring a constant 13 times. Delete the commented-out `fill_order` bench and its `mprocs.yaml` entry. - `trade_update`: the `cap` loop sliced nothing, so every id iterated over all orders and only the label changed. Slice `&test_bids[..cap]` so the sweep actually varies the work. - `position`: the group wide throughput of 1000 labelled the single-call getters with the wrong unit. Set throughput per family, and drop the `quantity`/`entry_price` ids: they are `getset` field reads. - `submit_market_order`: the exchange, config and contract spec were built inside `b.iter`, so setup was part of the measurement. Build them in an `iter_with_setup` closure instead.
`benches/order_book.rs` was the only bench missing from `Cargo.toml`, so cargo built it as a libtest harness: `cargo bench --bench order_book` printed "running 0 tests" and the benchmark never ran. Add the `[[bench]]` entry with `harness = false`. The file now uses the shared config and a log-spaced sweep (5 ids instead of 99), so enabling it costs seconds instead of minutes. The starting balance had to grow with it: the old sweep died out before the margin ran out, a sweep up to 100 orders at 50..200 price does not, so the account needs enough balance to hold all of them.
The suite only ever filled nothing: `check_active_orders` submits bids at 99 and then feeds a sell trade at 100, and `Trade::fills_order` requires a strict cross (trade price < limit price), so the loop broke on the first order and the whole fill path was unmeasured. That is why the `check_active_orders` sweep is flat (67 ns for 1 order, 84 ns for 100). Add `benches/exchange_fills.rs` with two families over 1..100 resting bids: - `Exchange/update_state_fills`: one trade per fill through `update_state`, i.e. the backtest hot loop: market state update, maintenance margin check, fill, taker fee, PnL settlement, margin reconciliation. - `Exchange/check_active_orders_fills`: one trade whose quantity sweeps the whole book, isolating the fill loop from the market state update. Measured: ~165 ns for one fill, ~9.7M fills/s at 100 resting orders. Also add `common::linear_exchange`, the base-sized linear setup that the new order-path benchmarks share, so the config is not copied a fourth time.
`Exchange::amend_limit_order` had no coverage at all, although it is the composite with the most moving parts: a linear `get_by_id` scan, a removal from the sorted book, the quantity-delta/leaves-quantity branch and a full resubmit through the risk engine. Add `benches/amend_limit_order.rs` with a sweep over the number of resting orders (1..100), amending every order once: - `Exchange/amend_price`: same quantity, different price. - `Exchange/amend_quantity`: same price, larger quantity (the delta branch). Measured: 142 ns per amend on a one order book, 180 ns per amend on a 100 order book, i.e. the scan grows linearly with the book as expected.
The per-operation benchmarks measure one call on a small book. The README claims 25M limit order submissions per second, but no benchmark measured a full order round trip on a realistic book, so the claim cannot be reproduced from the suite. Add `benches/order_lifecycle.rs`: a 100 order passive backlog, then a sweep over 1..100 cycles of submit -> amend -> cancel, with 3 operations per cycle as the throughput unit. Measured: ~7.3-7.8M full submit+amend+cancel operations per second.
MathisWellmann
force-pushed
the
faster-benches
branch
from
November 26, 2026 16:50
b3c3e37 to
7503b96
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.