Skip to content

Commit

Permalink
Toned back comments
Browse files Browse the repository at this point in the history
  • Loading branch information
TeachMeTW committed Oct 18, 2024
1 parent db1e7bb commit 0b39803
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions emission/tests/storageTests/TestStatsQueries.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,25 @@ def verify_stats_entries(self, expected_entries: list[tuple[str, str, float]]):
:param expected_entries: A list of tuples containing (key, name, expected_elapsed_ms).
"""
# Log the number of entries expected to exist in the database.
logging.debug(f"Ensuring {len(expected_entries)} entries exist in DB.")
# Extract the keys from the expected entries for querying the database.
# Prepare keys for database query based on expected entries.
key_list = [key for (key, _, _) in expected_entries]
# Retrieve the stored entries from the database matching the keys.
# Fetch matching entries from the timeseries database.
stored_entrys = list(self.timeseries_db.find_entries(key_list))
# Assert that the number of stored entries matches the number of expected entries.
# Check if the number of retrieved entries matches expectations.
self.assertEqual(len(stored_entrys), len(expected_entries))

# Iterate over each expected entry to verify its correctness.
# Validate each stored entry against the expected data.
for i in range(len(expected_entries)):
stored_entry = stored_entrys[i]
expected_key, expected_name, expected_reading = expected_entries[i]
# Log the comparison between expected and stored entries.
logging.debug(f"Comparing expected {expected_entries[i]} " +
f"with stored {stored_entry['metadata']['key']} {stored_entry['data']}")
# Assert that the stored key matches the expected key.
# Verify the key matches.
self.assertEqual(stored_entry['metadata']['key'], expected_key)
# Assert that the stored name matches the expected name.
# Verify the name matches.
self.assertEqual(stored_entry['data']['name'], expected_name)
# Assert that the stored reading (elapsed time) matches the expected value.
# Verify the elapsed time is as expected.
self.assertEqual(stored_entry['data']['reading'], expected_reading)

def test_single_function_timing(self):
Expand All @@ -68,14 +66,14 @@ def sample_function():
time.sleep(2) # Simulate processing time by sleeping for 2 seconds.
return True

# Use the Timer context manager to measure the execution time of 'sample_function'.
# Measure the execution time of 'sample_function'.
with ect.Timer() as timer:
sample_function()

# Store the timing information in the database under the key 'sample_function'.
# Record the timing data in the database.
esdsq.store_dashboard_time("sample_function", timer)

# Verify that the timing entry was stored correctly in the database.
# Confirm the timing was recorded correctly.
self.verify_stats_entries([
("stats/dashboard_time", "sample_function", timer.elapsed_ms)
])
Expand All @@ -96,31 +94,29 @@ def function_two():
# Simulate processing time by sleeping for 1.5 seconds.
return time.sleep(1.5)

# Start the overall timer for both functions.
# Track the total time for both functions.
with ect.Timer() as timer_both:
# Start and stop the timer for 'function_one'.
# Time 'function_one' execution.
with ect.Timer() as timer_one:
function_one()
# Store the timing information for 'function_one'.
# Record 'function_one' timing.
esdsq.store_dashboard_time("function_one", timer_one)

# Start and stop the timer for 'function_two'.
# Time 'function_two' execution.
with ect.Timer() as timer_two:
function_two()
# Store the timing information for 'function_two'.
# Record 'function_two' timing.
esdsq.store_dashboard_time("function_two", timer_two)

# Store the combined timing information for both functions.
# Record the combined timing for both functions.
esdsq.store_dashboard_time("functions_one_and_two", timer_both)

# Assert that the elapsed time for 'function_one' is approximately 1000ms (1 second).
# Validate individual and combined timings.
self.assertAlmostEqual(timer_one.elapsed_ms, 1000, delta=100)
# Assert that the elapsed time for 'function_two' is approximately 1500ms (1.5 seconds).
self.assertAlmostEqual(timer_two.elapsed_ms, 1500, delta=100)
# Assert that the combined elapsed time is approximately 2500ms (2.5 seconds).
self.assertAlmostEqual(timer_both.elapsed_ms, 2500, delta=100)

# Verify that all timing entries were stored correctly in the database.
# Ensure all timing entries are correctly stored.
self.verify_stats_entries([
("stats/dashboard_time", "function_one", timer_one.elapsed_ms),
("stats/dashboard_time", "function_two", timer_two.elapsed_ms),
Expand All @@ -139,26 +135,23 @@ def faulty_function():
raise ValueError("Simulated error in faulty_function")

try:
# Attempt to execute the faulty function while timing its execution.
# Attempt to execute and time the faulty function.
with ect.Timer() as timer:
faulty_function()
except ValueError as e:
# Catch the expected ValueError exception.
# Handle the expected exception and record the timing as an error.
logging.info(f"Caught expected error: {e}")
# Store the timing information as an error in the database under 'faulty_function'.
esdsq.store_dashboard_error('faulty_function', timer)
# Pass to continue execution after handling the exception.
# Continue after handling the exception.
pass

# Verify that the error timing entry was stored correctly in the database.
# Verify that the error timing was recorded.
self.verify_stats_entries([
("stats/dashboard_error", "faulty_function", timer.elapsed_ms)
])



if __name__ == '__main__':
# Configure logging settings before running the tests.
etc.configLogging()
# Run all the test cases defined in the TestFunctionTiming class.
unittest.main()

0 comments on commit 0b39803

Please sign in to comment.