test_performance_issues.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import random
  2. import string
  3. from datetime import datetime, timedelta
  4. from unittest.mock import patch
  5. import pytz
  6. from fixtures.page_objects.issue_details import IssueDetailsPage
  7. from sentry import options
  8. from sentry.models import Group
  9. from sentry.testutils import AcceptanceTestCase, SnubaTestCase
  10. from sentry.utils import json
  11. FEATURES = {
  12. "projects:performance-suspect-spans-ingestion": True,
  13. "organizations:performance-issues": True,
  14. "organizations:performance-issues-ingest": True,
  15. }
  16. class PerformanceIssuesTest(AcceptanceTestCase, SnubaTestCase):
  17. def setUp(self):
  18. super().setUp()
  19. self.org = self.create_organization(owner=self.user, name="Rowdy Tiger")
  20. self.team = self.create_team(
  21. organization=self.org, name="Mariachi Band", members=[self.user]
  22. )
  23. self.project = self.create_project(organization=self.org, teams=[self.team], name="Bengal")
  24. self.login_as(self.user)
  25. options.set("performance.issues.all.problem-detection", 1.0)
  26. options.set("performance.issues.all.problem-creation", 1.0)
  27. options.set("performance.issues.n_plus_one_db.problem-creation", 1.0)
  28. self.page = IssueDetailsPage(self.browser, self.client)
  29. self.dismiss_assistant()
  30. def create_sample_event(self, start_timestamp):
  31. event = json.loads(
  32. self.load_fixture("events/performance_problems/n-plus-one-in-django-new-view.json")
  33. )
  34. for key in ["datetime", "location", "title"]:
  35. del event[key]
  36. event["contexts"] = {
  37. "trace": {"trace_id": "530c14e044aa464db6ddb43660e6474f", "span_id": "139fcdb7c5534eb4"}
  38. }
  39. ms_delta = start_timestamp - event["start_timestamp"]
  40. for item in [event, *event["spans"]]:
  41. item["start_timestamp"] += ms_delta
  42. item["timestamp"] += ms_delta
  43. return event
  44. def randomize_span_description(self, span):
  45. return {
  46. **span,
  47. "description": "".join(random.choice(string.ascii_lowercase) for _ in range(10)),
  48. }
  49. @patch("django.utils.timezone.now")
  50. def test_with_one_performance_issue(self, mock_now):
  51. mock_now.return_value = datetime.utcnow().replace(tzinfo=pytz.utc) - timedelta(minutes=5)
  52. event_data = self.create_sample_event(mock_now.return_value.timestamp())
  53. with self.feature(FEATURES):
  54. event = self.store_event(data=event_data, project_id=self.project.id)
  55. self.page.visit_issue(self.org.slug, event.groups[0].id)
  56. self.browser.click('[aria-label="Show Details"]')
  57. self.browser.snapshot("performance issue details", desktop_only=True)
  58. @patch("django.utils.timezone.now")
  59. def test_multiple_events_with_one_cause_are_grouped(self, mock_now):
  60. mock_now.return_value = datetime.utcnow().replace(tzinfo=pytz.utc) - timedelta(minutes=5)
  61. event_data = self.create_sample_event(mock_now.return_value.timestamp())
  62. with self.feature(FEATURES):
  63. [self.store_event(data=event_data, project_id=self.project.id) for _ in range(3)]
  64. assert Group.objects.count() == 1
  65. @patch("django.utils.timezone.now")
  66. def test_multiple_events_with_multiple_causes_are_not_grouped(self, mock_now):
  67. mock_now.return_value = datetime.utcnow().replace(tzinfo=pytz.utc) - timedelta(minutes=5)
  68. # Create identical events with different parent spans
  69. for _ in range(3):
  70. event_data = self.create_sample_event(mock_now.return_value.timestamp())
  71. event_data["spans"] = [
  72. self.randomize_span_description(span) if span["op"] == "django.view" else span
  73. for span in event_data["spans"]
  74. ]
  75. with self.feature(FEATURES):
  76. self.store_event(data=event_data, project_id=self.project.id)
  77. assert Group.objects.count() == 3