Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat/add max time and quartiles #45

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
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
16 changes: 12 additions & 4 deletions src/pytest_codspeed/instruments/walltime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dataclasses import asdict, dataclass
from math import ceil
from statistics import mean, median, stdev
from statistics import mean, quantiles, stdev
from time import get_clock_info, perf_counter_ns
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -40,8 +40,10 @@ def from_codspeed_config(cls, config: CodSpeedConfig) -> BenchmarkConfig:
if config.warmup_time_ns is not None
else DEFAULT_WARMUP_TIME_NS,
min_round_time_ns=DEFAULT_MIN_ROUND_TIME_NS,
max_time_ns=DEFAULT_MAX_TIME_NS,
max_rounds=config.max_rounds or None,
max_time_ns=config.max_time_ns
if config.max_time_ns is not None
else DEFAULT_MAX_TIME_NS,
max_rounds=config.max_rounds,
)


Expand All @@ -51,7 +53,9 @@ class BenchmarkStats:
max_ns: float
stdev_ns: float
mean_ns: float
q1_ns: float
median_ns: float
q3_ns: float

rounds: int
total_time: float
Expand All @@ -71,12 +75,15 @@ def from_list(
) -> BenchmarkStats:
stdev_ns = stdev(times_ns) if len(times_ns) > 1 else 0
mean_ns = mean(times_ns)
q1_ns, median_ns, q3_ns = quantiles(times_ns, n=4)
return cls(
min_ns=min(times_ns),
max_ns=max(times_ns),
stdev_ns=stdev_ns,
mean_ns=mean_ns,
median_ns=median(times_ns),
median_ns=median_ns,
q1_ns=q1_ns,
q3_ns=q3_ns,
rounds=rounds,
total_time=total_time,
outlier_rounds=sum(1 for t in times_ns if abs(t - mean_ns) > stdev_ns),
Expand Down Expand Up @@ -125,6 +132,7 @@ def run_benchmark(
rounds = int(config.max_time_ns / round_time_ns)
else:
rounds = config.max_rounds
rounds = max(1, rounds)

# Benchmark
iter_range = range(iter_per_round)
Expand Down
21 changes: 18 additions & 3 deletions src/pytest_codspeed/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ def pytest_addoption(parser: pytest.Parser):
"only for walltime mode"
),
)
group.addoption(
"--codspeed-max-time",
action="store",
type=float,
help=(
"The maximum time to run a benchmark for (in seconds), "
"only for walltime mode"
),
)
group.addoption(
"--codspeed-max-rounds",
action="store",
Expand All @@ -75,6 +84,7 @@ def pytest_addoption(parser: pytest.Parser):
@dataclass(frozen=True)
class CodSpeedConfig:
warmup_time_ns: int | None = None
max_time_ns: int | None = None
max_rounds: int | None = None

@classmethod
Expand All @@ -83,9 +93,12 @@ def from_pytest_config(cls, config: pytest.Config) -> CodSpeedConfig:
warmup_time_ns = (
int(warmup_time * 1_000_000_000) if warmup_time is not None else None
)
max_time = config.getoption("--codspeed-max-time", None)
max_time_ns = int(max_time * 1_000_000_000) if max_time is not None else None
return cls(
warmup_time_ns=warmup_time_ns,
max_rounds=config.getoption("--codspeed-max-rounds", None),
max_time_ns=max_time_ns,
)


Expand Down Expand Up @@ -248,15 +261,17 @@ def _measure(
) -> T:
is_gc_enabled = gc.isenabled()
if is_gc_enabled:
gc.collect()
gc.disable()
pass
# gc.collect()
# gc.disable()
try:
uri, name = get_git_relative_uri_and_name(nodeid, config.rootpath)
return plugin.instrument.measure(name, uri, fn, *args, **kwargs)
finally:
# Ensure GC is re-enabled even if the test failed
if is_gc_enabled:
gc.enable()
pass
# gc.enable()


def wrap_runtest(
Expand Down
Loading