Skip to content

Instantly share code, notes, and snippets.

@pieh

pieh/test.js Secret

Last active June 25, 2021 15:05
Show Gist options
  • Save pieh/9c3f6dbdd3acafa5a8811418eca4d356 to your computer and use it in GitHub Desktop.
Save pieh/9c3f6dbdd3acafa5a8811418eca4d356 to your computer and use it in GitHub Desktop.
test.js
/**
* Copy of https://github.com/facebook/jest/tree/48cda778b285d32e47b3a1bae0f64fffe6601ce0/packages/jest-worker/src/__performance_tests__
* adjusted to measure `jest-worker` vs `gatsby-worker`
*/
"use strict"
const assert = require(`assert`)
const { performance } = require(`perf_hooks`)
const GatsbyWorker = require(`../dist`).WorkerPool
const JestWorker = require(`jest-worker`).Worker
// Typical tests: node --expose-gc test.js empty 100000
// node --expose-gc test.js loadTest 10000
assert(process.argv[2], `Pass a child method name`)
assert(process.argv[3], `Pass the number of iterations`)
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
const method = process.argv[2]
const calls = +process.argv[3]
const threads = 6
const iterations = 10
function testJestWorker() {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async resolve => {
const startTime = performance.now()
let count = 0
async function countToFinish() {
if (++count === calls) {
farm.end()
const endTime = performance.now()
// Let all workers go down.
await sleep(2000)
resolve({
globalTime: endTime - startTime - 2000,
processingTime: endTime - startProcess,
})
}
}
const farm = new JestWorker(require.resolve(`./workers/worker`), {
exposedMethods: [method],
forkOptions: { execArgv: [] },
numWorkers: threads,
})
farm.getStdout().pipe(process.stdout)
farm.getStderr().pipe(process.stderr)
// Let all workers come up.
await sleep(2000)
const startProcess = performance.now()
for (let i = 0; i < calls; i++) {
const promisified = farm[method]()
promisified.then(countToFinish)
}
})
}
function testGatsbyWorker() {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async resolve => {
const startTime = performance.now()
let count = 0
async function countToFinish() {
if (++count === calls) {
farm.end()
const endTime = performance.now()
// Let all workers go down.
await sleep(2000)
resolve({
globalTime: endTime - startTime - 2000,
processingTime: endTime - startProcess,
})
}
}
const farm = new GatsbyWorker(require.resolve(`./workers/worker`), {
numWorkers: threads,
})
// farm.getStdout().pipe(process.stdout)
// farm.getStderr().pipe(process.stderr)
// Let all workers come up.
await sleep(2000)
const startProcess = performance.now()
for (let i = 0; i < calls; i++) {
const promisified = farm.single[method]()
promisified.then(countToFinish)
}
})
}
function profile(x) {
console.profile(x)
}
function profileEnd(x) {
console.profileEnd(x)
}
async function main() {
if (!global.gc) {
console.warn(`GC not present, start with node --expose-gc`)
}
const gWResults = []
const jWResults = []
for (let i = 0; i < iterations; i++) {
console.log(`-`.repeat(75))
profile(`worker farm`)
const gW = await testGatsbyWorker()
profileEnd(`worker farm`)
await sleep(3000)
if (global.gc) {
global.gc()
}
profile(`jest worker`)
const jW = await testJestWorker()
profileEnd(`jest worker`)
await sleep(3000)
if (global.gc) {
global.gc()
}
gWResults.push(gW)
jWResults.push(jW)
console.log(`jest-worker:`, jW)
console.log(`gatsby-worker:`, gW)
}
let gWGT = 0
let gWPT = 0
let jWGT = 0
let jWPT = 0
for (let i = 0; i < iterations; i++) {
gWGT += gWResults[i].globalTime
gWPT += gWResults[i].processingTime
jWGT += jWResults[i].globalTime
jWPT += jWResults[i].processingTime
}
console.log(`-`.repeat(75))
console.log(`total gatsby-worker:`, { gWGT, gWPT })
console.log(`total jest-worker:`, { jWGT, jWPT })
console.log(`-`.repeat(75))
if (gWGT < jWGT) {
console.log(
`global time: gatsby-worker over ${calls} calls was faster by %`, (100 * (jWGT - gWGT)) / jWGT
)
} else {
console.log(
`global time: jest-worker over ${calls} calls was faster by %`, (100 * (gWGT - jWGT)) / gWGT
)
}
if (gWPT < jWPT) {
console.log(
`processing time: gatsby-worker over ${calls} calls was faster by %`, (100 * (jWPT - gWPT)) / jWPT
)
} else {
console.log(
`processing time: jest-worker over ${calls} calls was faster by %`, (100 * (gWPT - jWPT)) / gWPT
)
}
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment