-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy path__init__.py
More file actions
94 lines (73 loc) · 3.32 KB
/
__init__.py
File metadata and controls
94 lines (73 loc) · 3.32 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
91
92
93
94
from __future__ import annotations
import os
import sys
import warnings
from typing import TYPE_CHECKING
from pytest_codspeed.utils import SUPPORTS_PERF_TRAMPOLINE
if TYPE_CHECKING:
from .dist_instrument_hooks import InstrumentHooksPointer, LibType
# Feature flags for instrument hooks
FEATURE_DISABLE_CALLGRIND_MARKERS = 0
class InstrumentHooks:
"""Zig library wrapper class providing benchmark measurement functionality."""
lib: LibType
instance: InstrumentHooksPointer
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 .dist_instrument_hooks import lib # type: ignore
except ImportError as e:
raise RuntimeError(f"Failed to load instrument hooks library: {e}") from e
self.lib = lib
self.instance = self.lib.instrument_hooks_init()
if self.instance == 0:
raise RuntimeError("Failed to initialize CodSpeed instrumentation library.")
if SUPPORTS_PERF_TRAMPOLINE and not sys.is_stack_trampoline_active():
sys.activate_stack_trampoline("perf") # type: ignore
def __del__(self):
if hasattr(self, "lib") and hasattr(self, "instance"):
self.lib.instrument_hooks_deinit(self.instance)
def start_benchmark(self) -> None:
"""Start a new benchmark measurement."""
ret = self.lib.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.lib.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.lib.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.lib.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.lib.instrument_hooks_is_instrumented(self.instance)
def set_feature(self, feature: int, enabled: bool) -> None:
"""Set a feature flag in the instrument hooks library.
Args:
feature: The feature flag to set
enabled: Whether to enable or disable the feature
"""
self.lib.instrument_hooks_set_feature(feature, enabled)