BC-4 Bench — statistical benchmarking for Clojure code.
Wave-2 implementation: warmup + N timed iterations, percentile aggregation
(p50/p95/p99/min/max/mean/stddev), :tt/bench-run event emission, and a
defbench macro that wraps deftest with a p95 threshold assertion.
Design notes:
BC-4 Bench — statistical benchmarking for Clojure code.
Wave-2 implementation: warmup + N timed iterations, percentile aggregation
(p50/p95/p99/min/max/mean/stddev), :tt/bench-run event emission, and a
`defbench` macro that wraps `deftest` with a p95 threshold assertion.
Design notes:
* Pure data in, pure data out. BenchResult is a value type.
* Event publishing delegated to hive-ttracking.events/publish, which
swallows throwables so a stalled router never fails a bench.
* Warmup iterations run the thunk but are NOT included in samples —
they let JIT compile hot paths before measurement.(aggregate-samples {:keys [name runs warmup]} samples-ns)Build a BenchResult from a seq of iteration wall-time samples (nanos).
Build a BenchResult from a seq of iteration wall-time samples (nanos).
(bench* {:keys [name runs warmup] :or {runs 30 warmup 3} :as opts} thunk)Run thunk warmup times (discarded) then runs times (measured).
Emits :tt/bench-run and returns a BenchResult.
opts: :name keyword/string — label (required) :runs long — measured iterations (default 30) :warmup long — warmup iterations (default 3)
The returned :samples-ns is a vector of raw nanosecond measurements in execution order — callers computing extra stats can reuse it.
Run `thunk` `warmup` times (discarded) then `runs` times (measured). Emits :tt/bench-run and returns a BenchResult. opts: :name keyword/string — label (required) :runs long — measured iterations (default 30) :warmup long — warmup iterations (default 3) The returned :samples-ns is a vector of raw nanosecond measurements in execution order — callers computing extra stats can reuse it.
(defbench bench-sym opts & body)Define a clojure.test test that runs a bench and asserts a p95 threshold.
opts: :name keyword — bench label (defaults to test sym) :runs long — measured iterations (default 30) :warmup long — warmup iterations (default 3) :threshold-p95-ms double — fail if p95 exceeds this (required) :on-result ref/ns-qualified fn — side effect on BenchResult
Example: (defbench hot-path-bench {:runs 100 :threshold-p95-ms 5.0} (work!))
Expands to a tagged deftest — run via lein test :bench or equivalent.
Define a clojure.test test that runs a bench and asserts a p95 threshold.
opts:
:name keyword — bench label (defaults to test sym)
:runs long — measured iterations (default 30)
:warmup long — warmup iterations (default 3)
:threshold-p95-ms double — fail if p95 exceeds this (required)
:on-result ref/ns-qualified fn — side effect on BenchResult
Example:
(defbench hot-path-bench
{:runs 100 :threshold-p95-ms 5.0}
(work!))
Expands to a tagged deftest — run via `lein test :bench` or equivalent.(aggregate this samples)(compare-benches this base target)(run-iteration this f)cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |