Coverage analysis is widely used but can suffer from high overhead. This overhead is especially acute in the context of Python, which is already notoriously slow (a recent study observes a roughly 30x slowdown vs. native code). We find that the state-of-the-art coverage tool for Python, $\tt coverage.py$, introduces a median overhead of 180% with the standard Python interpreter. Slowdowns are even more extreme when using PyPy, a JIT-compiled Python implementation, with $\tt coverage.py$ imposing a median overhead of 1,300%. This performance degradation reduces the utility of coverage analysis in most use cases, including testing and fuzzing, and precludes its use in deployment. This paper presents SlipCover, a novel, near-zero overhead coverage analyzer for Python. SlipCover works without modifications to either the Python interpreter or PyPy. It first processes a program's AST to accurately identify all branches and lines. SlipCover then dynamically rewrites Python bytecodes to add lightweight instrumentation to each identified branch and line. At run time, SlipCover periodically de-instruments already-covered lines and branches. The result is extremely low overheads -- a median of just 5% -- making SlipCover suitable for use in deployment. We show its efficiency can translate to significant increases in the speed of coverage-based clients. As a proof of concept, we integrate SlipCover into TPBT, a targeted property-based testing system, and observe a 22x speedup.
翻译:覆盖率分析被广泛使用,但可能面临高开销的问题。这种开销在Python环境中尤为突出——Python本身已因运行缓慢而著称(最新研究显示其运行速度比原生代码慢约30倍)。我们发现,当前Python最先进的覆盖率工具$\tt coverage.py$在标准Python解释器下会产生180%的中位开销。当使用JIT编译型Python实现PyPy时,性能下降更为显著:$\tt coverage.py$引入的中位开销高达1,300%。这种性能损耗降低了覆盖率分析在测试、模糊测试等典型场景中的实用性,并导致其无法应用于生产环境。本文提出SlipCover——一种新型的近乎零开销的Python覆盖率分析工具。SlipCover无需修改Python解释器或PyPy即可工作。它首先解析程序的AST以精确识别所有分支和代码行,随后通过动态重写Python字节码,为每个识别出的分支和代码行添加轻量级探针。在运行时,SlipCover会周期性地移除已覆盖代码行和分支的检测代码,最终实现极低的性能开销——中位仅5%,使其适用于生产环境。我们证明其高效性可显著提升基于覆盖率的客户端的运行速度。作为概念验证,我们将SlipCover集成至TPBT(目标属性测试系统),观察到22倍的加速效果。