feat: open child process for each mock machine

This commit is contained in:
Sérgio Salgado 2021-01-13 14:15:47 +00:00 committed by Josh Harvey
parent 20727a60cd
commit 86e437a23a
3 changed files with 70 additions and 55 deletions

59
test/stress/child.js Normal file
View file

@ -0,0 +1,59 @@
const https = require('https')
const path = require('path')
const fs = require('fs')
const uuid = require('uuid')
function getCert (machineIndex) {
try {
return {
key: fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'client.key')),
cert: fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'client.pem'))
}
} catch (e) {
return null
}
}
function connectionInfo (machineIndex) {
try {
return JSON.parse(fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'connection_info.json')))
} catch (e) {
return null
}
}
let counter = 0
function request (machineIndex, pid) {
https.get({
hostname: 'localhost',
port: 3000,
path: '/poll?state=chooseCoin&model=unknown&version=7.5.0-beta.0&idle=true&pid=' + pid + '&sn=' + counter,
method: 'GET',
key: getCert(machineIndex).key,
cert: getCert(machineIndex).cert,
ca: connectionInfo(machineIndex).ca,
headers: {
date: new Date().toISOString(),
'request-id': uuid.v4()
}
}, res => {
res.on('data', (d) => {
process.send({ message: Buffer.from(d).toString() })
})
})
counter++
}
process.on('message', (msg) => {
console.log('Message from parent:', msg)
const pid = uuid.v4()
request(msg.machineIndex, pid)
setInterval(() => {
const pid = uuid.v4()
request(msg.machineIndex, pid)
}, 5000)
})

View file

@ -1,7 +1,5 @@
const https = require('https')
const path = require('path')
const fs = require('fs')
const uuid = require('uuid')
const { fork } = require('child_process')
const cmd = require('./scripts')
const variables = require('./utils/variables')
@ -11,58 +9,16 @@ async function createMachines () {
)
}
function getCert (machineIndex) {
try {
return {
key: fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'client.key')),
cert: fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'client.pem'))
}
} catch (e) {
return null
}
}
async function run () {
await createMachines()
function connectionInfo (machineIndex) {
console.log(machineIndex)
try {
return JSON.parse(fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'connection_info.json')))
} catch (e) {
return null
}
}
let index = 0
function request (machineIndex, pid) {
https.get({
hostname: 'localhost',
port: 3000,
path: '/poll?state=chooseCoin&model=unknown&version=7.5.0-beta.0&idle=true&pid=' + pid + '&sn=' + index,
method: 'GET',
key: getCert(machineIndex).key,
cert: getCert(machineIndex).cert,
ca: connectionInfo(machineIndex).ca,
headers: {
date: new Date().toISOString(),
'request-id': uuid.v4()
}
}, res => {
res.on('data', (d) => {
console.log(Buffer.from(d).toString())
for (let i = 1; i <= variables.NUMBER_OF_MACHINES; i++) {
const forked = fork('child.js')
forked.send({ machineIndex: i })
forked.on('message', msg => {
console.log(`Message from child ${i}: ${msg}`)
})
})
index++
}
function run () {
createMachines().then(() => {
for (let i = 1; i <= variables.NUMBER_OF_MACHINES; i++) {
const pid = uuid.v4()
request(i, pid)
setInterval(() => request(i, pid), 5000)
}
})
}
}
run()

View file

@ -13,7 +13,7 @@ async function execCommand (cmd) {
} else {
console.log(stdout)
console.error(stderr)
// resolve({ stdout, stderr })
resolve({ stdout, stderr })
}
})
})