-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconftest.py
116 lines (92 loc) · 3.84 KB
/
conftest.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# SPDX-FileCopyrightText: © 2023 Tenstorrent Inc.
# SPDX-License-Identifier: Apache-2.0
import time
import pytest
import psutil
import threading
from loguru import logger
from datetime import datetime
@pytest.fixture(scope="function", autouse=True)
def record_test_timestamp(record_property):
start_timestamp = datetime.strftime(datetime.now(), "%Y-%m-%dT%H:%M:%S%z")
record_property("start_timestamp", start_timestamp)
yield
end_timestamp = datetime.strftime(datetime.now(), "%Y-%m-%dT%H:%M:%S%z")
record_property("end_timestamp", end_timestamp)
@pytest.fixture(scope="function", autouse=True)
def record_forge_property(record_property):
"""
A pytest fixture that automatically records a property named 'frontend' with the value 'tt-forge-fe'
for each test function. This fixture is applied to all test functions due to `autouse=True`.
Parameters:
----------
record_property : function
A pytest built-in function used to record test metadata, such as custom properties or
additional information about the test execution.
Yields:
-------
function
The `record_property` function, allowing tests to add additional properties if needed.
Usage:
------
def test_model(record_forge_property):
# Record Forge Property
record_forge_property("key", value)
"""
# Record default properties for forge
record_property("frontend", "tt-forge-fe")
yield record_property
@pytest.fixture(autouse=True)
def memory_usage_tracker():
"""
A pytest fixture that tracks memory usage during the execution of a test.
This fixture automatically tracks the memory usage of the process running the tests.
It starts tracking before the test runs, continues tracking in a background thread during the test,
and stops tracking after the test completes. It logs the memory usage statistics including the
minimum, maximum, average, and total memory usage by the test.
The memory usage is measured in megabytes (MB).
Note:
- This fixture is automatically used for all tests due to the `autouse=True` parameter.
- The interval for memory readings can be adjusted by changing the sleep duration in the `track_memory` function.
- Min, max, and avg memory usage are calculated based on the recorded memory readings from system memory.
"""
process = psutil.Process()
# Initialize memory tracking variables
start_mem = process.memory_info().rss / (1024 * 1024) # MB
min_mem = start_mem
max_mem = start_mem
total_mem = start_mem
count = 1
# Start a background thread or loop to collect memory usage over time
tracking = True
def track_memory():
nonlocal min_mem, max_mem, total_mem, count
while tracking:
current_mem = process.memory_info().rss / (1024 * 1024)
min_mem = min(min_mem, current_mem)
max_mem = max(max_mem, current_mem)
total_mem += current_mem
count += 1
time.sleep(0.1) # Adjust the interval as needed
# Start tracking in a background thread
import threading
tracker_thread = threading.Thread(target=track_memory)
tracker_thread.start()
# Run the test
yield
# Stop tracking and wait for the thread to finish
tracking = False
tracker_thread.join()
# Calculate end memory and memory usage stats
end_mem = process.memory_info().rss / (1024 * 1024) # MB
min_mem = min(min_mem, end_mem)
max_mem = max(max_mem, end_mem)
total_mem += end_mem
count += 1
avg_mem = total_mem / count
# Log memory usage statistics
logger.info(f"Test memory usage:")
logger.info(f" By test: {end_mem - start_mem:.2f} MB")
logger.info(f" Minimum: {min_mem:.2f} MB")
logger.info(f" Maximum: {max_mem:.2f} MB")
logger.info(f" Average: {avg_mem:.2f} MB")