Skip to content

Commit

Permalink
feat: add tests and chagne timer to use datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnuon committed Jan 4, 2024
1 parent 6155763 commit c2855a2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
8 changes: 4 additions & 4 deletions search/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ def perform_search(
track.emit("edx.course.search.executed", {
"search_term": search_term,
"processing_time": {
"start": processing_timer.start_time,
"end": processing_timer.end_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,
"end": search_timer.end_time,
"start": search_timer.start_time_string,
"end": search_timer.start_time_string,
"elapsed": search_timer.elapsed_time,
},
"filter_generation_time": {
Expand Down
37 changes: 37 additions & 0 deletions search/tests/test_timer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from django.test import TestCase
from search.utils import Timer

import datetime

class TimerTest(TestCase):

def test_start_timer(self):
timer = Timer()
timer.start()
timer.stop()
self.assertTrue(timer.start_time is not None)
self.assertTrue(timer.end_time is not None)

def test_elapsed_time(self):
start = datetime.datetime(2024, 1, 0, 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):
start = datetime.datetime(2024, 1, 0, 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")
18 changes: 14 additions & 4 deletions search/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
""" Utility classes to support others """

import importlib
import time
import datetime
from collections.abc import Iterable


Expand Down Expand Up @@ -77,11 +77,11 @@ def __init__(self):

def start(self):
""" Start the timer """
self._start_time = time.time()
self._start_time = datetime.datetime.now()

def stop(self):
""" Stop the timer """
self._end_time = time.time()
self._end_time = datetime.datetime.now()

@property
def start_time(self):
Expand All @@ -93,7 +93,17 @@ def end_time(self):
""" Return the end time """
return self._end_time

@property
def start_time_string(self):
""" use isoformat for the start time """
return self._start_time.isoformat()

@property
def end_time_string(self):
""" use isoformat for the end time """
return self._end_time.isoformat()

@property
def elapsed_time(self):
""" Return the elapsed time """
return self._end_time - self._start_time
return (self._end_time - self._start_time).seconds

0 comments on commit c2855a2

Please sign in to comment.