Question: is it possible to use it in tests?
Closed this issue · 1 comments
Can it be launched in tests so developer is able to specify the maximum depth of call stack for the project and if the call stack depth is higher than the specified limit - it throws some exception?
I think this might be a useful feature for many projects where too large call stack size is prohibited.
Tool is aware of the call stack size in every place of the app (thanks to traces it collects) that means it can theoretically check it in various test scenarios and thus being used in tests along with pytest for example?
Hello @artempronevskiy thanks for open an issue, regarding you question, yes you can add Profyle only on tests:
# test_main.py
from .main import ProfyleMiddleware
from profyle.fastapi import ProfyleMiddleware
app.add_middleware(ProfyleMiddleware)
client = TestClient(app)
def test_read_main():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"msg": "Hello World"}
You have also a flask example in the tests folder
About your suggestion, I don't think profiles should raise that type of error, however, you can use min_duration
or max_stack_depth
attribute to filter traces and only record those ones that fill with your requirements.
ProfyleMiddleware(
min_duration=3000,
max_stack_depth=20
)
In addition, you can access the trace data on testing.
ProfyleMiddleware has the attribute trace_repository
that stores all tracing data, maybe you could use it to do some validations, something like this:
from .main import ProfyleMiddleware
from profyle.fastapi import ProfyleMiddleware
app.add_middleware(ProfyleMiddleware)
client = TestClient(app)
def test_main():
client.get("/")
traces = app.user_middleware[0].trace_repo.get_all_traces()
first_trace = trace[0]
print(first_trace.data) # dict with all trace data