diff --git a/test/stress/child.js b/test/stress/child.js new file mode 100644 index 00000000..211524c3 --- /dev/null +++ b/test/stress/child.js @@ -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) +}) diff --git a/test/stress/index.js b/test/stress/index.js index ca566a40..32c05100 100644 --- a/test/stress/index.js +++ b/test/stress/index.js @@ -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() diff --git a/test/stress/scripts/index.js b/test/stress/scripts/index.js index be7080af..2612abc5 100644 --- a/test/stress/scripts/index.js +++ b/test/stress/scripts/index.js @@ -13,7 +13,7 @@ async function execCommand (cmd) { } else { console.log(stdout) console.error(stderr) - // resolve({ stdout, stderr }) + resolve({ stdout, stderr }) } }) })