feat: multiprocessing and some diagnostics
This commit is contained in:
parent
86e437a23a
commit
c75319d166
4 changed files with 40 additions and 9 deletions
|
|
@ -2,6 +2,11 @@ const https = require('https')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const uuid = require('uuid')
|
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) {
|
function getCert (machineIndex) {
|
||||||
try {
|
try {
|
||||||
|
|
@ -23,8 +28,11 @@ function connectionInfo (machineIndex) {
|
||||||
}
|
}
|
||||||
|
|
||||||
let counter = 0
|
let counter = 0
|
||||||
|
const requestTimes = []
|
||||||
|
let latestResponseTime = 0
|
||||||
|
|
||||||
function request (machineIndex, pid) {
|
function request (machineIndex, pid) {
|
||||||
|
performance.mark('A')
|
||||||
https.get({
|
https.get({
|
||||||
hostname: 'localhost',
|
hostname: 'localhost',
|
||||||
port: 3000,
|
port: 3000,
|
||||||
|
|
@ -39,6 +47,9 @@ function request (machineIndex, pid) {
|
||||||
}
|
}
|
||||||
}, res => {
|
}, res => {
|
||||||
res.on('data', (d) => {
|
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() })
|
process.send({ message: Buffer.from(d).toString() })
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
@ -46,9 +57,17 @@ function request (machineIndex, pid) {
|
||||||
counter++
|
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)
|
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()
|
const pid = uuid.v4()
|
||||||
request(msg.machineIndex, pid)
|
request(msg.machineIndex, pid)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,25 @@
|
||||||
|
|
||||||
const { fork } = require('child_process')
|
const { fork } = require('child_process')
|
||||||
|
const minimist = require('minimist')
|
||||||
|
|
||||||
const cmd = require('./scripts')
|
const cmd = require('./scripts')
|
||||||
const variables = require('./utils/variables')
|
const variables = require('./utils/variables')
|
||||||
|
|
||||||
async function createMachines () {
|
async function createMachines (numberOfMachines) {
|
||||||
await cmd.execCommand(
|
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 () {
|
async function run (args = minimist(process.argv.slice(2))) {
|
||||||
await createMachines()
|
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')
|
const forked = fork('child.js')
|
||||||
forked.send({ machineIndex: i })
|
forked.send({ machineIndex: i, hasVariance: HAS_VARIANCE })
|
||||||
forked.on('message', msg => {
|
forked.on('message', msg => {
|
||||||
console.log(`Message from child ${i}: ${msg}`)
|
console.log(`Message from child ${i}: ${msg}`)
|
||||||
})
|
})
|
||||||
|
|
|
||||||
5
test/stress/utils/index.js
Normal file
5
test/stress/utils/index.js
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
function randomIntFromInterval (min, max) {
|
||||||
|
return Math.floor(Math.random() * (max - min + 1) + min)
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { randomIntFromInterval }
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
const NUMBER_OF_MACHINES = 4
|
|
||||||
const SERVER_CERT_PATH = `../../certs/Lamassu_OP_Root_CA.pem`
|
const SERVER_CERT_PATH = `../../certs/Lamassu_OP_Root_CA.pem`
|
||||||
const MACHINE_PATH = `../../../lamassu-machine`
|
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 }
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue