-
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.
- Loading branch information
Showing
61 changed files
with
350 additions
and
285 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
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, lt as isLowerThan, 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 isNegative = isLowerThan(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(isNegative, 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(isNegative, 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
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.