test_performance_issues.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import random
  2. import string
  3. from datetime import timedelta
  4. from unittest import mock
  5. from unittest.mock import patch
  6. import pytz
  7. from fixtures.page_objects.issue_details import IssueDetailsPage
  8. from sentry import options
  9. from sentry.issues.grouptype import (
  10. NoiseConfig,
  11. PerformanceNPlusOneAPICallsGroupType,
  12. PerformanceNPlusOneGroupType,
  13. )
  14. from sentry.issues.ingest import send_issue_occurrence_to_eventstream
  15. from sentry.models import Group
  16. from sentry.testutils import AcceptanceTestCase, SnubaTestCase
  17. from sentry.testutils.cases import PerformanceIssueTestCase
  18. from sentry.testutils.helpers.datetime import before_now
  19. from sentry.utils import json
  20. FEATURES = {
  21. "projects:performance-suspect-spans-ingestion": True,
  22. "organizations:performance-n-plus-one-api-calls-detector": True,
  23. }
  24. class PerformanceIssuesTest(AcceptanceTestCase, SnubaTestCase, PerformanceIssueTestCase):
  25. def setUp(self):
  26. super().setUp()
  27. self.org = self.create_organization(owner=self.user, name="Rowdy Tiger")
  28. self.team = self.create_team(
  29. organization=self.org, name="Mariachi Band", members=[self.user]
  30. )
  31. self.project = self.create_project(organization=self.org, teams=[self.team], name="Bengal")
  32. self.login_as(self.user)
  33. options.set("performance.issues.all.problem-detection", 1.0)
  34. options.set("performance.issues.n_plus_one_db.problem-creation", 1.0)
  35. options.set("performance.issues.n_plus_one_api_calls.problem-creation", 1.0)
  36. self.page = IssueDetailsPage(self.browser, self.client)
  37. self.dismiss_assistant()
  38. def create_sample_event(self, fixture, start_timestamp):
  39. event = json.loads(self.load_fixture(f"events/performance_problems/{fixture}.json"))
  40. for key in ["datetime", "location", "title"]:
  41. del event[key]
  42. event["contexts"] = {
  43. "trace": {"trace_id": "530c14e044aa464db6ddb43660e6474f", "span_id": "139fcdb7c5534eb4"}
  44. }
  45. ms_delta = start_timestamp - event["start_timestamp"]
  46. for item in [event, *event["spans"]]:
  47. item["start_timestamp"] += ms_delta
  48. item["timestamp"] += ms_delta
  49. return event
  50. def randomize_span_description(self, span):
  51. return {
  52. **span,
  53. "description": "".join(random.choice(string.ascii_lowercase) for _ in range(10)),
  54. }
  55. @patch("django.utils.timezone.now")
  56. def test_with_one_performance_issue(self, mock_now):
  57. mock_now.return_value = before_now(minutes=5).replace(tzinfo=pytz.utc)
  58. event_data = self.create_sample_event(
  59. "n-plus-one-in-django-new-view", mock_now.return_value.timestamp()
  60. )
  61. with self.feature(FEATURES), mock.patch(
  62. "sentry.issues.ingest.send_issue_occurrence_to_eventstream",
  63. side_effect=send_issue_occurrence_to_eventstream,
  64. ) as mock_eventstream, mock.patch.object(
  65. PerformanceNPlusOneGroupType,
  66. "noise_config",
  67. new=NoiseConfig(0, timedelta(minutes=1)),
  68. ), self.feature(
  69. "organizations:issue-platform"
  70. ):
  71. self.store_event(data=event_data, project_id=self.project.id)
  72. group = mock_eventstream.call_args[0][2].group
  73. self.page.visit_issue(self.org.slug, group.id)
  74. self.browser.snapshot("performance issue details", desktop_only=True)
  75. @patch("django.utils.timezone.now")
  76. def test_multiple_events_with_one_cause_are_grouped(self, mock_now):
  77. mock_now.return_value = before_now(minutes=5).replace(tzinfo=pytz.utc)
  78. event_data = self.create_sample_event(
  79. "n-plus-one-in-django-new-view", mock_now.return_value.timestamp()
  80. )
  81. self.create_performance_issue(event_data=event_data)
  82. assert Group.objects.count() == 1
  83. @patch("django.utils.timezone.now")
  84. def test_n_one_api_call_performance_issue(self, mock_now):
  85. mock_now.return_value = before_now(minutes=5).replace(tzinfo=pytz.utc)
  86. event_data = self.create_sample_event(
  87. "n-plus-one-api-calls/n-plus-one-api-calls-in-issue-stream",
  88. mock_now.return_value.timestamp(),
  89. )
  90. event_data["contexts"]["trace"]["op"] = "navigation"
  91. with self.feature(FEATURES), mock.patch(
  92. "sentry.issues.ingest.send_issue_occurrence_to_eventstream",
  93. side_effect=send_issue_occurrence_to_eventstream,
  94. ) as mock_eventstream, mock.patch.object(
  95. PerformanceNPlusOneAPICallsGroupType,
  96. "noise_config",
  97. new=NoiseConfig(0, timedelta(minutes=1)),
  98. ), self.feature(
  99. "organizations:issue-platform"
  100. ):
  101. self.store_event(data=event_data, project_id=self.project.id)
  102. group = mock_eventstream.call_args[0][2].group
  103. self.page.visit_issue(self.org.slug, group.id)
  104. self.browser.snapshot("N+1 API Call issue details", desktop_only=True)
  105. @patch("django.utils.timezone.now")
  106. def test_multiple_events_with_multiple_causes_are_not_grouped(self, mock_now):
  107. mock_now.return_value = before_now(minutes=5).replace(tzinfo=pytz.utc)
  108. # Create identical events with different parent spans
  109. for _ in range(3):
  110. event_data = self.create_sample_event(
  111. "n-plus-one-in-django-new-view", mock_now.return_value.timestamp()
  112. )
  113. event_data["spans"] = [
  114. self.randomize_span_description(span) if span["op"] == "django.view" else span
  115. for span in event_data["spans"]
  116. ]
  117. self.create_performance_issue(event_data=event_data)
  118. assert Group.objects.count() == 3