-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #362 from lo1tuma/esm
Switch to ESM
- Loading branch information
Showing
62 changed files
with
373 additions
and
291 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,63 @@ | ||
const os = require('node:os'); | ||
const { performance: performanceHooks } = require('node:perf_hooks'); | ||
const { times, median, map, prop } = require('rambda'); | ||
import os from 'node:os'; | ||
import { performance as performanceHooks } from 'node:perf_hooks'; | ||
import { filter, lte as isLowerThanOrEquals, map, median, prop, times } from 'rambda'; | ||
|
||
const [{ speed: cpuSpeed }] = os.cpus(); | ||
|
||
function clearRequireCache() { | ||
Object.keys(require.cache).forEach((key) => { | ||
delete require.cache[key]; | ||
}); | ||
export { cpuSpeed }; | ||
|
||
export async function importFresh(modulePath) { | ||
const cacheBuster = `${performanceHooks.now()}_${Math.random()}`; | ||
const cacheBustingModulePath = `${modulePath}?buster=${cacheBuster}`; | ||
|
||
await import(cacheBustingModulePath); | ||
} | ||
|
||
function runBenchmark(fn, count) { | ||
const isPositiveNumber = isLowerThanOrEquals(0); | ||
|
||
export function runSyncBenchmark(fn, count) { | ||
const results = []; | ||
|
||
times(() => { | ||
const startTime = performanceHooks.now(); | ||
const startMemory = process.memoryUsage().rss; | ||
const startMemory = process.memoryUsage.rss(); | ||
fn(); | ||
const endTime = performanceHooks.now(); | ||
const endMemory = process.memoryUsage().rss; | ||
const endMemory = process.memoryUsage.rss(); | ||
const duration = endTime - startTime; | ||
const memory = endMemory - startMemory; | ||
|
||
results.push({ duration, memory }); | ||
}, count); | ||
|
||
const medianDuration = median(map(prop('duration'), results)); | ||
const medianMemory = median(map(prop('memory'), results)); | ||
const medianMemory = median(filter(isPositiveNumber, map(prop('memory'), results))); | ||
|
||
return { medianDuration, medianMemory }; | ||
} | ||
|
||
module.exports = { | ||
runBenchmark, | ||
clearRequireCache, | ||
cpuSpeed | ||
}; | ||
async function measureSingleAsyncTask(fn) { | ||
const startTime = performanceHooks.now(); | ||
const startMemory = process.memoryUsage().rss; | ||
await fn(); | ||
const endTime = performanceHooks.now(); | ||
const endMemory = process.memoryUsage().rss; | ||
const duration = endTime - startTime; | ||
const memory = endMemory - startMemory; | ||
|
||
return { duration, memory }; | ||
} | ||
|
||
export async function runAsyncBenchmark(fn, count) { | ||
const results = []; | ||
|
||
for (let iteration = 0; iteration < count; iteration += 1) { | ||
const result = await measureSingleAsyncTask(fn); | ||
results.push(result); | ||
} | ||
|
||
const medianDuration = median(map(prop('duration'), results)); | ||
const medianMemory = median(filter(isPositiveNumber, map(prop('memory'), results))); | ||
|
||
return { medianDuration, medianMemory }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,35 @@ | ||
const assert = require('node:assert'); | ||
const { runBenchmark, cpuSpeed, clearRequireCache } = require('./measure'); | ||
import assert from 'node:assert'; | ||
import { cpuSpeed, importFresh, runAsyncBenchmark } from './measure.js'; | ||
|
||
const iterations = 50; | ||
|
||
describe('startup / require time', function () { | ||
it('should not take longer as the defined budget to require the plugin', function () { | ||
const cpuAgnosticBudget = 85_000; | ||
it('should not take longer as the defined budget to require the plugin', async function () { | ||
const cpuAgnosticBudget = 20_000; | ||
const budget = cpuAgnosticBudget / cpuSpeed; | ||
|
||
const { medianDuration } = runBenchmark(() => { | ||
clearRequireCache(); | ||
require('../index'); | ||
const { medianDuration } = await runAsyncBenchmark(async () => { | ||
await importFresh('../index.js'); | ||
}, iterations); | ||
|
||
assert.strictEqual(medianDuration < budget, true); | ||
assert.strictEqual( | ||
medianDuration < budget, | ||
true, | ||
`Expected duration ${medianDuration} to be lower than budget ${budget}` | ||
); | ||
}); | ||
|
||
it('should not consume more memory as the defined budget', function () { | ||
const budget = 600_000; | ||
it('should not consume more memory as the defined budget', async function () { | ||
const budget = 225_000; | ||
|
||
const { medianMemory } = runBenchmark(() => { | ||
clearRequireCache(); | ||
require('../index'); | ||
const { medianMemory } = await runAsyncBenchmark(async () => { | ||
await importFresh('../index.js'); | ||
}, iterations); | ||
|
||
assert.strictEqual(medianMemory < budget, true); | ||
assert.strictEqual( | ||
medianMemory < budget, | ||
true, | ||
`Expected memory ${medianMemory} to be lower than budget ${budget}` | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.