-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy path__init__.py
More file actions
90 lines (70 loc) · 3.15 KB
/
__init__.py
File metadata and controls
90 lines (70 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from __future__ import annotations
import os
import sys
import warnings
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Any
from pytest_codspeed.utils import SUPPORTS_PERF_TRAMPOLINE
class InstrumentHooks:
"""Native library wrapper class providing benchmark measurement functionality."""
_module: Any
_instance: Any
def __init__(self) -> None:
if os.environ.get("CODSPEED_ENV") is None:
raise RuntimeError(
"Can't run benchmarks outside of CodSpeed environment."
"Please set the CODSPEED_ENV environment variable."
)
try:
from . import dist_instrument_hooks # type: ignore
except ImportError as e:
raise RuntimeError(f"Failed to load instrument hooks library: {e}") from e
self._module = dist_instrument_hooks
self._instance = self._module.instrument_hooks_init()
if self._instance is None:
raise RuntimeError("Failed to initialize CodSpeed instrumentation library.")
if SUPPORTS_PERF_TRAMPOLINE:
sys.activate_stack_trampoline("perf") # type: ignore
def __del__(self):
# Don't manually deinit - let the capsule destructor handle it
pass
def start_benchmark(self) -> None:
"""Start a new benchmark measurement."""
ret = self._module.instrument_hooks_start_benchmark(self._instance)
if ret != 0:
warnings.warn("Failed to start benchmark measurement", RuntimeWarning)
def stop_benchmark(self) -> None:
"""Stop the current benchmark measurement."""
ret = self._module.instrument_hooks_stop_benchmark(self._instance)
if ret != 0:
warnings.warn("Failed to stop benchmark measurement", RuntimeWarning)
def set_executed_benchmark(self, uri: str, pid: int | None = None) -> None:
"""Set the executed benchmark URI and process ID.
Args:
uri: The benchmark URI string identifier
pid: Optional process ID (defaults to current process)
"""
if pid is None:
pid = os.getpid()
ret = self._module.instrument_hooks_set_executed_benchmark(
self._instance, pid, uri.encode("ascii")
)
if ret != 0:
warnings.warn("Failed to set executed benchmark", RuntimeWarning)
def set_integration(self, name: str, version: str) -> None:
"""Set the integration name and version."""
ret = self._module.instrument_hooks_set_integration(
self._instance, name.encode("ascii"), version.encode("ascii")
)
if ret != 0:
warnings.warn("Failed to set integration name and version", RuntimeWarning)
def is_instrumented(self) -> bool:
"""Check if simulation is active."""
return self._module.instrument_hooks_is_instrumented(self._instance)
def callgrind_start_instrumentation(self) -> None:
"""Start callgrind instrumentation."""
self._module.callgrind_start_instrumentation()
def callgrind_stop_instrumentation(self) -> None:
"""Stop callgrind instrumentation."""
self._module.callgrind_stop_instrumentation()