test(profiling): Add basic profiling tests (#1677) · gitcommit90/sentry-python@3bc8bb8 · GitHub
Skip to content

Commit 3bc8bb8

Browse files
authored
test(profiling): Add basic profiling tests (getsentry#1677)
This introduces some basic tests to the setup of the profiler.
1 parent 6e0b02b commit 3bc8bb8

3 files changed

Lines changed: 110 additions & 38 deletions

File tree

tests/conftest.py

Lines changed: 10 additions & 3 deletions

tests/integrations/wsgi/test_wsgi.py

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
import sys
2+
13
from werkzeug.test import Client
24

35
import pytest
46

57
import sentry_sdk
68
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
7-
from sentry_sdk.profiler import teardown_profiler
89
from collections import Counter
9-
from sentry_sdk.utils import PY33
1010

1111
try:
1212
from unittest import mock # python 3.3 and above
@@ -284,38 +284,42 @@ def sample_app(environ, start_response):
284284
assert len(session_aggregates) == 1
285285

286286

287-
if PY33:
288-
289-
@pytest.fixture
290-
def profiling():
291-
yield
292-
teardown_profiler()
287+
@pytest.mark.skipif(
288+
sys.version_info < (3, 3), reason="Profiling is only supported in Python >= 3.3"
289+
)
290+
@pytest.mark.parametrize(
291+
"profiles_sample_rate,profile_count",
292+
[
293+
pytest.param(1.0, 1, id="profiler sampled at 1.0"),
294+
pytest.param(0.75, 1, id="profiler sampled at 0.75"),
295+
pytest.param(0.25, 0, id="profiler not sampled at 0.25"),
296+
pytest.param(None, 0, id="profiler not enabled"),
297+
],
298+
)
299+
def test_profile_sent(
300+
capture_envelopes,
301+
sentry_init,
302+
teardown_profiling,
303+
profiles_sample_rate,
304+
profile_count,
305+
):
306+
def test_app(environ, start_response):
307+
start_response("200 OK", [])
308+
return ["Go get the ball! Good dog!"]
293309

294-
@pytest.mark.parametrize(
295-
"profiles_sample_rate,should_send",
296-
[(1.0, True), (0.75, True), (0.25, False), (None, False)],
310+
sentry_init(
311+
traces_sample_rate=1.0,
312+
_experiments={"profiles_sample_rate": profiles_sample_rate},
297313
)
298-
def test_profile_sent_when_profiling_enabled(
299-
capture_envelopes, sentry_init, profiling, profiles_sample_rate, should_send
300-
):
301-
def test_app(environ, start_response):
302-
start_response("200 OK", [])
303-
return ["Go get the ball! Good dog!"]
304-
305-
sentry_init(
306-
traces_sample_rate=1.0,
307-
_experiments={"profiles_sample_rate": profiles_sample_rate},
308-
)
309-
app = SentryWsgiMiddleware(test_app)
310-
envelopes = capture_envelopes()
311-
312-
with mock.patch("sentry_sdk.profiler.random.random", return_value=0.5):
313-
client = Client(app)
314-
client.get("/")
315-
316-
profile_sent = False
317-
for item in envelopes[0].items:
318-
if item.headers["type"] == "profile":
319-
profile_sent = True
320-
break
321-
assert profile_sent == should_send
314+
app = SentryWsgiMiddleware(test_app)
315+
envelopes = capture_envelopes()
316+
317+
with mock.patch("sentry_sdk.profiler.random.random", return_value=0.5):
318+
client = Client(app)
319+
client.get("/")
320+
321+
count_item_types = Counter()
322+
for envelope in envelopes:
323+
for item in envelope.items:
324+
count_item_types[item.type] += 1
325+
assert count_item_types["profile"] == profile_count

tests/test_profiler.py

Lines changed: 61 additions & 0 deletions

0 commit comments

Comments
 (0)