Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Kernel] Fix flaky test for the Timer class for metrics #3946

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ class MetricsUtilsSuite extends AnyFunSuite {
// Timer tests //
////////////////

val NANOSECONDS_PER_MILLISECOND = 1000000

def millisToNanos(millis: Long): Long = {
millis*1000000
millis*NANOSECONDS_PER_MILLISECOND
}

/**
Expand All @@ -37,29 +39,34 @@ class MetricsUtilsSuite extends AnyFunSuite {
*/
def testTimer(incrementFx: (Long, Timer) => Unit): Unit = {
val timer = new Timer()
// Verify initial values
assert(timer.count == 0)
assert(timer.totalDuration == 0)

val incrementAmt1 = 0
val paddedEndTimeOp1 = incrementAmt1 + 5 // We pad each operation by 5ms
incrementFx(incrementAmt1, timer)
assert(timer.count == 1)
assert(timer.totalDuration >= millisToNanos(incrementAmt1) &&
timer.totalDuration < millisToNanos(paddedEndTimeOp1))

val incrementAmt2 = 20
val paddedEndTimeOp2 = paddedEndTimeOp1 + incrementAmt2 + 5 // 30
incrementFx(incrementAmt2, timer)
assert(timer.count == 2)
assert(timer.totalDuration >= millisToNanos(incrementAmt1 + incrementAmt2) &&
timer.totalDuration < millisToNanos(paddedEndTimeOp2))

val incrementAmt3 = 50
val paddedEndTimeOp3 = paddedEndTimeOp2 + incrementAmt3 + 5 // 85
incrementFx(incrementAmt3, timer)
assert(timer.count == 3)
assert(timer.totalDuration >= millisToNanos(incrementAmt1 + incrementAmt2 + incrementAmt3) &&
timer.totalDuration < millisToNanos(paddedEndTimeOp3))
def incrementAndCheck(amtMillis: Long): Unit = {
val initialCount = timer.count()
val initialDuration = timer.totalDuration() // in nanoseconds

val startTime = System.currentTimeMillis()
incrementFx(amtMillis, timer)
// upperLimitDuration is in milliseconds; we take the max of time elapsed vs the incrementAmt
val upperLimitDuration = Math.max(
// we pad by 1 due to rounding of nanoseconds to milliseconds for system time
System.currentTimeMillis() - startTime + 1,
amtMillis
)

// check count
assert(timer.count == initialCount + 1)
// check lowerbound
assert(timer.totalDuration >= initialDuration + millisToNanos(amtMillis))
// check upperbound
assert(timer.totalDuration <= initialDuration + millisToNanos(upperLimitDuration))
}

incrementAndCheck(0)
incrementAndCheck(20)
incrementAndCheck(50)
}

test("Timer class") {
Expand Down
Loading