forked from pytorch/rl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
41 lines (33 loc) · 1.07 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
import os
import time
from collections import defaultdict
import pytest
CALL_TIMES = defaultdict(lambda: 0.0)
def pytest_sessionfinish(maxprint=50):
out_str = """
Call times:
===========
"""
maxchar = max(*[len(key) for key in CALL_TIMES.keys()])
for i, (key, item) in enumerate(
sorted(CALL_TIMES.items(), key=lambda x: x[1], reverse=True)
):
spaces = " " + " " * (maxchar - len(key))
out_str += f"\t{key}{spaces}{item: 4.4f}\n"
if i == maxprint - 1:
break
print(out_str)
@pytest.fixture(autouse=True)
def measure_duration(request: pytest.FixtureRequest):
start_time = time.time()
def fin():
duration = time.time() - start_time
name = request.node.name
class_name = request.cls.__name__ if request.cls else None
name = name.split("[")[0]
if class_name is not None:
name = "::".join([class_name, name])
file = os.path.basename(request.path)
name = f"{file}::{name}"
CALL_TIMES[name] = CALL_TIMES[name] + duration
request.addfinalizer(fin)