diff --git a/test/stress/child.js b/test/stress/child.js index 211524c3..7e20b114 100644 --- a/test/stress/child.js +++ b/test/stress/child.js @@ -2,6 +2,11 @@ const https = require('https') const path = require('path') const fs = require('fs') const uuid = require('uuid') +const _ = require('lodash/fp') +const { PerformanceObserver, performance } = require('perf_hooks') + +const utils = require('./utils') +const variables = require('./utils/variables') function getCert (machineIndex) { try { @@ -23,8 +28,11 @@ function connectionInfo (machineIndex) { } let counter = 0 +const requestTimes = [] +let latestResponseTime = 0 function request (machineIndex, pid) { + performance.mark('A') https.get({ hostname: 'localhost', port: 3000, @@ -39,6 +47,9 @@ function request (machineIndex, pid) { } }, res => { res.on('data', (d) => { + performance.mark('B') + performance.measure('A to B', 'A', 'B') + console.log(`Machine ${machineIndex} || Avg request response time: ${_.mean(requestTimes).toFixed(3)} || Latest response time: ${latestResponseTime.toFixed(3)}`) process.send({ message: Buffer.from(d).toString() }) }) }) @@ -46,9 +57,17 @@ function request (machineIndex, pid) { counter++ } -process.on('message', (msg) => { +const obs = new PerformanceObserver((items) => { + latestResponseTime = items.getEntries()[0].duration + requestTimes.push(latestResponseTime) + performance.clearMarks() +}) +obs.observe({ entryTypes: ['measure'] }) + +process.on('message', async (msg) => { console.log('Message from parent:', msg) + if (msg.hasVariance) await new Promise(resolve => setTimeout(resolve, utils.randomIntFromInterval(1, variables.POLLING_INTERVAL))) const pid = uuid.v4() request(msg.machineIndex, pid) diff --git a/test/stress/index.js b/test/stress/index.js index 32c05100..2f90427d 100644 --- a/test/stress/index.js +++ b/test/stress/index.js @@ -1,20 +1,25 @@ const { fork } = require('child_process') +const minimist = require('minimist') + const cmd = require('./scripts') const variables = require('./utils/variables') -async function createMachines () { +async function createMachines (numberOfMachines) { await cmd.execCommand( - `bash ./scripts/create-machines.sh ${variables.NUMBER_OF_MACHINES} ${variables.SERVER_CERT_PATH} ${variables.MACHINE_PATH}` + `bash ./scripts/create-machines.sh ${numberOfMachines} ${variables.SERVER_CERT_PATH} ${variables.MACHINE_PATH}` ) } -async function run () { - await createMachines() +async function run (args = minimist(process.argv.slice(2))) { + const NUMBER_OF_MACHINES = args._[0] + const HAS_VARIANCE = args.v || false - for (let i = 1; i <= variables.NUMBER_OF_MACHINES; i++) { + await createMachines(NUMBER_OF_MACHINES) + + for (let i = 1; i <= NUMBER_OF_MACHINES; i++) { const forked = fork('child.js') - forked.send({ machineIndex: i }) + forked.send({ machineIndex: i, hasVariance: HAS_VARIANCE }) forked.on('message', msg => { console.log(`Message from child ${i}: ${msg}`) }) diff --git a/test/stress/utils/index.js b/test/stress/utils/index.js new file mode 100644 index 00000000..e2445705 --- /dev/null +++ b/test/stress/utils/index.js @@ -0,0 +1,5 @@ +function randomIntFromInterval (min, max) { + return Math.floor(Math.random() * (max - min + 1) + min) +} + +module.exports = { randomIntFromInterval } diff --git a/test/stress/utils/variables.js b/test/stress/utils/variables.js index 487ed8fc..53391dd9 100644 --- a/test/stress/utils/variables.js +++ b/test/stress/utils/variables.js @@ -1,5 +1,7 @@ -const NUMBER_OF_MACHINES = 4 const SERVER_CERT_PATH = `../../certs/Lamassu_OP_Root_CA.pem` const MACHINE_PATH = `../../../lamassu-machine` -module.exports = { NUMBER_OF_MACHINES, SERVER_CERT_PATH, MACHINE_PATH } +// Request timers +const POLLING_INTERVAL = 5000 + +module.exports = { SERVER_CERT_PATH, MACHINE_PATH, POLLING_INTERVAL }