|
5 | 5 | import json |
6 | 6 | import os |
7 | 7 | import random |
| 8 | +from collections.abc import AsyncIterator |
| 9 | +from contextlib import contextmanager |
8 | 10 | from dataclasses import dataclass, field |
| 11 | +from inspect import isawaitable |
9 | 12 | from pathlib import Path |
10 | 13 | from time import time |
11 | | -from typing import TYPE_CHECKING, cast |
| 14 | +from typing import TYPE_CHECKING, cast, overload |
12 | 15 |
|
13 | 16 | import pytest |
14 | 17 | from _pytest.fixtures import FixtureManager |
@@ -232,35 +235,85 @@ def pytest_collection_modifyitems( |
232 | 235 | items[:] = selected |
233 | 236 |
|
234 | 237 |
|
235 | | -def _measure( |
236 | | - plugin: CodSpeedPlugin, |
237 | | - node: pytest.Item, |
238 | | - config: pytest.Config, |
239 | | - pedantic_options: PedanticOptions | None, |
240 | | - fn: Callable[..., T], |
241 | | - args: tuple[Any, ...], |
242 | | - kwargs: dict[str, Any], |
243 | | -) -> T: |
| 238 | +@contextmanager |
| 239 | +def _measure_context(node: pytest.Item) -> AsyncIterator[None] |
244 | 240 | marker_options = BenchmarkMarkerOptions.from_pytest_item(node) |
245 | 241 | random.seed(0) |
246 | 242 | is_gc_enabled = gc.isenabled() |
247 | 243 | if is_gc_enabled: |
248 | 244 | gc.collect() |
249 | 245 | gc.disable() |
250 | | - try: |
251 | | - uri, name = get_git_relative_uri_and_name(node.nodeid, config.rootpath) |
| 246 | + |
| 247 | + yield |
| 248 | + |
| 249 | + # Ensure GC is re-enabled even if the test failed |
| 250 | + if is_gc_enabled: |
| 251 | + gc.enable() |
| 252 | + |
| 253 | + |
| 254 | +async def _async_measure( |
| 255 | + plugin: CodSpeedPlugin, |
| 256 | + node: pytest.Item, |
| 257 | + pedantic_options: PedanticOptions | None, |
| 258 | + fn: Awaitable[T], |
| 259 | + args: tuple[Any, ...], |
| 260 | + kwargs: dict[str, Any], |
| 261 | +) -> T: |
| 262 | + with _measure_context(node): |
252 | 263 | if pedantic_options is None: |
253 | | - return plugin.instrument.measure( |
| 264 | + return await plugin.instrument.measure_async( |
254 | 265 | marker_options, name, uri, fn, *args, **kwargs |
255 | 266 | ) |
256 | 267 | else: |
257 | | - return plugin.instrument.measure_pedantic( |
| 268 | + return await plugin.instrument.measure_pedantic_async( |
258 | 269 | marker_options, pedantic_options, name, uri |
259 | 270 | ) |
260 | | - finally: |
261 | | - # Ensure GC is re-enabled even if the test failed |
262 | | - if is_gc_enabled: |
263 | | - gc.enable() |
| 271 | + |
| 272 | + |
| 273 | +@overload |
| 274 | +def _measure( |
| 275 | + plugin: CodSpeedPlugin, |
| 276 | + node: pytest.Item, |
| 277 | + config: pytest.Config, |
| 278 | + pedantic_options: PedanticOptions | None, |
| 279 | + fn: Awaitable[T], |
| 280 | + args: tuple[Any, ...], |
| 281 | + kwargs: dict[str, Any], |
| 282 | +) -> Awaitable[T]: |
| 283 | + ... |
| 284 | +@overload |
| 285 | +def _measure( |
| 286 | + plugin: CodSpeedPlugin, |
| 287 | + node: pytest.Item, |
| 288 | + config: pytest.Config, |
| 289 | + pedantic_options: PedanticOptions | None, |
| 290 | + fn: Callable[..., T], |
| 291 | + args: tuple[Any, ...], |
| 292 | + kwargs: dict[str, Any], |
| 293 | +) -> T: |
| 294 | + ... |
| 295 | +def _measure( |
| 296 | + plugin: CodSpeedPlugin, |
| 297 | + node: pytest.Item, |
| 298 | + config: pytest.Config, |
| 299 | + pedantic_options: PedanticOptions | None, |
| 300 | + fn: Callable[..., T] | Awaitable[T], |
| 301 | + args: tuple[Any, ...], |
| 302 | + kwargs: dict[str, Any], |
| 303 | +) -> T | Awaitable[T]: |
| 304 | + uri, name = get_git_relative_uri_and_name(node.nodeid, config.rootpath) |
| 305 | + if isawaitable(fn): |
| 306 | + return _async_measure(plugin, node, pedantic_options, fn, args, kwargs) |
| 307 | + else: |
| 308 | + with _measure_context(node): |
| 309 | + if pedantic_options is None: |
| 310 | + return plugin.instrument.measure( |
| 311 | + marker_options, name, uri, fn, *args, **kwargs |
| 312 | + ) |
| 313 | + else: |
| 314 | + return plugin.instrument.measure_pedantic( |
| 315 | + marker_options, pedantic_options, name, uri |
| 316 | + ) |
264 | 317 |
|
265 | 318 |
|
266 | 319 | def wrap_runtest( |
|
0 commit comments