You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The pytest plugin was documented only in a FAQ entry near the bottom,
which is where you look after you already suspect the feature exists.
Jupyter support gets a top-level "Scalene with Jupyter" section; pytest
now gets the parallel treatment, placed right beside it, with the full
option table, the marker, and how to view the resulting profile.
Also documents the pytest-xdist restriction added in af3648d -- users
who run their suite with -n will hit it, and the error should not be the
first they hear of it -- and adds a bullet to "Other features". The FAQ
entry keeps its short answer and now links to the full section instead
of drifting out of sync with it.
Every claim checked against the running plugin: option names and help
text match `pytest --help` verbatim, and the marker's "unmarked tests
still run, with sampling suspended" wording matches the code.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: README.md
+71Lines changed: 71 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -302,6 +302,8 @@ disclosure triangles at the bottom of the GUI.
302
302
303
303
- Scalene can produce **reduced profiles** (via `--reduced-profile`) that only report lines that consume more than 1% of CPU or perform at least 100 allocations.
304
304
- Scalene supports `@profile` decorators to profile only specific functions.
305
+
- Scalene profiles **pytest test suites** directly, via `pytest --scalene`
306
+
(and `@pytest.mark.scalene` to narrow it to specific tests).
305
307
- When Scalene is profiling a program launched in the background (via `&`), you can **suspend and resume profiling**.
306
308
- Scalene supports **free-threaded Python (3.13t / 3.14t)** with full
307
309
CPU + memory profiling.
@@ -487,6 +489,68 @@ code...
487
489
```
488
490
</details>
489
491
492
+
### Scalene with pytest
493
+
494
+
<details>
495
+
<summary>
496
+
Instructions for profiling a test suite with <code>pytest --scalene</code>
497
+
</summary>
498
+
499
+
Scalene ships a pytest plugin, so profiling a test run is just a flag on your
500
+
usual pytest command — no throwaway driver script. The plugin is registered
501
+
automatically when you install Scalene; it does nothing unless you pass
502
+
`--scalene`.
503
+
504
+
```console
505
+
pytest --scalene # profile the whole session (CPU)
506
+
pytest --scalene -k test_hot # profile only the selected tests
507
+
pytest --scalene --scalene-memory # profile CPU *and* memory
508
+
```
509
+
510
+
The profile is written to `scalene-profile.json`, which you view the usual way:
511
+
512
+
```console
513
+
scalene view # open in browser
514
+
scalene view --cli # view in terminal
515
+
```
516
+
517
+
To profile only certain tests, mark them with `@pytest.mark.scalene`. Unmarked
518
+
tests still run, but with profiling suspended:
519
+
520
+
```python
521
+
import pytest
522
+
523
+
@pytest.mark.scalene
524
+
def test_the_slow_one():
525
+
...
526
+
```
527
+
528
+
Options:
529
+
530
+
| Option | Effect |
531
+
| --- | --- |
532
+
| `--scalene` | Profile CPU, in-process (no re-launch) |
533
+
| `--scalene-memory` | Also profile memory (re-runs pytest under `scalene run`) |
534
+
| `--scalene-gpu` | Also profile GPU time and memory (also re-runs) |
535
+
| `--scalene-outfile PATH` | Where to write the profile |
536
+
| `--scalene-args='...'` | Extra arguments to forward to `scalene run` |
537
+
538
+
Memory and GPU profiling re-run pytest under `scalene run` automatically,
539
+
because allocation tracking needs Scalene's native library loaded before the
540
+
interpreter starts. Equivalently, you can drive it the other way around:
541
+
542
+
```console
543
+
scalene run -m pytest your_test.py
544
+
```
545
+
546
+
**Note on `pytest-xdist`:** profiling is not compatible with distributed runs
547
+
(`-n 2`, `--dist`). Tests execute in worker subprocesses that the profiler
548
+
does not observe, so the profile would come out nearly empty; Scalene reports
549
+
an error instead of writing a misleading profile. Run without `-n` — or with
550
+
`-n 0`, which keeps tests in the current process and profiles normally.
551
+
552
+
</details>
553
+
490
554
## Installation
491
555
492
556
<details open>
@@ -591,6 +655,13 @@ You can also drive it the other way around, which is equivalent to
591
655
scalene run -m pytest your_test.py
592
656
```
593
657
658
+
Profiling is not compatible with distributed `pytest-xdist` runs (`-n 2`,
659
+
`--dist`): the tests run in worker subprocesses the profiler cannot see, so
660
+
Scalene reports an error rather than writing a near-empty profile. Use `-n 0`
661
+
or drop the flag.
662
+
663
+
See [Scalene with pytest](#scalene-with-pytest) for the full set of options.
0 commit comments