|
| 1 | +/* |
| 2 | + * Copyright 2020 New Relic Corporation. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict' |
| 7 | + |
| 8 | +const test = require('node:test') |
| 9 | +const assert = require('node:assert') |
| 10 | + |
| 11 | +test('Loop Metrics', async (t) => { |
| 12 | + const MICRO_TO_MILLIS = 1e-3 |
| 13 | + const SPIN_TIME = 2000 |
| 14 | + const CPU_EPSILON = SPIN_TIME * 0.05 // Allowed fudge factor for CPU times in MS |
| 15 | + const metricEmitter = require('../..')({ timeout: 10 }) |
| 16 | + const testStart = Date.now() |
| 17 | + |
| 18 | + t.after(() => { |
| 19 | + metricEmitter.unbind() |
| 20 | + }) |
| 21 | + |
| 22 | + // Check the structure of the metric object. |
| 23 | + let metric = metricEmitter.getLoopMetrics().usage |
| 24 | + assert.strictEqual(typeof metric, 'object', 'should provide a metric object') |
| 25 | + assert.strictEqual(typeof metric.total, 'number', 'should have a total') |
| 26 | + assert.strictEqual(typeof metric.min, 'number', 'should have a min') |
| 27 | + assert.strictEqual(typeof metric.max, 'number', 'should have a max') |
| 28 | + assert.strictEqual(typeof metric.sumOfSquares, 'number', 'should have a sumOfSquares') |
| 29 | + assert.strictEqual(typeof metric.count, 'number', 'should have a count') |
| 30 | + |
| 31 | + // Check that the values are reset after the first call. Since this is |
| 32 | + // synchronous with the previous call, all results should be zero. |
| 33 | + metric = metricEmitter.getLoopMetrics().usage |
| 34 | + assert.deepStrictEqual( |
| 35 | + metric, |
| 36 | + { |
| 37 | + total: 0, |
| 38 | + min: 0, |
| 39 | + max: 0, |
| 40 | + sumOfSquares: 0, |
| 41 | + count: 0 |
| 42 | + }, |
| 43 | + 'should reset all the values' |
| 44 | + ) |
| 45 | + |
| 46 | + // Queue up a loop with some CPU burn. |
| 47 | + await new Promise((resolve) => setTimeout(resolve, 100)) |
| 48 | + |
| 49 | + // spinning cpu... |
| 50 | + const start = Date.now() |
| 51 | + while (Date.now() - start < SPIN_TIME) {} // Spin the CPU for 2 seconds. |
| 52 | + |
| 53 | + // Wait another tick and then check the loop stats. |
| 54 | + await new Promise((resolve) => setTimeout(resolve, 100)) |
| 55 | + |
| 56 | + metric = metricEmitter.getLoopMetrics() |
| 57 | + const testDuration = Date.now() - testStart + CPU_EPSILON |
| 58 | + const durationSquare = testDuration * testDuration |
| 59 | + const usage = metric.usage |
| 60 | + |
| 61 | + const meanTime = usage.total / usage.count |
| 62 | + |
| 63 | + assert.ok( |
| 64 | + usage.total * MICRO_TO_MILLIS > SPIN_TIME - CPU_EPSILON, |
| 65 | + 'should have total greater than spin time' |
| 66 | + ) |
| 67 | + assert.ok( |
| 68 | + usage.total * MICRO_TO_MILLIS <= testDuration, |
| 69 | + 'should have total less than wall-clock time' |
| 70 | + ) |
| 71 | + assert.ok(usage.min <= meanTime, 'should have min less than the mean usage time') |
| 72 | + assert.ok(usage.max >= meanTime, 'should have max greater than the mean usage time') |
| 73 | + assert.ok(usage.max * MICRO_TO_MILLIS > SPIN_TIME - CPU_EPSILON, 'should have expected max') |
| 74 | + assert.ok( |
| 75 | + usage.sumOfSquares * MICRO_TO_MILLIS * MICRO_TO_MILLIS < durationSquare, |
| 76 | + 'should have expected sumOfSquares' |
| 77 | + ) |
| 78 | + assert.ok(usage.count >= 2, 'should have expected count') |
| 79 | +}) |
0 commit comments