Skip to content

Commit

Permalink
speed up perf timer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hsheth2 committed Oct 16, 2024
1 parent 88d9554 commit 8110e46
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions metadata-ingestion/tests/unit/utilities/test_perf_timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,39 @@

def test_perf_timer_simple():
with PerfTimer() as timer:
time.sleep(1)
assert approx(timer.elapsed_seconds()) == 1
time.sleep(0.4)
assert approx(timer.elapsed_seconds()) == 0.4

assert approx(timer.elapsed_seconds()) == 1
assert approx(timer.elapsed_seconds()) == 0.4


def test_perf_timer_paused_timer():
with PerfTimer() as current_timer:
time.sleep(1)
assert approx(current_timer.elapsed_seconds()) == 1
time.sleep(0.5)
assert approx(current_timer.elapsed_seconds()) == 0.5
with current_timer.pause():
time.sleep(2)
assert approx(current_timer.elapsed_seconds()) == 1
assert approx(current_timer.elapsed_seconds()) == 1
time.sleep(1)
time.sleep(0.3)
assert approx(current_timer.elapsed_seconds()) == 0.5
assert approx(current_timer.elapsed_seconds()) == 0.5
time.sleep(0.2)

assert approx(current_timer.elapsed_seconds()) == 2
assert approx(current_timer.elapsed_seconds()) == 0.7


def test_generator_with_paused_timer():
n = 4

def generator_function():
with PerfTimer() as inner_timer:
time.sleep(1)
for i in range(10):
for i in range(n):
time.sleep(0.2)
with inner_timer.pause():
time.sleep(0.2)
yield i
assert approx(inner_timer.elapsed_seconds()) == 1 + 0.2 * 10
assert approx(inner_timer.elapsed_seconds()) == 1 + 0.2 * n

with PerfTimer() as outer_timer:
seq = generator_function()
list([i for i in seq])
assert approx(outer_timer.elapsed_seconds()) == 1 + 0.2 * 10 + 0.2 * 10
assert approx(outer_timer.elapsed_seconds()) == 1 + 0.2 * n + 0.2 * n

0 comments on commit 8110e46

Please sign in to comment.