Skip to content

Commit

Permalink
fix: disable automatic garbage collection to increase stability
Browse files Browse the repository at this point in the history
  • Loading branch information
art049 committed Nov 22, 2022
1 parent 1c5f2cd commit 3c8bc32
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/pytest_codspeed/plugin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import gc
import os
import pkgutil
from dataclasses import dataclass, field
Expand Down Expand Up @@ -140,6 +141,22 @@ def pytest_collection_modifyitems(
items[:] = selected


def _run_with_instrumentation(
lib: "LibType", fn: Callable[[], Any], nodeId: str
) -> Any:
is_gc_enabled = gc.isenabled()
if is_gc_enabled:
gc.collect()
gc.disable()
lib.zero_stats()
lib.start_instrumentation()
fn()
lib.stop_instrumentation()
lib.dump_stats_at(f"{nodeId}".encode("ascii"))
if is_gc_enabled:
gc.enable()


@pytest.hookimpl(trylast=True)
def pytest_runtest_call(item: "pytest.Item"):
plugin = get_plugin(item.config)
Expand All @@ -155,11 +172,7 @@ def pytest_runtest_call(item: "pytest.Item"):
item.runtest()
else:
assert plugin.lib is not None
plugin.lib.zero_stats()
plugin.lib.start_instrumentation()
item.runtest()
plugin.lib.stop_instrumentation()
plugin.lib.dump_stats_at(f"{item.nodeid}".encode("ascii"))
_run_with_instrumentation(plugin.lib, item.runtest, item.nodeid)


@pytest.hookimpl()
Expand All @@ -181,11 +194,9 @@ def codspeed_benchmark(request: "pytest.FixtureRequest") -> Callable:
def run(func: Callable[..., Any], *args: Any):
if plugin.is_codspeed_enabled and plugin.should_measure:
assert plugin.lib is not None
plugin.lib.zero_stats()
plugin.lib.start_instrumentation()
func(*args)
plugin.lib.stop_instrumentation()
plugin.lib.dump_stats_at(f"{request.node.nodeid}".encode("ascii"))
_run_with_instrumentation(
plugin.lib, lambda: func(*args), request.node.nodeid
)
else:
func(*args)

Expand Down

0 comments on commit 3c8bc32

Please # to comment.