pytest-probability 0.1.0__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/PKG-INFO +25 -28
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/README.md +24 -27
- pytest_probability-0.2.0/docs/cases.md +152 -0
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/docs/changelog.md +25 -0
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/docs/conf.py +2 -2
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/docs/contributing.md +1 -1
- pytest_probability-0.2.0/docs/cost.md +109 -0
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/docs/index.md +13 -14
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/docs/installation.md +2 -2
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/docs/internals.md +46 -32
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/docs/json-report.md +20 -22
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/docs/parametrization.md +3 -4
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/docs/quickstart.md +11 -14
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/docs/reference.md +26 -31
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/pyproject.toml +1 -1
- pytest_probability-0.2.0/src/pytest_probability/__init__.py +6 -0
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/src/pytest_probability/plugin.py +173 -123
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/tests/test_plugin.py +174 -104
- pytest_probability-0.1.0/docs/cases.md +0 -182
- pytest_probability-0.1.0/docs/cost.md +0 -101
- pytest_probability-0.1.0/src/pytest_probability/__init__.py +0 -6
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/.gitignore +0 -0
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/.readthedocs.yaml +0 -0
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/LICENSE +0 -0
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/docs/requirements.txt +0 -0
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/docs/running.md +0 -0
- {pytest_probability-0.1.0 → pytest_probability-0.2.0}/tests/conftest.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pytest-probability
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: pytest plugin for nondeterministic tests: run cases N times, report empirical pass fractions, flaky detection, and cost.
|
|
5
5
|
Project-URL: Homepage, https://github.com/xu-hao/pytest-probability
|
|
6
6
|
Project-URL: Repository, https://github.com/xu-hao/pytest-probability
|
|
@@ -40,11 +40,11 @@ fraction per case, so `7/10 FLAKY` stops hiding inside a green checkmark.
|
|
|
40
40
|
$ pytest benchmarks/ --prob-runs=10
|
|
41
41
|
...
|
|
42
42
|
================================= probability ==================================
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
43
|
+
classify::is_question 10/10 $0.0020
|
|
44
|
+
classify::identify_pii 7/10 $0.0020 FLAKY
|
|
45
|
+
classify::extract_amount 0/10 $0.0020 FAIL
|
|
46
|
+
triage::refund-terse 8/10 $0.0010 FLAKY
|
|
47
|
+
triage::refund-chain_of_thought 10/10 $0.0040
|
|
48
48
|
|
|
49
49
|
Overall: 35/50 passed (70%)
|
|
50
50
|
Cost: $0.0110
|
|
@@ -76,14 +76,13 @@ and pytest 7.4+.
|
|
|
76
76
|
|
|
77
77
|
Benchmark files are named `bench_*.py`, and every `bench_*` function in
|
|
78
78
|
them is a benchmark — the same convention pytest applies to `test_*`.
|
|
79
|
-
|
|
79
|
+
**A bench function is a pytest test body**: cases are stock
|
|
80
80
|
`@pytest.mark.parametrize`, values arrive as function arguments, and
|
|
81
|
-
the
|
|
81
|
+
the body asserts:
|
|
82
82
|
|
|
83
83
|
```python
|
|
84
|
-
import time
|
|
85
84
|
import pytest
|
|
86
|
-
from pytest_probability import
|
|
85
|
+
from pytest_probability import record_cost
|
|
87
86
|
|
|
88
87
|
@pytest.mark.parametrize("text,expected", [
|
|
89
88
|
pytest.param("is this a question?", "question", id="is_question"),
|
|
@@ -91,16 +90,17 @@ from pytest_probability import StepResult
|
|
|
91
90
|
marks=pytest.mark.fast),
|
|
92
91
|
])
|
|
93
92
|
def bench_classify(text, expected):
|
|
94
|
-
t0 = time.time()
|
|
95
93
|
answer = my_classifier(text)
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
passed=(answer == expected),
|
|
99
|
-
elapsed=time.time() - t0,
|
|
100
|
-
cost=0.0002, # optional — summed into the report
|
|
101
|
-
)
|
|
94
|
+
record_cost(0.0002) # optional — summed into the report
|
|
95
|
+
assert answer == expected
|
|
102
96
|
```
|
|
103
97
|
|
|
98
|
+
A run's class falls straight out of Python: a clean return **passes**,
|
|
99
|
+
an `AssertionError` **fails** (wrong answer — with pytest's full
|
|
100
|
+
assertion introspection, `assert 'other' == 'pii'`), any other
|
|
101
|
+
exception **errors** (broken harness). The plugin times each run
|
|
102
|
+
itself.
|
|
103
|
+
|
|
104
104
|
Every parameter combination is a case with its own fraction row,
|
|
105
105
|
namespaced by function (`classify::identify_pii`); items collect as
|
|
106
106
|
`bench_x.py::bench_classify::identify_pii[run3]`. `pytest.param`
|
|
@@ -125,7 +125,7 @@ thresholds) on their own axes and each combination gets its own row:
|
|
|
125
125
|
|
|
126
126
|
```python
|
|
127
127
|
import pytest
|
|
128
|
-
from pytest_probability import
|
|
128
|
+
from pytest_probability import record_usage
|
|
129
129
|
|
|
130
130
|
@pytest.mark.parametrize("style", ["terse", "chain_of_thought"])
|
|
131
131
|
@pytest.mark.parametrize("text", [
|
|
@@ -133,19 +133,16 @@ from pytest_probability import StepResult, TokenUsage
|
|
|
133
133
|
])
|
|
134
134
|
def bench_triage(text, style):
|
|
135
135
|
response = my_classifier(text, prompt_style=style)
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
output_tokens=response.output_tokens,
|
|
142
|
-
cost=response.cost)],
|
|
143
|
-
)
|
|
136
|
+
record_usage(model=response.model,
|
|
137
|
+
input_tokens=response.input_tokens,
|
|
138
|
+
output_tokens=response.output_tokens,
|
|
139
|
+
cost=response.cost) # survives a failing assert
|
|
140
|
+
assert response.answer == "billing"
|
|
144
141
|
```
|
|
145
142
|
|
|
146
143
|
```
|
|
147
|
-
|
|
148
|
-
|
|
144
|
+
triage::refund-terse 8/10 $0.0010 FLAKY
|
|
145
|
+
triage::refund-chain_of_thought 10/10 $0.0040
|
|
149
146
|
```
|
|
150
147
|
|
|
151
148
|
## Options
|
|
@@ -16,11 +16,11 @@ fraction per case, so `7/10 FLAKY` stops hiding inside a green checkmark.
|
|
|
16
16
|
$ pytest benchmarks/ --prob-runs=10
|
|
17
17
|
...
|
|
18
18
|
================================= probability ==================================
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
classify::is_question 10/10 $0.0020
|
|
20
|
+
classify::identify_pii 7/10 $0.0020 FLAKY
|
|
21
|
+
classify::extract_amount 0/10 $0.0020 FAIL
|
|
22
|
+
triage::refund-terse 8/10 $0.0010 FLAKY
|
|
23
|
+
triage::refund-chain_of_thought 10/10 $0.0040
|
|
24
24
|
|
|
25
25
|
Overall: 35/50 passed (70%)
|
|
26
26
|
Cost: $0.0110
|
|
@@ -52,14 +52,13 @@ and pytest 7.4+.
|
|
|
52
52
|
|
|
53
53
|
Benchmark files are named `bench_*.py`, and every `bench_*` function in
|
|
54
54
|
them is a benchmark — the same convention pytest applies to `test_*`.
|
|
55
|
-
|
|
55
|
+
**A bench function is a pytest test body**: cases are stock
|
|
56
56
|
`@pytest.mark.parametrize`, values arrive as function arguments, and
|
|
57
|
-
the
|
|
57
|
+
the body asserts:
|
|
58
58
|
|
|
59
59
|
```python
|
|
60
|
-
import time
|
|
61
60
|
import pytest
|
|
62
|
-
from pytest_probability import
|
|
61
|
+
from pytest_probability import record_cost
|
|
63
62
|
|
|
64
63
|
@pytest.mark.parametrize("text,expected", [
|
|
65
64
|
pytest.param("is this a question?", "question", id="is_question"),
|
|
@@ -67,16 +66,17 @@ from pytest_probability import StepResult
|
|
|
67
66
|
marks=pytest.mark.fast),
|
|
68
67
|
])
|
|
69
68
|
def bench_classify(text, expected):
|
|
70
|
-
t0 = time.time()
|
|
71
69
|
answer = my_classifier(text)
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
passed=(answer == expected),
|
|
75
|
-
elapsed=time.time() - t0,
|
|
76
|
-
cost=0.0002, # optional — summed into the report
|
|
77
|
-
)
|
|
70
|
+
record_cost(0.0002) # optional — summed into the report
|
|
71
|
+
assert answer == expected
|
|
78
72
|
```
|
|
79
73
|
|
|
74
|
+
A run's class falls straight out of Python: a clean return **passes**,
|
|
75
|
+
an `AssertionError` **fails** (wrong answer — with pytest's full
|
|
76
|
+
assertion introspection, `assert 'other' == 'pii'`), any other
|
|
77
|
+
exception **errors** (broken harness). The plugin times each run
|
|
78
|
+
itself.
|
|
79
|
+
|
|
80
80
|
Every parameter combination is a case with its own fraction row,
|
|
81
81
|
namespaced by function (`classify::identify_pii`); items collect as
|
|
82
82
|
`bench_x.py::bench_classify::identify_pii[run3]`. `pytest.param`
|
|
@@ -101,7 +101,7 @@ thresholds) on their own axes and each combination gets its own row:
|
|
|
101
101
|
|
|
102
102
|
```python
|
|
103
103
|
import pytest
|
|
104
|
-
from pytest_probability import
|
|
104
|
+
from pytest_probability import record_usage
|
|
105
105
|
|
|
106
106
|
@pytest.mark.parametrize("style", ["terse", "chain_of_thought"])
|
|
107
107
|
@pytest.mark.parametrize("text", [
|
|
@@ -109,19 +109,16 @@ from pytest_probability import StepResult, TokenUsage
|
|
|
109
109
|
])
|
|
110
110
|
def bench_triage(text, style):
|
|
111
111
|
response = my_classifier(text, prompt_style=style)
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
output_tokens=response.output_tokens,
|
|
118
|
-
cost=response.cost)],
|
|
119
|
-
)
|
|
112
|
+
record_usage(model=response.model,
|
|
113
|
+
input_tokens=response.input_tokens,
|
|
114
|
+
output_tokens=response.output_tokens,
|
|
115
|
+
cost=response.cost) # survives a failing assert
|
|
116
|
+
assert response.answer == "billing"
|
|
120
117
|
```
|
|
121
118
|
|
|
122
119
|
```
|
|
123
|
-
|
|
124
|
-
|
|
120
|
+
triage::refund-terse 8/10 $0.0010 FLAKY
|
|
121
|
+
triage::refund-chain_of_thought 10/10 $0.0040
|
|
125
122
|
```
|
|
126
123
|
|
|
127
124
|
## Options
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# Benchmark files
|
|
2
|
+
|
|
3
|
+
A benchmark file is an ordinary Python module whose name matches the
|
|
4
|
+
`prob_pattern` ini option (default `bench_*.py`). Inside it, **every
|
|
5
|
+
module-level function whose name starts with `bench_` is a benchmark**
|
|
6
|
+
— the same convention pytest applies to `test_*` functions in
|
|
7
|
+
`test_*.py` files. Point pytest at a directory or at the file itself.
|
|
8
|
+
|
|
9
|
+
## The module contract
|
|
10
|
+
|
|
11
|
+
`bench_*(**params)` functions
|
|
12
|
+
: Each is an **ordinary test body**: it takes its case's values as
|
|
13
|
+
arguments, does the work, and `assert`s the outcome. Cases come from
|
|
14
|
+
plain `@pytest.mark.parametrize` on the function — every parameter
|
|
15
|
+
combination is one case. A function with no parametrize marks is a
|
|
16
|
+
single case named after the function. See {doc}`parametrization`.
|
|
17
|
+
|
|
18
|
+
`setup()` / `teardown()` *(optional)*
|
|
19
|
+
: Called once per file: `setup()` before the file's first item runs,
|
|
20
|
+
`teardown()` after its last — shared by all `bench_*` functions in
|
|
21
|
+
the file. If `setup()` raises, the file's items error out
|
|
22
|
+
individually with that exception.
|
|
23
|
+
|
|
24
|
+
A file matching the pattern with no `bench_*` functions is silently
|
|
25
|
+
skipped — zero items, not an error. A file that raises on import is
|
|
26
|
+
reported as a pytest collection error, with the traceback.
|
|
27
|
+
|
|
28
|
+
```{note}
|
|
29
|
+
Benchmark files are not test modules: pytest fixtures and
|
|
30
|
+
`conftest.py` fixtures do not apply (see {doc}`internals` for why). If
|
|
31
|
+
you need per-run resources, create them inside the bench function;
|
|
32
|
+
per-file resources belong in `setup()`/`teardown()`.
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Writing a benchmark
|
|
36
|
+
|
|
37
|
+
If you can write a pytest test, you already know the syntax:
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import pytest
|
|
41
|
+
from pytest_probability import record_cost
|
|
42
|
+
|
|
43
|
+
@pytest.mark.parametrize("text,expected", [
|
|
44
|
+
pytest.param("my ssn is 078-05-1120", "pii", id="identify_pii",
|
|
45
|
+
marks=pytest.mark.fast),
|
|
46
|
+
pytest.param("is this a question?", "question", id="is_question"),
|
|
47
|
+
])
|
|
48
|
+
def bench_classify(text, expected):
|
|
49
|
+
answer = my_classifier(text)
|
|
50
|
+
record_cost(0.0002) # optional, see Cost
|
|
51
|
+
assert answer == expected, f"got '{answer}'"
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
- **Values are function arguments** — named, hintable, refactorable.
|
|
55
|
+
- **`pytest.param(id=...)`** names the case; without it, pytest's id
|
|
56
|
+
rules apply (value text for scalars, `argnameN` for objects,
|
|
57
|
+
`ids=` list/callable supported).
|
|
58
|
+
- **`pytest.param(marks=...)`** tags a single case for `-m` selection
|
|
59
|
+
or skips/xfails it; marks on the function apply to every case.
|
|
60
|
+
- **Stacked decorators** cross-product with pytest's exact id layout —
|
|
61
|
+
`refund-terse`, `refund-chain_of_thought`, bottom decorator leftmost.
|
|
62
|
+
- **No parametrize at all** → the function runs as one case named
|
|
63
|
+
after itself (`bench_smoke` → case `smoke`), which makes
|
|
64
|
+
`--prob-runs` a repeat-runner for plain functions.
|
|
65
|
+
|
|
66
|
+
Each case gets its own summary row, namespaced by the function's short
|
|
67
|
+
name so identical ids in different benchmarks never collide:
|
|
68
|
+
|
|
69
|
+
```text
|
|
70
|
+
classify::identify_pii 7/10 FLAKY
|
|
71
|
+
classify::is_question 10/10
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Dataset-driven suites are a comprehension away — a ragged payload is
|
|
75
|
+
just a dict-valued parameter:
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
@pytest.mark.parametrize("row", [
|
|
79
|
+
pytest.param(row, id=row["id"]) for row in load_dataset()
|
|
80
|
+
])
|
|
81
|
+
def bench_extract(row):
|
|
82
|
+
assert extract(row["input"]) == row["expected"]
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Run outcomes
|
|
86
|
+
|
|
87
|
+
A run's outcome is decided by how the function body ends — the three
|
|
88
|
+
classes fall straight out of Python:
|
|
89
|
+
|
|
90
|
+
| The body… | Outcome | Meaning |
|
|
91
|
+
|---|---|---|
|
|
92
|
+
| returns normally | **pass** | correct answer |
|
|
93
|
+
| raises `AssertionError` | **fail** | your code answered wrong |
|
|
94
|
+
| raises anything else | **error** | your harness broke (API down, timeout, bug) |
|
|
95
|
+
|
|
96
|
+
Two consequences worth internalizing:
|
|
97
|
+
|
|
98
|
+
- **Assert the *outcome*, let infrastructure raise.** A wrong
|
|
99
|
+
classification is an `assert`; a `ConnectionError` from the client
|
|
100
|
+
library propagates by itself and lands in the error class. You get
|
|
101
|
+
the fail/error separation for free by writing idiomatic code.
|
|
102
|
+
- **`pytest.skip()` / `xfail` work** exactly as in pytest — and a
|
|
103
|
+
skipped run is not a sample: it never enters any fraction.
|
|
104
|
+
|
|
105
|
+
The plugin measures each run's wall-clock time itself (`elapsed` in the
|
|
106
|
+
{doc}`JSON report <json-report>`) — no stopwatch code in your benches.
|
|
107
|
+
|
|
108
|
+
Failures are ordinary assertion failures with **pytest's full assertion
|
|
109
|
+
rewriting**: bench modules pass through the same rewriter as
|
|
110
|
+
`test_*.py` files, so a bare assert reports its operands and diffs —
|
|
111
|
+
|
|
112
|
+
```text
|
|
113
|
+
> assert answer == expected
|
|
114
|
+
E AssertionError: assert 'other' == 'pii'
|
|
115
|
+
|
|
116
|
+
bench_classify.py:41: AssertionError
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
The first line of that message (your explicit assert message, or the
|
|
120
|
+
rewritten `assert 'other' == 'pii'`) also lands in the JSON report as
|
|
121
|
+
the run's `message`, so wrong answers are analyzable in bulk.
|
|
122
|
+
|
|
123
|
+
Rewriting honors pytest's own escape hatches: `--assert=plain`
|
|
124
|
+
disables it session-wide, and a module docstring containing
|
|
125
|
+
`PYTEST_DONT_REWRITE` opts that file out.
|
|
126
|
+
|
|
127
|
+
Want several checks against one case? Either assert them in sequence
|
|
128
|
+
(the first failure ends the run — exactly like a test), or split them
|
|
129
|
+
into separate `bench_*` functions sharing a helper so each check gets
|
|
130
|
+
its own fraction row.
|
|
131
|
+
|
|
132
|
+
## Multiple benchmarks per file
|
|
133
|
+
|
|
134
|
+
A file holds as many `bench_*` functions as you like; they collect in
|
|
135
|
+
definition order:
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
@pytest.mark.parametrize("row", CASES)
|
|
139
|
+
def bench_classify(row):
|
|
140
|
+
assert classify(row["input"]) == row["expected"]
|
|
141
|
+
|
|
142
|
+
@pytest.mark.parametrize("row", CASES)
|
|
143
|
+
def bench_latency(row):
|
|
144
|
+
t0 = time.perf_counter()
|
|
145
|
+
classify(row["input"])
|
|
146
|
+
assert time.perf_counter() - t0 < 2.0
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Summary rows are namespaced by the function's short name
|
|
150
|
+
(`bench_classify` → `classify::`), so identical case ids in different
|
|
151
|
+
benchmarks aggregate separately, and `-k bench_latency` selects one
|
|
152
|
+
benchmark's items.
|
|
@@ -1,5 +1,30 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.0 (2026-07-08)
|
|
4
|
+
|
|
5
|
+
Bench functions are now **plain test bodies that assert** — the
|
|
6
|
+
yield-a-result style is gone.
|
|
7
|
+
|
|
8
|
+
- **Breaking:** `StepResult` is removed. A bench function passes by
|
|
9
|
+
returning, fails on `AssertionError`, and errors on any other
|
|
10
|
+
exception — the three outcome classes fall straight out of Python.
|
|
11
|
+
`pytest.skip`/`xfail` bypass recording, as in pytest. A generator
|
|
12
|
+
bench (the old style) is rejected with a clear error.
|
|
13
|
+
- **Breaking:** rows aggregate per case (the step/label dimension is
|
|
14
|
+
gone): terminal rows read `classify::identify_pii 7/10 FLAKY`, and
|
|
15
|
+
the JSON `rows` lose `label` while `records` become flat per-run
|
|
16
|
+
entries (`outcome`, `message`, `error`, `elapsed`, `cost`, `usage`).
|
|
17
|
+
- **Assertion rewriting:** bench modules pass through pytest's
|
|
18
|
+
rewriter, so bare asserts report operands and diffs
|
|
19
|
+
(`assert 'other' == 'pii'`); the first line lands in the JSON
|
|
20
|
+
`message`. `--assert=plain` and `PYTEST_DONT_REWRITE` opt out.
|
|
21
|
+
- **New:** `record_usage(...)` and `record_cost(...)` attribute
|
|
22
|
+
per-model token usage and plain cost to the current run from inside
|
|
23
|
+
the body; usage recorded before a failing assert is kept. Run
|
|
24
|
+
wall-clock time is measured by the plugin (`elapsed` in records).
|
|
25
|
+
- Multiple checks per case = multiple asserts (first failure ends the
|
|
26
|
+
run) or multiple `bench_*` functions sharing a helper.
|
|
27
|
+
|
|
3
28
|
## 0.1.0 (2026-07-08)
|
|
4
29
|
|
|
5
30
|
Initial release.
|
|
@@ -32,7 +32,7 @@ def test_fraction_and_flaky_in_summary(pytester):
|
|
|
32
32
|
pytester.makepyfile(bench_flaky=BENCH_FLAKY)
|
|
33
33
|
result = pytester.runpytest("--prob-runs=2")
|
|
34
34
|
result.assert_outcomes(passed=1, failed=1)
|
|
35
|
-
result.stdout.fnmatch_lines(["*= probability =*", "*wobbly
|
|
35
|
+
result.stdout.fnmatch_lines(["*= probability =*", "*check::wobbly 1/2 FLAKY*"])
|
|
36
36
|
```
|
|
37
37
|
|
|
38
38
|
Conventions that keep the suite honest:
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# Cost and token usage
|
|
2
|
+
|
|
3
|
+
Sampling nondeterministic code N times has a literal price when the code
|
|
4
|
+
calls a paid API. The plugin makes spend visible next to the fractions
|
|
5
|
+
it bought — per row, in total, and broken down by model and token type:
|
|
6
|
+
|
|
7
|
+
```text
|
|
8
|
+
triage::refund-terse 8/10 $0.0010 FLAKY
|
|
9
|
+
triage::refund-chain_of_thought 10/10 $0.0040
|
|
10
|
+
|
|
11
|
+
Overall: 18/20 passed (90%)
|
|
12
|
+
Cost: $0.0050
|
|
13
|
+
Tokens: m-small 1,200 in / 80 out / 640 cached $0.0010
|
|
14
|
+
m-large 4,800 in / 900 out $0.0040
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
That table is an argument: the chain-of-thought prompt is four times
|
|
18
|
+
the price and eliminates the flakiness — now the trade-off is a number,
|
|
19
|
+
not a feeling.
|
|
20
|
+
|
|
21
|
+
## Recording usage: `record_usage`
|
|
22
|
+
|
|
23
|
+
Call `record_usage` from inside the bench function, once per model
|
|
24
|
+
call:
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
import pytest
|
|
28
|
+
from pytest_probability import record_usage
|
|
29
|
+
|
|
30
|
+
@pytest.mark.parametrize("text", CASES)
|
|
31
|
+
def bench_triage(text):
|
|
32
|
+
response = client.complete(prompt(text))
|
|
33
|
+
record_usage(
|
|
34
|
+
model=response.model,
|
|
35
|
+
input_tokens=response.usage.input_tokens,
|
|
36
|
+
output_tokens=response.usage.output_tokens,
|
|
37
|
+
cached_input_tokens=response.usage.cache_read_tokens,
|
|
38
|
+
cost=price_of(response),
|
|
39
|
+
)
|
|
40
|
+
assert grade(response)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
It accepts the fields as keywords (above), a `TokenUsage` instance, or
|
|
44
|
+
any duck-typed object/dict with the same attribute names — handy when
|
|
45
|
+
the numbers come straight from an API response:
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
record_usage({"model": "m-big", "input_tokens": 50, "output_tokens": 5,
|
|
49
|
+
"cached_input_tokens": 30, "cost": 0.04})
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Call it as many times as the run makes calls — a router plus a worker,
|
|
53
|
+
a generator plus a judge — and entries aggregate per model. The token
|
|
54
|
+
types stay separate (`input_tokens`, `output_tokens`,
|
|
55
|
+
`cached_input_tokens`) so cache effectiveness remains visible instead
|
|
56
|
+
of disappearing into a blended number.
|
|
57
|
+
|
|
58
|
+
**Record before you assert.** Usage recorded earlier in the body
|
|
59
|
+
survives a failing assert — a wrong answer never loses the money it
|
|
60
|
+
cost, which is precisely the kind of thing you want visible:
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
record_usage(model=response.model, ..., cost=0.03)
|
|
64
|
+
assert response.answer == expected # fails -> the $0.03 still reports
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Pricing is yours: the plugin does not know provider price sheets, it
|
|
68
|
+
sums what you report.
|
|
69
|
+
|
|
70
|
+
## Recording plain cost: `record_cost`
|
|
71
|
+
|
|
72
|
+
When there is no token breakdown to report — a flat per-call fee, a
|
|
73
|
+
metered tool — record the number directly:
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from pytest_probability import record_cost
|
|
77
|
+
|
|
78
|
+
def bench_search(query):
|
|
79
|
+
record_cost(0.0002)
|
|
80
|
+
assert search(query).ok
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## How the numbers combine
|
|
84
|
+
|
|
85
|
+
- A run's **cost** is the sum of its `record_cost` amounts plus the
|
|
86
|
+
`cost` of every `record_usage` entry.
|
|
87
|
+
- **Rows** sum run cost across all runs of the case, and merge usage
|
|
88
|
+
per model.
|
|
89
|
+
- **Totals**: the `Cost:` line sums all rows; the `Tokens:` block
|
|
90
|
+
merges all rows per model. Both appear only when something was
|
|
91
|
+
recorded; the `cached` segment appears only when non-zero.
|
|
92
|
+
- Calling either function **outside a bench run** raises
|
|
93
|
+
`RuntimeError` — they only make sense with a run to attribute to.
|
|
94
|
+
|
|
95
|
+
## Where it all shows up
|
|
96
|
+
|
|
97
|
+
- **Terminal summary** — `$` per row, `Cost:` overall, and the
|
|
98
|
+
per-model `Tokens:` block.
|
|
99
|
+
- **JSON report** — `rows[].cost`, `rows[].usage` (keyed by model),
|
|
100
|
+
`totals.cost`, `totals.usage`, and per-run `cost`/`usage` in
|
|
101
|
+
`records`; see {doc}`json-report`.
|
|
102
|
+
|
|
103
|
+
## Practical notes
|
|
104
|
+
|
|
105
|
+
- The `cost` field is currency-neutral — report whatever unit you
|
|
106
|
+
like, consistently. The `$` in the terminal is a money marker, not a
|
|
107
|
+
currency claim.
|
|
108
|
+
- When comparing variants for price, remember the per-row figure is the
|
|
109
|
+
sum over N runs, so divide by `--prob-runs` for per-call cost.
|
|
@@ -14,11 +14,11 @@ inside a green checkmark.
|
|
|
14
14
|
$ pytest benchmarks/ --prob-runs=10
|
|
15
15
|
...
|
|
16
16
|
================================= probability ==================================
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
classify::is_question 10/10 $0.0020
|
|
18
|
+
classify::identify_pii 7/10 $0.0020 FLAKY
|
|
19
|
+
classify::extract_amount 0/10 $0.0020 FAIL
|
|
20
|
+
triage::refund-terse 8/10 $0.0010 FLAKY
|
|
21
|
+
triage::refund-chain_of_thought 10/10 $0.0040
|
|
22
22
|
|
|
23
23
|
Overall: 35/50 passed (70%)
|
|
24
24
|
Cost: $0.0110
|
|
@@ -44,14 +44,12 @@ case
|
|
|
44
44
|
run
|
|
45
45
|
: One execution of a case — one pytest item
|
|
46
46
|
(`bench_x.py::bench_classify::identify_pii[run3]`). Lands in exactly
|
|
47
|
-
one of three classes:
|
|
48
|
-
|
|
49
|
-
step
|
|
50
|
-
: One yielded `StepResult` within a run — a labeled check.
|
|
47
|
+
one of three classes: a clean return **passes**, an `AssertionError`
|
|
48
|
+
**fails**, any other exception **errors**.
|
|
51
49
|
|
|
52
50
|
row
|
|
53
|
-
: The aggregate of all runs of one
|
|
54
|
-
|
|
51
|
+
: The aggregate of all runs of one case — a fraction, a status, and a
|
|
52
|
+
cost in the summary table.
|
|
55
53
|
|
|
56
54
|
## Why fractions?
|
|
57
55
|
|
|
@@ -90,9 +88,10 @@ up the gap.
|
|
|
90
88
|
- **Fractions per combination** — every parameter combination gets its
|
|
91
89
|
own row and pass fraction, stacked axes cross-product with pytest's
|
|
92
90
|
exact id rules, and an unparametrized function runs as a single case.
|
|
93
|
-
- **Cost and token accounting** —
|
|
94
|
-
|
|
95
|
-
per-model input/output/cached token
|
|
91
|
+
- **Cost and token accounting** — `record_usage()`/`record_cost()`
|
|
92
|
+
attribute spend to the current run (surviving failed asserts) and
|
|
93
|
+
yield per-row cost plus a per-model input/output/cached token
|
|
94
|
+
breakdown.
|
|
96
95
|
- **Machine-readable output** — `--prob-json` writes fractions,
|
|
97
96
|
statuses, and raw per-run records to a single file.
|
|
98
97
|
- **One dependency: pytest.**
|
|
@@ -31,7 +31,7 @@ point — there is nothing to enable. Two quick checks:
|
|
|
31
31
|
$ pytest --version -v
|
|
32
32
|
...
|
|
33
33
|
registered plugins:
|
|
34
|
-
probability-0.
|
|
34
|
+
probability-0.2.0 at .../pytest_probability/plugin.py
|
|
35
35
|
```
|
|
36
36
|
|
|
37
37
|
```text
|
|
@@ -43,7 +43,7 @@ $ pytest --help | grep -A2 prob-runs
|
|
|
43
43
|
The header of every test session also lists it:
|
|
44
44
|
|
|
45
45
|
```text
|
|
46
|
-
plugins: probability-0.
|
|
46
|
+
plugins: probability-0.2.0
|
|
47
47
|
```
|
|
48
48
|
|
|
49
49
|
## Disabling the plugin
|