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.
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
================================================================ test session starts ================================================================
platform darwin -- Python 3.9.12, pytest-7.1.2, pluggy-1.0.0
rootdir: /Users/juan/project/wally/d2k-5, configfile: pytest.ini
plugins: hypothesis-6.39.3, mock-3.7.0, repeat-0.9.1, doctestplus-0.12.0, arraydiff-0.5.0
collected 439 items
tests/box_test.py ......................... [ 5%]
tests/image_test.py ............... [ 9%]
tests/network_equivalence_test.py .........................................s................................................................. [ 33%]
.............................................................................. [ 51%]
tests/network_test.py ....................................................................................................................... [ 78%]
............................................................................................... [100%]
=================================================== 438 passed, 1 skipped, 62 warnings in 48.43s ====================================================
File #lines #miss Cover% Lines missing
--------------------------------- -------- ------- -------- ------------------------
d2k/__init__.py 3 0 100
d2k/box.py 105 27 74 73, 142-181
d2k/image.py 38 4 89 70-73
d2k/network.py 359 1 99 236
tests/box_test.py 178 0 100
tests/darknet.py 132 11 91 146, 179-191
tests/image_test.py 45 0 100
tests/network_equivalence_test.py 304 30 90 63, 68, 191-215, 455-465
tests/network_test.py 453 0 100
$
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
Logo design by Sophia Berger.
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.9-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 953fe7055b11f2a8f1f765089bb590376d0a63db12f98ce9a623acd4058a75c4 |
|
MD5 | a387809cee1a644bef9e5fa7ed59f2fb |
|
BLAKE2b-256 | a59d8ec14b3b0fb557374aa432b7c1f81db7382ca3aa59b7e97444535e57d42e |
Hashes for slipcover-0.1.9-cp310-cp310-manylinux_2_24_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a9fbb6e7c276a1e068d9a57f7d121281ec8aa5dd015aaddd3be9b156fd3dff5 |
|
MD5 | 9c03ea962b9985f857a5d3013c88c567 |
|
BLAKE2b-256 | 031ffe2de83087897db923f346d350eceb653135291afd9399825f33a9e66c6e |
Hashes for slipcover-0.1.9-cp310-cp310-macosx_10_15_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d7d3e834f2296e2dbee3111312329ab8f34c599020ec7fbad0fb7aa9b81ff2d |
|
MD5 | 3ebb10232d0cdec2997a3212a6378e9f |
|
BLAKE2b-256 | 6233a9a6cb1c89d1e2d24d6fb8bf7ea4c9a326d2636a364b228fc48da52c55b4 |
Hashes for slipcover-0.1.9-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3fa149fa50bb1703bdd378f2c430c960225219db7682a4aa1dc0f3fab1777675 |
|
MD5 | 81708d6c54cbf209a489bb406cd79558 |
|
BLAKE2b-256 | 6230545a6b397efc713974a43c65084fa13761617bcfb3e5158feff1a6a38268 |
Hashes for slipcover-0.1.9-cp39-cp39-manylinux_2_24_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd01be1950467cca3926537a4b8aebea717d510f36a30b98db8c449068bd5489 |
|
MD5 | ecebfc37491a1dee52c8e6d1f5a41ec3 |
|
BLAKE2b-256 | 040881c78b116e49846aba85317c4f3361b61e6ff3bb80c6c43f3def5b1d16ea |
Hashes for slipcover-0.1.9-cp39-cp39-macosx_10_15_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 220fcfc5c5d086174d0e5a7817331074e48d88ebf5b7257c2fe676aba0c9777f |
|
MD5 | 6f8352bf846eab4dccd29de19ae8f8cb |
|
BLAKE2b-256 | e102522ca60b791410a99836661750a7cecb033d7bb7dd00436b823861948878 |
Hashes for slipcover-0.1.9-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba822acc554dc0adb61fa079ce095dbb927bcb5a3e998098e11ba7b93cf78239 |
|
MD5 | b840f5a1c15556db860deebd4755a357 |
|
BLAKE2b-256 | 4999b4981f565a527a3aa1bc11145ec590035fc6ba5ae0f14ef6e354d6abfd8c |
Hashes for slipcover-0.1.9-cp38-cp38-manylinux_2_24_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ec871356bdce3a0e43f20c1e26605eb10efdf2665d26acf0382c2fff31a87c2 |
|
MD5 | deb3026cf711cf5a40223e76956d37cc |
|
BLAKE2b-256 | 0712f452161e8d37010058902f375b1607048de683480081a768926df0da198c |
Hashes for slipcover-0.1.9-cp38-cp38-macosx_10_15_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7346528a48b401d620ce23ad2ee924c2d93710ff28dbb357defe1fd34cd1384 |
|
MD5 | be28c3f2ddb5b97d7186095ee1470576 |
|
BLAKE2b-256 | 5418c5510b0507934dd52e8ea63332cdb358afa973340416ddabd8f8038d77cf |