Skip to content

Commit ef62849

Browse files
committed
fixup! feat: store more benchmark metadata in results
1 parent 6d5ca95 commit ef62849

File tree

7 files changed

+27
-26
lines changed

7 files changed

+27
-26
lines changed

src/pytest_codspeed/benchmark.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def has_args(item: pytest.Item) -> bool:
1717

1818

1919
@dataclass
20-
class Benchmark:
20+
class BenchmarkMetadata:
2121
file: str
2222
module: str
2323
groups: list[str]
@@ -26,7 +26,7 @@ class Benchmark:
2626
args_names: list[str]
2727

2828
@classmethod
29-
def from_item(cls, item: pytest.Item) -> Benchmark:
29+
def from_item(cls, item: pytest.Item) -> BenchmarkMetadata:
3030
file = str(get_git_relative_path(item.path))
3131
module = "::".join(
3232
[node.name for node in item.listchain() if isinstance(node, pytest.Class)]

src/pytest_codspeed/instruments/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import pytest
1111

12-
from pytest_codspeed.benchmark import Benchmark
12+
from pytest_codspeed.benchmark import BenchmarkMetadata
1313
from pytest_codspeed.plugin import CodSpeedConfig
1414

1515
T = TypeVar("T")
@@ -28,7 +28,7 @@ def get_instrument_config_str_and_warns(self) -> tuple[str, list[str]]: ...
2828
@abstractmethod
2929
def measure(
3030
self,
31-
benchmark: Benchmark,
31+
benchmark_metadata: BenchmarkMetadata,
3232
fn: Callable[P, T],
3333
*args: P.args,
3434
**kwargs: P.kwargs,

src/pytest_codspeed/instruments/valgrind/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from pytest import Session
1616

17-
from pytest_codspeed.benchmark import Benchmark
17+
from pytest_codspeed.benchmark import BenchmarkMetadata
1818
from pytest_codspeed.instruments import P, T
1919
from pytest_codspeed.instruments.valgrind._wrapper import LibType
2020
from pytest_codspeed.plugin import CodSpeedConfig
@@ -28,7 +28,7 @@ class ValgrindInstrument(Instrument):
2828

2929
def __init__(self, config: CodSpeedConfig) -> None:
3030
self.benchmark_count = 0
31-
self.benchmarks: list[Benchmark] = []
31+
self.benchmarks_metadata: list[BenchmarkMetadata] = []
3232
self.should_measure = os.environ.get("CODSPEED_ENV") is not None
3333
if self.should_measure:
3434
self.lib = get_lib()
@@ -57,13 +57,13 @@ def get_instrument_config_str_and_warns(self) -> tuple[str, list[str]]:
5757

5858
def measure(
5959
self,
60-
benchmark: Benchmark,
60+
benchmark_metadata: BenchmarkMetadata,
6161
fn: Callable[P, T],
6262
*args: P.args,
6363
**kwargs: P.kwargs,
6464
) -> T:
6565
self.benchmark_count += 1
66-
self.benchmarks.append(benchmark)
66+
self.benchmarks_metadata.append(benchmark_metadata)
6767
if self.lib is None: # Thus should_measure is False
6868
return fn(*args, **kwargs)
6969

@@ -81,7 +81,7 @@ def __codspeed_root_frame__() -> T:
8181
finally:
8282
# Ensure instrumentation is stopped even if the test failed
8383
self.lib.stop_instrumentation()
84-
self.lib.dump_stats_at(benchmark.to_json_string().encode("ascii"))
84+
self.lib.dump_stats_at(benchmark_metadata.to_json_string().encode("ascii"))
8585

8686
def report(self, session: Session) -> None:
8787
reporter = session.config.pluginmanager.get_plugin("terminalreporter")
@@ -94,5 +94,5 @@ def report(self, session: Session) -> None:
9494
def get_result_dict(self) -> dict[str, Any]:
9595
return {
9696
"instrument": {"type": self.instrument},
97-
"benchmarks": [asdict(bench) for bench in self.benchmarks],
97+
"benchmarks": [asdict(bench) for bench in self.benchmarks_metadata],
9898
}

src/pytest_codspeed/instruments/walltime.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from rich.table import Table
1212
from rich.text import Text
1313

14-
from pytest_codspeed.benchmark import Benchmark
14+
from pytest_codspeed.benchmark import BenchmarkMetadata
1515
from pytest_codspeed.instruments import Instrument
1616

1717
if TYPE_CHECKING:
@@ -115,13 +115,13 @@ def from_list(
115115

116116

117117
@dataclass
118-
class WalltimeBenchmark(Benchmark):
118+
class WalltimeBenchmark(BenchmarkMetadata):
119119
config: WalltimeBenchmarkConfig
120120
stats: WalltimeBenchmarkStats
121121

122122

123123
def run_benchmark(
124-
benchmark: Benchmark,
124+
benchmark_metadata: BenchmarkMetadata,
125125
fn: Callable[P, T],
126126
args,
127127
kwargs,
@@ -182,7 +182,7 @@ def run_benchmark(
182182
)
183183

184184
return WalltimeBenchmark(
185-
**asdict(benchmark),
185+
**asdict(benchmark_metadata),
186186
config=config,
187187
stats=stats,
188188
), out
@@ -200,13 +200,13 @@ def get_instrument_config_str_and_warns(self) -> tuple[str, list[str]]:
200200

201201
def measure(
202202
self,
203-
benchmark: Benchmark,
203+
benchmark_metadata: BenchmarkMetadata,
204204
fn: Callable[P, T],
205205
*args: P.args,
206206
**kwargs: P.kwargs,
207207
) -> T:
208208
bench, out = run_benchmark(
209-
benchmark=benchmark,
209+
benchmark_metadata=benchmark_metadata,
210210
fn=fn,
211211
args=args,
212212
kwargs=kwargs,

src/pytest_codspeed/plugin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import pytest
1414
from _pytest.fixtures import FixtureManager
1515

16-
from pytest_codspeed.benchmark import Benchmark
16+
from pytest_codspeed.benchmark import BenchmarkMetadata
1717
from pytest_codspeed.instruments import (
1818
MeasurementMode,
1919
get_instrument_from_mode,
@@ -263,8 +263,8 @@ def _measure(
263263
gc.collect()
264264
gc.disable()
265265
try:
266-
benchmark = Benchmark.from_item(item)
267-
return plugin.instrument.measure(benchmark, fn, *args, **kwargs)
266+
benchmark_metadata = BenchmarkMetadata.from_item(item)
267+
return plugin.instrument.measure(benchmark_metadata, fn, *args, **kwargs)
268268
finally:
269269
# Ensure GC is re-enabled even if the test failed
270270
if is_gc_enabled:

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def run_pytest_codspeed_with_mode(
8484
# create empty `.git` folder in the rootdir to simulate a git repository
8585
if not pytester.path.joinpath(".git").exists():
8686
pytester.mkdir(".git")
87+
# subprocess is important for inline-snapshot to work properly
8788
return pytester.runpytest_subprocess(
8889
*csargs,
8990
*args,

tests/test_benchmark.py renamed to tests/test_benchmark_metadata.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from pytest_codspeed.plugin import Benchmark
3+
from pytest_codspeed.plugin import BenchmarkMetadata
44

55

66
class ClassWithStr:
@@ -9,31 +9,31 @@ def __str__(self):
99

1010

1111
benchmarks = [
12-
Benchmark(
12+
BenchmarkMetadata(
1313
file="test_benchmark_results.py",
1414
module="TestClass::TestNested",
1515
groups=["bench-group"],
1616
name="test_fully_parametrized",
1717
args=[58.3, "baz"],
1818
args_names=["a", "b"],
1919
),
20-
Benchmark(
20+
BenchmarkMetadata(
2121
file="another_test_file.py",
2222
module="AnotherClass::AnotherTest",
2323
groups=["another-group"],
2424
name="test_another_case",
2525
args=[42, "foo"],
2626
args_names=["x", "y"],
2727
),
28-
Benchmark(
28+
BenchmarkMetadata(
2929
file="yet_another_test_file.py",
3030
module="",
3131
groups=[],
3232
name="test_yet_another_case",
3333
args=[],
3434
args_names=[],
3535
),
36-
Benchmark(
36+
BenchmarkMetadata(
3737
file="test_complex_args.py",
3838
module="",
3939
groups=[],
@@ -65,7 +65,7 @@ def __str__(self):
6565
),
6666
],
6767
)
68-
def test_benchmark_to_json(bench, expected_json):
68+
def test_benchmark_metadata_to_json(bench, expected_json):
6969
assert bench.to_json_string() == expected_json
7070

7171

@@ -78,5 +78,5 @@ def test_benchmark_to_json(bench, expected_json):
7878
(benchmarks[3], "test_complex_args[x0-foo-z2-w3-class_str]"),
7979
],
8080
)
81-
def test_benchmark_display_name(bench, expected_display_name):
81+
def test_benchmark_metadata_display_name(bench, expected_display_name):
8282
assert bench.display_name == expected_display_name

0 commit comments

Comments
 (0)