Bench Harness¶
benchmatrix.bench_harness ¶
Build pytest-benchmark matrices and attach benchmatrix metadata.
pytest-benchmark remains the measurement engine and source of truth for
calibration, timing, statistics, reporting, and JSON export. This module
orchestrates metric-by-implementation-by-case matrices, attaches strict
JSON-safe metadata to benchmark.extra_info, and streams lightweight
invocation progress records. Detailed timing statistics should be read from
pytest-benchmark's terminal report, CSV output, saved runs, or JSON output.
Target functions must be synchronous callables that complete the work to be measured before returning. Async functions are not supported. Lazy return values such as generators, lazy dataframe expressions, query objects, futures, and deferred computation graphs are not forced by this harness; if such objects are returned, the benchmark may measure only object construction.
TargetFunction
module-attribute
¶
TargetFunction: TypeAlias = Callable[..., object]
Synchronous callable measured by pytest-benchmark through benchmatrix.
Target functions must perform the work being measured before returning. Async functions are rejected. Lazy return values are not forced by the harness.
BenchmarkLifecycleHook
module-attribute
¶
BenchmarkLifecycleHook: TypeAlias = Callable[
[BenchmarkHookContext], None
]
Synchronous setup or cleanup hook for one benchmark invocation.
BenchmarkResultValidator
module-attribute
¶
BenchmarkResultValidator: TypeAlias = Callable[
[BenchmarkHookContext, object], None
]
Synchronous correctness hook for one benchmark invocation result.
BenchmarkFixture ¶
Bases: Protocol
pytest-benchmark fixture surface used by benchmatrix.
Attributes:
| Name | Type | Description |
|---|---|---|
extra_info |
MutableMapping[str, object]
|
Mutable metadata attached to pytest-benchmark output. |
Source code in src/benchmatrix/bench_harness.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
__call__ ¶
__call__(
target: Callable[..., T],
*args: object,
**kwargs: object,
) -> T
Benchmark target with pytest-benchmark automatic calibration.
Source code in src/benchmatrix/bench_harness.py
127 128 129 | |
pedantic ¶
pedantic(
target: Callable[..., T],
*,
args: Sequence[object] | None = None,
kwargs: Mapping[str, object] | None = None,
setup: Callable[
[], tuple[Sequence[object], Mapping[str, object]]
]
| None = None,
teardown: Callable[..., object] | None = None,
rounds: int = _DEFAULT_PEDANTIC_ROUNDS,
warmup_rounds: int = _DEFAULT_WARMUP_ROUNDS,
iterations: int = _DEFAULT_PEDANTIC_ITERATIONS,
) -> T
Benchmark target with pytest-benchmark pedantic mode.
Source code in src/benchmatrix/bench_harness.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
BenchmarkConfig
dataclass
¶
Configuration passed from benchmatrix to pytest-benchmark.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pedantic_rounds
|
int
|
Number of pedantic benchmark rounds to request. |
_DEFAULT_PEDANTIC_ROUNDS
|
warmup_rounds
|
int
|
Number of pedantic warmup rounds to request. |
_DEFAULT_WARMUP_ROUNDS
|
pedantic_iterations
|
int
|
Number of function calls per pedantic round when
inputs are reused. This value is intentionally ignored when
|
_DEFAULT_PEDANTIC_ITERATIONS
|
stream_progress
|
bool
|
Whether benchmark helpers should print one progress line per benchmark invocation. |
True
|
before_benchmark
|
BenchmarkLifecycleHook | None
|
Optional synchronous hook called immediately before pytest-benchmark starts an invocation. |
None
|
validate_result
|
BenchmarkResultValidator | None
|
Optional synchronous correctness hook called with the result returned by pytest-benchmark. The hook should raise when the result is invalid. |
None
|
after_benchmark
|
BenchmarkLifecycleHook | None
|
Optional synchronous hook called after result validation, or during cleanup if benchmarking or validation raises. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
pedantic_rounds |
int
|
Number of pedantic benchmark rounds to request. |
warmup_rounds |
int
|
Number of pedantic warmup rounds to request. |
pedantic_iterations |
int
|
Number of function calls per pedantic round when inputs are reused. |
stream_progress |
bool
|
Whether benchmark helpers should print one progress line per benchmark invocation. |
before_benchmark |
BenchmarkLifecycleHook | None
|
Optional untimed setup hook for a benchmark invocation. |
validate_result |
BenchmarkResultValidator | None
|
Optional untimed correctness hook for the returned target result. |
after_benchmark |
BenchmarkLifecycleHook | None
|
Optional untimed cleanup hook for a benchmark invocation. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a timing control has the wrong type, progress output is not boolean, or a configured hook is not callable or is asynchronous. |
ValueError
|
If rounds or iterations are not positive, or if warmup rounds are negative. |
Warning
For tail_latency benchmarks, setting pedantic_iterations above
one means raw samples are per-round averages of multiple calls rather
than individual-call latency samples. The harness emits a runtime
warning for this configuration.
Source code in src/benchmatrix/bench_harness.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |
__post_init__ ¶
__post_init__() -> None
Validate benchmark configuration after initialization.
Source code in src/benchmatrix/bench_harness.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |
BenchmarkCase
dataclass
¶
Named input case and metadata for a pytest-benchmark matrix.
Warning
If fresh_inputs is false, pytest-benchmark may call the target
function repeatedly with the same argument objects. That is appropriate
only when the target function treats its inputs as immutable or when
reuse reflects the workload you want to measure.
If fresh_inputs is true, this harness uses pytest-benchmark
pedantic setup so input construction is setup work rather than timed
target-function work. That avoids accidentally timing input creation,
but it also means the benchmark is not an end-to-end measurement that
includes input construction. To benchmark construction cost, put that
construction inside the target function itself.
When fresh_inputs is true, BenchmarkConfig.pedantic_iterations
is ignored because pytest-benchmark setup mode is used. The harness
emits a runtime warning when a non-default value is ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Human-readable case name used in parameter IDs and metadata. |
required |
make_args
|
Callable[[], tuple[object, ...]]
|
Factory returning positional arguments for the target function. |
_empty_args
|
make_kwargs
|
Callable[[], dict[str, object]]
|
Factory returning keyword arguments for the target function. |
_empty_kwargs
|
work_units
|
float | Callable[[], float] | None
|
Positive logical amount of work performed by one target call. This can represent items, rows, bytes, tokens, records, events, or any other domain-specific unit. |
None
|
work_unit_name
|
str
|
Name of the logical work unit, such as |
_DEFAULT_WORK_UNIT_NAME
|
fresh_inputs
|
bool
|
Whether each benchmark round needs newly created inputs. |
False
|
metadata
|
Mapping[str, object]
|
Additional strict-JSON-renderable metadata describing the
case. Reasonable scalar types such as paths, datetimes, enums, and
NumPy scalars are coerced; unsupported values raise
|
_empty_metadata()
|
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Human-readable case name used in parameter IDs and metadata. |
make_args |
Callable[[], tuple[object, ...]]
|
Factory returning positional arguments for the target function. |
make_kwargs |
Callable[[], dict[str, object]]
|
Factory returning keyword arguments for the target function. |
work_units |
float | Callable[[], float] | None
|
Positive logical amount of work performed by one target call. |
work_unit_name |
str
|
Name of the logical work unit. |
fresh_inputs |
bool
|
Whether each benchmark round needs newly created inputs. |
metadata |
Mapping[str, object]
|
Strict JSON-safe metadata describing the case. |
Source code in src/benchmatrix/bench_harness.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | |
__post_init__ ¶
__post_init__() -> None
Validate benchmark case fields after initialization.
Source code in src/benchmatrix/bench_harness.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | |
make_call ¶
make_call() -> tuple[tuple[object, ...], dict[str, object]]
Return positional and keyword arguments for one target invocation.
Returns:
| Type | Description |
|---|---|
tuple[tuple[object, ...], dict[str, object]]
|
A tuple containing positional arguments and keyword arguments. |
Source code in src/benchmatrix/bench_harness.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | |
work_unit_count ¶
work_unit_count() -> float | None
Return the logical work-unit count for throughput metrics.
Returns:
| Type | Description |
|---|---|
float | None
|
The logical work-unit count, or |
float | None
|
unit count. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the work-unit count is not positive or finite. |
Source code in src/benchmatrix/bench_harness.py
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
from_values
classmethod
¶
from_values(
name: str,
*args: object,
work_units: float | Callable[[], float] | None = None,
work_unit_name: str = _DEFAULT_WORK_UNIT_NAME,
fresh_inputs: bool = False,
copier: Callable[[object], object] | None = None,
metadata: Mapping[str, object] | None = None,
**kwargs: object,
) -> BenchmarkCase
Create a benchmark case from concrete argument values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Case name. |
required |
*args
|
object
|
Positional arguments for the target function. |
()
|
work_units
|
float | Callable[[], float] | None
|
Positive logical amount of work performed by one target call. |
None
|
work_unit_name
|
str
|
Name of the logical work unit, such as |
_DEFAULT_WORK_UNIT_NAME
|
fresh_inputs
|
bool
|
Whether target invocations need fresh inputs. When
true and |
False
|
copier
|
Callable[[object], object] | None
|
Optional copy function applied to each argument value. Use
|
None
|
metadata
|
Mapping[str, object] | None
|
Optional strict-JSON-renderable case metadata. |
None
|
**kwargs
|
object
|
Keyword arguments for the target function. |
{}
|
Returns:
| Type | Description |
|---|---|
BenchmarkCase
|
A configured benchmark case. |
Source code in src/benchmatrix/bench_harness.py
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | |
BenchmarkHookContext
dataclass
¶
Identity and inputs available to benchmark lifecycle hooks.
Attributes:
| Name | Type | Description |
|---|---|---|
metric_name |
MetricName
|
Metric requested for this benchmark invocation. |
implementation_name |
str
|
Name of the implementation under test. |
case_name |
str
|
Matrix case name under test. |
function |
TargetFunction
|
Synchronous target function under test. |
case |
BenchmarkCase
|
Benchmark case definition used by the invocation. |
Source code in src/benchmatrix/bench_harness.py
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | |
BenchmarkInvocationRecord
dataclass
¶
Lightweight record returned after one benchmark invocation.
This record is not a timing result. Timing results come from pytest-benchmark's report, saved runs, CSV output, or JSON output.
Attributes:
| Name | Type | Description |
|---|---|---|
metric_name |
MetricName
|
Metric requested for this benchmark invocation. |
implementation_name |
str
|
Name of the implementation under test. |
case_name |
str
|
Name of the input case under test. |
extra_info |
Mapping[str, object]
|
Strict JSON-safe metadata attached to pytest-benchmark output. Values are limited to JSON primitives, lists, and string-keyed mappings after metadata coercion. The metadata includes benchmatrix producer and schema-version markers. |
Source code in src/benchmatrix/bench_harness.py
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | |
benchmark_single_call_latency ¶
benchmark_single_call_latency(
benchmark: BenchmarkFixture,
implementation_name: str,
function: TargetFunction,
case_name: str,
case: BenchmarkCase,
*,
config: BenchmarkConfig | None = None,
stream: TextIO | None = None,
) -> BenchmarkInvocationRecord
Benchmark single-call latency for one implementation and case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
benchmark
|
BenchmarkFixture
|
Pytest-benchmark fixture instance. |
required |
implementation_name
|
str
|
Name of the implementation under test. |
required |
function
|
TargetFunction
|
Synchronous function implementation to benchmark. The function must complete the measured work before returning. |
required |
case_name
|
str
|
Name of the input case under test. |
required |
case
|
BenchmarkCase
|
Benchmark input case. |
required |
config
|
BenchmarkConfig | None
|
Benchmark harness configuration. Defaults to
|
None
|
stream
|
TextIO | None
|
Stream used for progress output. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
BenchmarkInvocationRecord
|
A lightweight invocation record containing metadata attached to the |
BenchmarkInvocationRecord
|
benchmark. This is not a timing result. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Warning
This measures completed target-function work only. Input construction,
lazy-result consumption, and other setup are excluded unless they occur
inside function.
Source code in src/benchmatrix/bench_harness.py
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | |
benchmark_batch_throughput ¶
benchmark_batch_throughput(
benchmark: BenchmarkFixture,
implementation_name: str,
function: TargetFunction,
case_name: str,
case: BenchmarkCase,
*,
config: BenchmarkConfig | None = None,
stream: TextIO | None = None,
) -> BenchmarkInvocationRecord
Benchmark batch throughput for one implementation and case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
benchmark
|
BenchmarkFixture
|
Pytest-benchmark fixture instance. |
required |
implementation_name
|
str
|
Name of the implementation under test. |
required |
function
|
TargetFunction
|
Synchronous function implementation to benchmark. The function must complete the measured work before returning. |
required |
case_name
|
str
|
Name of the input case under test. |
required |
case
|
BenchmarkCase
|
Benchmark input case. If |
required |
config
|
BenchmarkConfig | None
|
Benchmark harness configuration. Defaults to
|
None
|
stream
|
TextIO | None
|
Stream used for progress output. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
BenchmarkInvocationRecord
|
A lightweight invocation record containing metadata attached to the |
BenchmarkInvocationRecord
|
benchmark. This is not a timing result. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Warning
Throughput is derived from one synchronous target invocation. It does
not model concurrency, saturation, queueing, or service request load.
case.work_units must accurately describe work completed by each
target call.
Source code in src/benchmatrix/bench_harness.py
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 | |
benchmark_tail_latency ¶
benchmark_tail_latency(
benchmark: BenchmarkFixture,
implementation_name: str,
function: TargetFunction,
case_name: str,
case: BenchmarkCase,
*,
config: BenchmarkConfig | None = None,
stream: TextIO | None = None,
) -> BenchmarkInvocationRecord
Benchmark latency distribution for one implementation and case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
benchmark
|
BenchmarkFixture
|
Pytest-benchmark fixture instance. |
required |
implementation_name
|
str
|
Name of the implementation under test. |
required |
function
|
TargetFunction
|
Synchronous function implementation to benchmark. The function must complete the measured work before returning. |
required |
case_name
|
str
|
Name of the input case under test. |
required |
case
|
BenchmarkCase
|
Benchmark input case. |
required |
config
|
BenchmarkConfig | None
|
Benchmark harness configuration. Defaults to
|
None
|
stream
|
TextIO | None
|
Stream used for progress output. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
BenchmarkInvocationRecord
|
A lightweight invocation record containing metadata attached to the |
BenchmarkInvocationRecord
|
benchmark. This is not a timing result. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Warning
This uses pedantic mode. Tail percentiles should be calculated from
pytest-benchmark JSON data values. This is an
implementation-comparison metric, not production p95/p99 latency under
load.
If case.fresh_inputs is false and config.pedantic_iterations is
greater than one, raw samples are per-round averages of multiple calls,
not individual-call latency samples. The harness emits a runtime
warning for that configuration.
Source code in src/benchmatrix/bench_harness.py
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 | |
run_benchmark_metric ¶
run_benchmark_metric(
benchmark: BenchmarkFixture,
metric_name: MetricName,
implementation_name: str,
function: TargetFunction,
case_name: str,
case: BenchmarkCase,
*,
config: BenchmarkConfig | None = None,
stream: TextIO | None = None,
) -> BenchmarkInvocationRecord
Run one benchmark metric for one implementation and case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
benchmark
|
BenchmarkFixture
|
Pytest-benchmark fixture instance. |
required |
metric_name
|
MetricName
|
Metric to benchmark. |
required |
implementation_name
|
str
|
Name of the implementation under test. |
required |
function
|
TargetFunction
|
Synchronous function implementation to benchmark. |
required |
case_name
|
str
|
Name of the input case under test. |
required |
case
|
BenchmarkCase
|
Benchmark input case. |
required |
config
|
BenchmarkConfig | None
|
Benchmark harness configuration. Defaults to
|
None
|
stream
|
TextIO | None
|
Stream used for progress output. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
BenchmarkInvocationRecord
|
A lightweight invocation record containing metadata attached to the |
BenchmarkInvocationRecord
|
benchmark. This is not a timing result. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in src/benchmatrix/bench_harness.py
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 | |
make_benchmark_parameters ¶
make_benchmark_parameters(
implementations: Mapping[str, TargetFunction],
cases: Mapping[str, BenchmarkCase]
| Iterable[BenchmarkCase],
*,
metrics: Iterable[MetricName] | None = None,
) -> list[object]
Create pytest parameters for a metric-by-implementation-by-case matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
implementations
|
Mapping[str, TargetFunction]
|
Mapping from implementation name to target function. |
required |
cases
|
Mapping[str, BenchmarkCase] | Iterable[BenchmarkCase]
|
Mapping or iterable of benchmark input cases. |
required |
metrics
|
Iterable[MetricName] | None
|
Metrics to include in the parameter matrix. Defaults to all supported benchmatrix metrics. |
None
|
Returns:
| Type | Description |
|---|---|
list[object]
|
A list of values suitable for |
Source code in src/benchmatrix/bench_harness.py
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 | |
make_benchmark_test ¶
make_benchmark_test(
implementations: Mapping[str, TargetFunction],
cases: Mapping[str, BenchmarkCase]
| Iterable[BenchmarkCase],
*,
metrics: Iterable[MetricName] | None = None,
config: BenchmarkConfig | None = None,
) -> Callable[..., None]
Create a pytest test function for a complete benchmark matrix.
Assign the returned function to a module-level name beginning with
test_ so pytest collects it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
implementations
|
Mapping[str, TargetFunction]
|
Mapping from implementation name to target function. |
required |
cases
|
Mapping[str, BenchmarkCase] | Iterable[BenchmarkCase]
|
Mapping or iterable of benchmark input cases. |
required |
metrics
|
Iterable[MetricName] | None
|
Metrics to include in the parameter matrix. Defaults to all supported benchmatrix metrics. |
None
|
config
|
BenchmarkConfig | None
|
Benchmark harness configuration. Defaults to
|
None
|
Returns:
| Type | Description |
|---|---|
Callable[..., None]
|
A parametrized pytest test function ready for module-level assignment. |
Source code in src/benchmatrix/bench_harness.py
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 | |
shallow_copy ¶
shallow_copy(value: object) -> object
Return a shallow copy of value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
Value to copy. |
required |
Returns:
| Type | Description |
|---|---|
object
|
A shallow copy of |
Source code in src/benchmatrix/bench_harness.py
921 922 923 924 925 926 927 928 929 930 | |
deep_copy ¶
deep_copy(value: object) -> object
Return a deep copy of value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
Value to copy. |
required |
Returns:
| Type | Description |
|---|---|
object
|
A deep copy of |
Source code in src/benchmatrix/bench_harness.py
933 934 935 936 937 938 939 940 941 942 | |