Near Zero-Overhead Python Code Coverage
Project description
Slipcover: Near Zero-Overhead Python Code Coverage
by Juan Altmayer Pizzorno and Emery Berger at UMass Amherst's PLASMA lab.
About Slipcover
Slipcover is a fast code coverage tool. It tracks a Python program as it runs and reports on the parts that executed and those that didn't. That can help guide your testing (showing code that isn't being tested), debugging, fuzzing or to find "dead" code.
Past code coverage tools can make programs significantly slower; it is not uncommon for them to take twice as long to execute. Slipcover aims to provide the same information with near-zero overhead, often almost as fast as running the original Python program.
How it works
Previous coverage tools like Coverage.py rely on Python's tracing facilities, which add significant overhead. Instead, Slipcover uses just-in-time instrumentation and de-instrumentation. When Slipcover gathers coverage information, it modifies the program's Python byte codes, inserting instructions that let it keep track the lines executed by the program. As the program executes, Slipcover gradually removes instrumentation that is no longer needed, allowing those parts to run at full speed. Care is taken throughout Slipcover to keep things as efficient as possible.
Performance
The image on the right shows the execution time of a few benchmarks. It compares how long they take to run while tracking coverage using Coverage.py and tracking coverage using Slipcover, relative to their normal running times.
The first two benchmarks are the test suites for scikit-learn and Flask; "sudoku" runs Peter Norvig's Sudoku solver while the others were derived from the Python Benchmark Suite.
More "Python-intensive" programs such as sudoku and those from the benchmark suite (with a larger proportion of execution time spent in Python, rather than in native code) generate more tracing events, causing more overhead in Coverage.py. While each program's structure can affect Slipcover's ability to de-instrument, its running time stays relatively close to the original, mostly at 5% or less overhead.
Accuracy
We verified Slipcover's accuracy against Coverage.py and against a simple script of our own that collects coverage using Python tracing. We found Slipcover's results to be accurate, in fact, in certain cases more accurate.
Getting started
Slipcover is available from PyPI. You can install it like any other Python module with
pip3 install slipcover
You could then run your Python script with:
python3 -m slipcover myscript.py
Using it with a test harness
Slipcover can also execute a Python module, as in:
python3 -m slipcover -m pytest -x -v
which starts pytest
, passing it any options (-x -v
in this example)
after the module name.
No plug-in is required for pytest.
Usage example
$ python3 -m slipcover -m pytest --disable-warnings
============================================================================= test session starts ==============================================================================
platform darwin -- Python 3.9.9, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/juan/project/wally/d2k-5
collected 439 items
tests/box_test.py ......................... [ 5%]
tests/image_test.py ............... [ 9%]
tests/network_equivalence_test.py .........................................s............................................................................................ [ 39%]
................................................... [ 51%]
tests/network_test.py .................................................................................................................................................. [ 84%]
.................................................................... [100%]
================================================================= 438 passed, 1 skipped, 53 warnings in 55.31s =================================================================
File #lines #missed Cover% Lines missing
---------------- -------- --------- -------- ---------------------------------------------------------------------------------------------------
d2k/__init__.py 3 0 100
d2k/network.py 359 1 100 236
d2k/box.py 105 27 74 73, 142, 144-146, 148-149, 151, 154, 156-159, 161, 163-166, 168, 170-171, 173-174, 176-177, 180-181
d2k/image.py 38 4 89 70-73
tests/darknet.py 132 11 92 146, 179-181, 183-187, 189, 191
$
As can be seen in the coverage report, d2k lacks some coverage, especially in
its box.py
and image.py
components.
Platforms
Our GitHub workflows run the automated test suite on Linux, MacOS and Windows, but really it should work anywhere where CPython does.
Contributing
Slipcover is alpha software, and under active development. Please feel free to create a new issue with any suggestions or issues you may encounter.
Acknowledgements
This material is based upon work supported by the National Science Foundation under Grant No. 1955610. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for slipcover-0.1.6-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f65d819da388c5065d6a665bf4bc71dcb6512d7db08aaf080d1cdb4c3073b2f |
|
MD5 | a621968e6b98eac41d984800551dd2fa |
|
BLAKE2b-256 | 0bba69759a30170bac5f0d97a955498ffb97ad7b51e47f7353e4631c11e0d2cf |
Hashes for slipcover-0.1.6-cp310-cp310-manylinux_2_24_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 541dbb51eced97269f77c7fa792b1ff59399c95599ac3b3300543e28b95d269a |
|
MD5 | e1b2ffdef78adc8bea6d1da2f04e47ba |
|
BLAKE2b-256 | 5cd74ffaa05d6aaa03971b6250e1ae635a3d64f89609bca040ebe7971adb9e65 |
Hashes for slipcover-0.1.6-cp310-cp310-macosx_10_15_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed9338ac23258d3fa5dec979113dc9a94348716640047e749c33f737b695abd2 |
|
MD5 | f3dd4197bfb0eec25e451368a02624ac |
|
BLAKE2b-256 | 762e8058efb8689588d85f360d514b594ce2e042db7140c448868c798c9a2966 |
Hashes for slipcover-0.1.6-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b86607ade8fb19c48c3c4949a5e3e6bb4d56bfbfa4b7e18943f3de8869886ff9 |
|
MD5 | 8d78383084802289843e0d8dd8f3e7d0 |
|
BLAKE2b-256 | d146e73c9bb853ae24cc4c14c66d94680cfbb22a70bac58086b2dec7f700fd48 |
Hashes for slipcover-0.1.6-cp39-cp39-manylinux_2_24_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | beeb8515dc3708c22b301e93e3989b4f5ed7280fc1553bb5a3c66a157ddaa4b9 |
|
MD5 | 12163c395ac0ec39642289fc17ee8940 |
|
BLAKE2b-256 | 1fde8f6fd1d393f49f3013b8f4cfa573d25b762975642a77f32afbfa3fdeb251 |
Hashes for slipcover-0.1.6-cp39-cp39-macosx_10_15_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fcf18ee91a051ceeb13cee25b4f66360d2188203668ce3d230d03da330a69cd7 |
|
MD5 | 6dd707f11ce7c21265486e324c404d66 |
|
BLAKE2b-256 | c9c1d7b67348bfba6a5297362fcab2da10b2a132a4a8241b54f66197cd238221 |
Hashes for slipcover-0.1.6-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a5d6ff26b1553ed4ef4c2c3bc87108c028122af2eed08fcabbf1365442bfa25 |
|
MD5 | 09f57dfcaf492fb0da1408f95e46875c |
|
BLAKE2b-256 | e9e65b0a89775363c0ae5f2a4205682b2837245ea006d7927a7f9133009540bc |
Hashes for slipcover-0.1.6-cp38-cp38-manylinux_2_24_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0af060ab27504b5f596fd51faaf074a5cd6052ea6f003162be2a7e32fce03fd2 |
|
MD5 | 4485b5e6ce98f40c8b1d681048f25170 |
|
BLAKE2b-256 | 813c3ea89aa0572afe0daa0c7f4f4cfdda975cb6f024c9f39c7ce2b9ce52e657 |
Hashes for slipcover-0.1.6-cp38-cp38-macosx_10_15_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 76cdbe544626032389f34924444ebc25a5ca74f395847a9c26d0fc81d23146f7 |
|
MD5 | cd5fb3693d03461166c82f734e6ed6dd |
|
BLAKE2b-256 | b7a5becbc5b13dbaaf2919aa7f3b4d72ee4b245e120bb2cc6f8dd9f57127c4ad |