Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr committed Feb 27, 2024
1 parent 44a6187 commit bd2bb53
Showing 1 changed file with 35 additions and 31 deletions.
66 changes: 35 additions & 31 deletions tests/unit/loop-metrics.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,43 +51,47 @@ tap.test('Loop Metrics', function (t) {
// XXX Keep this as a timeout. On Node v4 it causes an extra period of idle
// wait on IO due to a bug in timers. This time should not be counted in
// the actual loop time because the process isn't doing anything.
setTimeout(function spinner() {
setTimeout(spinner, 5)

function spinner() {
t.comment('spinning cpu...')
const start = Date.now()
while (Date.now() - start < SPIN_TIME) {} // Spin the CPU for 2 seconds.

// Finally, wait another tick and then check the loop stats.
setTimeout(function () {
metric = metricEmitter.getLoopMetrics()
const testDuration = Date.now() - testStart + CPU_EPSILON
const durationSquare = testDuration * testDuration
const usage = metric.usage
process.nextTick(afterSpin)
}

const meanTime = usage.total / usage.count
if (process.arch === 'arm64') {
console.log(
`{ min: ${usage.min}, max: ${usage.max}, meanTime: ${meanTime}, count: ${usage.count}, total: ${usage.total} }`
)
}
t.ok(
usage.total * MICRO_TO_MILLIS > SPIN_TIME - CPU_EPSILON,
'should have total greater than spin time'
)
t.ok(
usage.total * MICRO_TO_MILLIS <= testDuration,
'should have total less than wall-clock time'
)
t.ok(usage.min <= meanTime, 'should have min less than the mean usage time')
t.ok(usage.max >= meanTime, 'should have max greater than the mean usage time')
t.ok(usage.max * MICRO_TO_MILLIS > SPIN_TIME - CPU_EPSILON, 'should have expected max')
t.ok(
usage.sumOfSquares * MICRO_TO_MILLIS * MICRO_TO_MILLIS < durationSquare,
'should have expected sumOfSquares'
function afterSpin() {
metric = metricEmitter.getLoopMetrics()
const testDuration = Date.now() - testStart + CPU_EPSILON
const durationSquare = testDuration * testDuration
const usage = metric.usage

const meanTime = usage.total / usage.count
if (process.arch === 'arm64') {
t.comment(
`{ min: ${usage.min}, max: ${usage.max}, meanTime: ${meanTime}, count: ${usage.count}, total: ${usage.total} }`
)
t.ok(usage.count >= 2, 'should have expected count')
}
t.ok(
usage.total * MICRO_TO_MILLIS > SPIN_TIME - CPU_EPSILON,
'should have total greater than spin time'
)
t.ok(
usage.total * MICRO_TO_MILLIS <= testDuration,
'should have total less than wall-clock time'
)
t.ok(usage.min <= meanTime, 'should have min less than the mean usage time')
t.ok(usage.max >= meanTime, 'should have max greater than the mean usage time')
t.ok(usage.max * MICRO_TO_MILLIS > SPIN_TIME - CPU_EPSILON, 'should have expected max')
t.ok(
usage.sumOfSquares * MICRO_TO_MILLIS * MICRO_TO_MILLIS < durationSquare,
'should have expected sumOfSquares'
)
t.ok(usage.count >= 2, 'should have expected count')

// Done!
t.end()
}, 5)
}, 5)
// Done!
t.end()
}
})

0 comments on commit bd2bb53

Please sign in to comment.