Skip to content

Commit b7536c7

Browse files
Performance test: Allow custom sampling intervals
Change-Id: Ib5f1c068f30a0ae86338701563da345ba58a7676
1 parent 44706af commit b7536c7

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

tests/performance/perftest_plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ def get_durations(timestamps: list[str]) -> list[float]:
11131113
start_time = Datetime.fromisoformat(timestamps[0])
11141114
end_time = Datetime.fromisoformat(timestamps[-1])
11151115
duration = end_time - start_time
1116-
interval = round(duration.total_seconds() / len(timestamps))
1116+
interval = round(duration.total_seconds() / len(timestamps), 1)
11171117
return [i * interval for i in range(len(timestamps))]
11181118

11191119
timestamps: list[str] = []

tests/performance/sysmon.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,12 @@ def _init_resources_file(task_name: str) -> Path:
206206

207207

208208
def _log_resources(
209-
file_path: Path, start_event: threading.Event, stop_event: threading.Event
209+
file_path: Path,
210+
start_event: threading.Event,
211+
stop_event: threading.Event,
212+
sampling_interval: float,
210213
) -> None:
211214
max_duration = 0.0
212-
statistics_interval = 1
213215
with open(file_path, "w") as stats_file:
214216
while not stop_event.is_set():
215217
start_time = time.time()
@@ -218,9 +220,9 @@ def _log_resources(
218220
end_time = time.time()
219221
duration = end_time - start_time
220222
max_duration = duration if duration > max_duration else max_duration
221-
# wait until next interval
222-
if duration < statistics_interval:
223-
time.sleep(statistics_interval - duration)
223+
# wait until next sampling interval
224+
if duration < sampling_interval:
225+
time.sleep(sampling_interval - duration)
224226
write_statistics(stats_file, close=True)
225227

226228

@@ -229,6 +231,7 @@ def track_resources(
229231
task_name: str,
230232
start_event: threading.Event | None = None,
231233
stop_event: threading.Event | None = None,
234+
sampling_interval: float = 1.0,
232235
) -> Iterator[None]:
233236
"""Monitor and track the resource usage of the host while executing a task context.
234237
@@ -240,13 +243,16 @@ def track_resources(
240243
task_name: The name of the task (used for naming the resources file).
241244
start_event: Controls the actual start of the resource tracking (optional).
242245
stop_event: Controls the actual end of the resource tracking (optional).
246+
sampling_interval: The measurement sampling interval in seconds (default: 1.0).
243247
"""
244248
file_path = _init_resources_file(task_name)
245249
if start_event is None:
246250
start_event = threading.Event()
247251
start_event.set()
248252
stop_event = stop_event or threading.Event()
249-
thread = threading.Thread(target=_log_resources, args=(file_path, start_event, stop_event))
253+
thread = threading.Thread(
254+
target=_log_resources, args=(file_path, start_event, stop_event, sampling_interval)
255+
)
250256
thread.start()
251257

252258
yield

0 commit comments

Comments
 (0)