Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: disable automatic garbage collection to increase stability #2

Merged
merged 1 commit into from
Nov 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 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,20 @@ def pytest_collection_modifyitems(
items[:] = selected


def _run_with_instrumentation(lib: "LibType", fn: Callable[[], Any], nodeId: str):
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 +170,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 +192,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