Skip to content

Commit

Permalink
Add test cases for helper function profileNanos
Browse files Browse the repository at this point in the history
  • Loading branch information
konjac-h authored and NikhilCollooru committed Mar 14, 2024
1 parent e51a143 commit 0572190
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,11 @@ public <V> V profileNanos(String tag, Supplier<V> supplier)
addMetricValueIgnoreZero(tag, NANO, System.nanoTime() - startTime);
return result;
}

public void profileNanosVoid(String tag, Runnable runnable)
{
long startTime = System.nanoTime();
runnable.run();
addMetricValueIgnoreZero(tag, NANO, System.nanoTime() - startTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public class TestRuntimeStats
private static final String TEST_METRIC_NAME_3 = "test3";
private static final String TEST_METRIC_NAME_NANO_1 = "test_nano_1";
private static final String TEST_METRIC_NAME_NANO_2 = "test_nano_2";
private static final String TEST_METRIC_NAME_NANO_3 = "test_nano_3";
private static final String TEST_METRIC_NAME_BYTE = "test_byte";
private static final long ONE_SECOND_IN_NANOS = 1_000_000_000L;

private void assertRuntimeMetricEquals(RuntimeMetric m1, RuntimeMetric m2)
{
Expand Down Expand Up @@ -234,4 +236,23 @@ public void testReturnUnmodifiedMetrics()
RuntimeStats stats = new RuntimeStats();
stats.getMetrics().put(TEST_METRIC_NAME_1, new RuntimeMetric(TEST_METRIC_NAME_1, NONE));
}

@Test
public void testProfileNano()
{
RuntimeStats stats = new RuntimeStats();
int status = stats.profileNanos(TEST_METRIC_NAME_NANO_3, () -> 1);

assert stats.getMetric(TEST_METRIC_NAME_NANO_3).getSum() < ONE_SECOND_IN_NANOS;
assertEquals(status, 1);
}

@Test
public void testProfileNanoVoid()
{
RuntimeStats stats = new RuntimeStats();
stats.profileNanosVoid(TEST_METRIC_NAME_NANO_3, () -> {});

assert stats.getMetric(TEST_METRIC_NAME_NANO_3).getSum() < ONE_SECOND_IN_NANOS;
}
}

0 comments on commit 0572190

Please sign in to comment.