Skip to content

Commit

Permalink
fix: fix types and remove dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
dnlup committed Nov 2, 2023
1 parent eb20316 commit abdfbee
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 96 deletions.
3 changes: 0 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ declare function createSampler(options?: SamplerOptions): Sampler
export default createSampler

export { createSampler }
export const eventLoopUtilizationSupported: Boolean
export const resourceUsageSupported: Boolean
export const gcFlagsSupported: Boolean
export { Sampler, SamplerOptions }
export { errors }
export * from './types/cpuMetric'
Expand Down
19 changes: 0 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const Sampler = require('./lib/sampler')
const util = require('./lib/util')
const errors = require('./lib/errors')

function createSampler (options = {}) {
Expand All @@ -14,21 +13,3 @@ module.exports.default = createSampler

module.exports.Sampler = Sampler
module.exports.errors = errors

Object.defineProperty(module.exports, 'eventLoopUtilizationSupported', {
value: util.eventLoopUtilizationSupported,
writable: false,
enumerable: true
})

Object.defineProperty(module.exports, 'resourceUsageSupported', {
value: util.resourceUsageSupported,
writable: false,
enumerable: true
})

Object.defineProperty(module.exports, 'gcFlagsSupported', {
value: util.gcFlagsSupported,
writable: false,
enumerable: true
})
6 changes: 0 additions & 6 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { expectType, expectError, expectAssignable } from 'tsd'
import {
createSampler,
eventLoopUtilizationSupported,
resourceUsageSupported,
gcFlagsSupported,
Sampler,
errors,
CPUMetric,
Expand All @@ -13,9 +10,6 @@ import {
GCMetric
} from '.'

expectType<Boolean>(eventLoopUtilizationSupported)
expectType<Boolean>(resourceUsageSupported)
expectType<Boolean>(gcFlagsSupported)
expectAssignable<errors.NotSupportedError>(new errors.NotSupportedError())
expectAssignable<'NotSupportedError'>(new errors.NotSupportedError().name)
expectAssignable<'DOC_ERR_NOT_SUPPORTED'>(new errors.NotSupportedError().code)
Expand Down
26 changes: 4 additions & 22 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
'use strict'

const debug = require('./debug')
const {
monitorEventLoopDelaySupported,
eventLoopUtilizationSupported,
resourceUsageSupported,
gcFlagsSupported
} = require('./util')
const { InvalidArgumentError, NotSupportedError } = require('./errors')
const { InvalidArgumentError } = require('./errors')

// This object serves as both a default config and config schema.
const DEFAULTS = {
sampleInterval: monitorEventLoopDelaySupported ? 1000 : 500,
sampleInterval: 1000,
autoStart: true,
unref: true,
eventLoopDelayOptions: {
resolution: 10
},
gcOptions: {
aggregate: false,
flags: gcFlagsSupported
flags: true
},
collect: {
cpu: true,
memory: true,
resourceUsage: false,
eventLoopDelay: true,
eventLoopUtilization: eventLoopUtilizationSupported,
eventLoopUtilization: true,
gc: false,
activeHandles: false
}
Expand Down Expand Up @@ -81,18 +75,6 @@ function validate (conf) {
}
}

if (conf.gcOptions.flags && !gcFlagsSupported) {
throw new NotSupportedError('GC flags are not supported on the Node.js version used')
}

if (conf.collect.eventLoopUtilization && !eventLoopUtilizationSupported) {
throw new NotSupportedError('eventLoopUtilization is not supported on the Node.js version used')
}

if (conf.collect.resourceUsage && !resourceUsageSupported) {
throw new NotSupportedError('resourceUsage is not supported on the Node.js version used')
}

if (conf.collect.cpu && conf.collect.resourceUsage) {
conf.collect.cpu = false
debug('disabling cpu metric because resourceUsage is enabled')
Expand Down
4 changes: 0 additions & 4 deletions lib/resourceUsage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ const {
kSample,
kReset
} = require('./symbols')
const { resourceUsageSupported } = require('./util')

const kLastSample = Symbol('kLastSample')

class ResourceUsageMetric {
constructor () {
if (!resourceUsageSupported) {
throw new Error('resourceUsage is not supported on this Node.js version')
}
this[kLastSample] = process.resourceUsage()
this[kRawMetric] = this[kLastSample]
this[kComputedMetric] = 0
Expand Down
14 changes: 0 additions & 14 deletions lib/util.js

This file was deleted.

2 changes: 1 addition & 1 deletion lint-staged.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ module.exports = {
const list = filenames.map(filename => `'markdown-toc -i ${filename}`)
return list
},
'*.{js,ts}': ['eslint']
'*.{js}': ['eslint']
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"directory": "."
},
"devDependencies": {
"@types/node": "^20.6.0",
"@types/node": "^20.6.3",
"atomic-sleep": "^1.0.0",
"autocannon": "^7.12.0",
"concurrently": "^8.2.1",
Expand Down
16 changes: 5 additions & 11 deletions test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

const { test } = require('tap')
const config = require('../lib/config')
const {
monitorEventLoopDelaySupported,
eventLoopUtilizationSupported,
gcFlagsSupported,
resourceUsageSupported
} = require('../lib/util')
const { InvalidArgumentError } = require('../lib/errors')

test('validation', t => {
Expand Down Expand Up @@ -160,11 +154,11 @@ test('validation', t => {
t.ok(opts.eventLoopDelayOptions.resolution, 10)

opts = config({ sampleInterval: null })
t.ok(opts.sampleInterval, monitorEventLoopDelaySupported ? 1000 : 500)
t.equal(opts.sampleInterval, 1000)
t.end()
})

test('should disable cpu if resourceUsage is enabled', { skip: !resourceUsageSupported }, t => {
test('should disable cpu if resourceUsage is enabled', t => {
const opts = config({
collect: {
cpu: true,
Expand All @@ -178,17 +172,17 @@ test('should disable cpu if resourceUsage is enabled', { skip: !resourceUsageSup

test('default options', t => {
const opts = config()
t.equal(opts.sampleInterval, monitorEventLoopDelaySupported ? 1000 : 500)
t.equal(opts.sampleInterval, 1000)
t.ok(opts.autoStart)
t.ok(opts.unref)
t.same(opts.eventLoopDelayOptions, { resolution: 10 })
t.notOk(opts.gcOptions.aggregate)
t.equal(opts.gcOptions.flags, gcFlagsSupported)
t.equal(opts.gcOptions.flags, true)
t.ok(opts.collect.cpu)
t.ok(opts.collect.memory)
t.notOk(opts.collect.resourceUsage)
t.ok(opts.collect.eventLoopDelay)
t.equal(opts.collect.eventLoopUtilization, eventLoopUtilizationSupported)
t.equal(opts.collect.eventLoopUtilization, true)
t.notOk(opts.collect.gc)
t.notOk(opts.collect.activeHandles)
t.end()
Expand Down
5 changes: 1 addition & 4 deletions test/esm/namedExports.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { strictEqual } from 'assert'
import { doc, Sampler, errors, eventLoopUtilizationSupported, resourceUsageSupported, gcFlagsSupported } from '../../index.js'
import { doc, Sampler, errors } from '../../index.js'

strictEqual(typeof doc, 'function')
strictEqual(typeof Sampler, 'function')
strictEqual(typeof errors, 'object')
strictEqual(typeof eventLoopUtilizationSupported, 'boolean')
strictEqual(typeof resourceUsageSupported, 'boolean')
strictEqual(typeof gcFlagsSupported, 'boolean')
20 changes: 9 additions & 11 deletions types/gcMetric.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HistogramSummary, WasmHistogram } from 'hdr-histogram-js'
import { RecordableHistogram } from 'perf_hooks';

declare enum GCFlag {
/** perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_NO */
Expand All @@ -25,31 +25,29 @@ export class GCEntry {
/**
* Total number of operations counted
*/
totalCount: WasmHistogram['totalCount'];
// @types/node is not aligned with Node implementation. Let's wait for a fix.
// @ts-ignore
totalCount: RecordableHistogram['count'];
/**
* Mean value in nanoseconds
*/
mean: WasmHistogram['mean'];
mean: RecordableHistogram['mean'];
/**
* Max value in nanoseconds
*/
max: WasmHistogram['maxValue'];
max: RecordableHistogram['max'];
/**
* Min value in nanoseconds
*/
min: WasmHistogram['minNonZeroValue'];
min: RecordableHistogram['min'];
/**
* Standard deviation in nanoseconds
*/
stdDeviation: WasmHistogram['stdDeviation'];
/**
* Histogram summary
*/
summary: HistogramSummary;
stdDeviation: RecordableHistogram['stddev'];
/**
* Get a percentile
*/
getValueAtPercentile: WasmHistogram['getValueAtPercentile'];
getValueAtPercentile: RecordableHistogram['percentile'];
}

export class GCAggregatedEntry extends GCEntry {
Expand Down

0 comments on commit abdfbee

Please sign in to comment.