Skip to content

Commit

Permalink
[Kernel] Fix flaky test for the Timer class for metrics (#3946)
Browse files Browse the repository at this point in the history
<!--
Thanks for sending a pull request!  Here are some tips for you:
1. If this is your first time, please read our contributor guidelines:
https://github.com/delta-io/delta/blob/master/CONTRIBUTING.md
2. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP]
Your PR title ...'.
  3. Be sure to keep the PR description updated to reflect all changes.
  4. Please write your PR title to summarize what this PR proposes.
5. If possible, provide a concise example to reproduce the issue for a
faster review.
6. If applicable, include the corresponding issue number in the PR title
and link it in the body.
-->

#### Which Delta project/connector is this regarding?
<!--
Please add the component selected below to the beginning of the pull
request title
For example: [Spark] Title of my pull request
-->

- [ ] Spark
- [ ] Standalone
- [ ] Flink
- [X] Kernel
- [ ] Other (fill in here)

## Description

Fixes a flaky test.

## How was this patch tested?

Unit test fix.

## Does this PR introduce _any_ user-facing changes?

No.
  • Loading branch information
allisonport-db authored Dec 19, 2024
1 parent da58cad commit ae4982c
Showing 1 changed file with 28 additions and 21 deletions.
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

0 comments on commit ae4982c

Please sign in to comment.