Skip to content

Commit

Permalink
feat: Add test for api timing events
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnuon committed Jan 4, 2024
1 parent 87294f9 commit 40d9a73
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 56 deletions.
20 changes: 13 additions & 7 deletions search/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,21 @@ def perform_search(
processing_timer.stop()
search_timer.stop()

emit_api_timing_event(search_term, course_id, filter_generation_timer, processing_timer, search_timer)
return results

def emit_api_timing_event(search_term, course_id, filter_generation_timer, processing_timer, search_timer):
"""
Emit the timing events for the search API
"""
track.emit("edx.course.search.executed", {
"search_term": search_term,
"course_id": course_id,
"filter_generation_time": {
"start": filter_generation_timer.start_time,
"end": filter_generation_timer.end_time,
"elapsed": filter_generation_timer.elapsed_time,
},
"processing_time": {
"start": processing_timer.start_time_string,
"end": processing_timer.start_time_string,
Expand All @@ -105,14 +118,7 @@ def perform_search(
"end": search_timer.start_time_string,
"elapsed": search_timer.elapsed_time,
},
"filter_generation_time": {
"start": filter_generation_timer.start_time,
"end": filter_generation_timer.end_time,
"elapsed": filter_generation_timer.elapsed_time,
}
})
return results


def course_discovery_search(search_term=None, size=20, from_=0, field_dictionary=None):
"""
Expand Down
108 changes: 108 additions & 0 deletions search/tests/test_api_timing_events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# pylint: disable=too-few-public-methods
# pylint: disable=too-many-ancestors
""" Tests for timing functionality """

import datetime

from django.test import TestCase
from django.test.utils import override_settings
from search.tests.mock_search_engine import MockSearchEngine
from search.utils import Timer
from search.api import emit_api_timing_event
from unittest.mock import patch, call

@override_settings(SEARCH_ENGINE="search.tests.mock_search_engine.MockSearchEngine")
class TimingEventsTest(TestCase):
""" Tests to see if timing events are emitted"""

def setUp(self):
MockSearchEngine.destroy()
patcher = patch('search.api.track')
self.mock_track = patcher.start()
self.addCleanup(patcher.stop)

def tearDown(self):
MockSearchEngine.destroy()
return super().tearDown()

def test_perform_search(self):
# pylint: disable=protected-access

search_term = "testing search"
course_id = "mock.course.id"

filter_generation_timer = Timer()
filter_generation_timer.start()
filter_generation_timer.stop()

search_timer = Timer()
search_timer.start()
search_timer.stop()

processing_timer = Timer()
processing_timer.start()
processing_timer.stop()

emit_api_timing_event(search_term, course_id, filter_generation_timer, processing_timer, search_timer)
timing_event_call = self.mock_track.emit.mock_calls[0]
expected_call = call("edx.course.search.executed", {
"search_term": search_term,
"filter_generation_time": {
"start": filter_generation_timer.start_time,
"end": filter_generation_timer.end_time,
"elapsed": filter_generation_timer.elapsed_time,
},
"processing_time": {
"start": processing_timer.start_time_string,
"end": processing_timer.start_time_string,
"elapsed": processing_timer.elapsed_time,
},
"search_time": {
"start": search_timer.start_time_string,
"end": search_timer.start_time_string,
"elapsed": search_timer.elapsed_time,
},
})
self.assertEqual(timing_event_call, expected_call)


class TimerTest(TestCase):
"""
Timer Test Case
"""

def test_start_timer(self):
timer = Timer()
timer.start()
timer.stop()
self.assertIsNotNone(timer.start_time)
self.assertIsNotNone(timer.end_time)

def test_elapsed_time(self):
# pylint: disable=protected-access

start = datetime.datetime(2024, 1, 1, 0, 0, 0, 0)
end = start + datetime.timedelta(seconds=5)

timer = Timer()
timer._start_time = start
timer._end_time = end

self.assertEqual(timer.elapsed_time, 5)
self.assertEqual(timer.start_time, start)
self.assertEqual(timer.end_time, end)

def test_elapsed_time_string(self):
# pylint: disable=protected-access

start = datetime.datetime(2024, 1, 1, 0, 0, 0, 0)
end = start + datetime.timedelta(seconds=5)

timer = Timer()
timer._start_time = start
timer._end_time = end

self.assertEqual(timer.elapsed_time, 5)
self.assertEqual(timer.start_time_string, "2024-01-01T00:00:00")
self.assertEqual(timer.end_time_string, "2024-01-01T00:00:05")
self.assertGreaterEqual(timer.end_time, timer.start_time)
49 changes: 0 additions & 49 deletions search/tests/test_timer.py

This file was deleted.

0 comments on commit 40d9a73

Please sign in to comment.