From 722a3d28537b273f873a12276ed1198b23e93e4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Thu, 14 Jan 2021 17:17:22 +0000 Subject: [PATCH] feat: wrapper to a single entrypoint to the stress test --- test/stress/index.js | 8 +++++++- test/stress/scripts/create-machines.sh | 11 ++++++----- test/stress/scripts/index.js | 18 ++++++++++++++---- test/stress/test-server.js | 7 +++++++ 4 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 test/stress/test-server.js diff --git a/test/stress/index.js b/test/stress/index.js index 2f90427d..c22b81f9 100644 --- a/test/stress/index.js +++ b/test/stress/index.js @@ -11,17 +11,23 @@ async function createMachines (numberOfMachines) { ) } +function startServer () { + const forked = fork('test-server.js') + forked.send('start') +} + async function run (args = minimist(process.argv.slice(2))) { const NUMBER_OF_MACHINES = args._[0] const HAS_VARIANCE = args.v || false + startServer() await createMachines(NUMBER_OF_MACHINES) for (let i = 1; i <= NUMBER_OF_MACHINES; i++) { const forked = fork('child.js') forked.send({ machineIndex: i, hasVariance: HAS_VARIANCE }) forked.on('message', msg => { - console.log(`Message from child ${i}: ${msg}`) + console.log(`Machine ${i} || ${msg}`) }) } } diff --git a/test/stress/scripts/create-machines.sh b/test/stress/scripts/create-machines.sh index 285a4178..d85b065d 100644 --- a/test/stress/scripts/create-machines.sh +++ b/test/stress/scripts/create-machines.sh @@ -20,13 +20,14 @@ fi rm -rf ./machines/* # Create stress database -# sudo -u postgres psql postgres -c "drop database if exists lamassu_stress" -# sudo -u postgres psql postgres -c "create database lamassu_stress with template lamassu" +sudo -u postgres psql postgres -c "drop database if exists lamassu_stress" +sudo -u postgres psql postgres -c "create database lamassu_stress with template lamassu" START=1 END=$1 for (( c=$START; c<=$END; c++ )) do + echo "Creating machine $c out of $END..." NUMBER=$c mkdir -p ./machines/$NUMBER/ cp "$3"/data/client.sample.pem ./machines/$NUMBER/ @@ -44,15 +45,15 @@ EOL DEVICE_ID=`openssl x509 -outform der -in ./machines/$NUMBER/client.pem | sha256sum | cut -d " " -f 1` # Update db config - sudo -u postgres psql -t -d lamassu -c "select data from user_config where type='config' order by id desc limit 1" > config.json + sudo -u postgres psql -t -d lamassu_stress -c "select data from user_config where type='config' order by id desc limit 1" > config.json NEW_CONFIG=$(node ./utils/save-config.js $NUMBER $DEVICE_ID) - sudo -u postgres psql "lamassu" << EOF + sudo -u postgres psql "lamassu_stress" << EOF insert into user_config(type, data, created, valid) values('config', '$NEW_CONFIG', now(), 't') EOF # Add device on db - sudo -u postgres psql "lamassu" << EOF + sudo -u postgres psql "lamassu_stress" << EOF insert into devices(device_id, cashbox, cassette1, cassette2, paired, display, created, name, last_online, location) values ('$DEVICE_ID', 0, 0, 0, 't', 't', now(), $NUMBER, now(), '{}'::json) EOF diff --git a/test/stress/scripts/index.js b/test/stress/scripts/index.js index 2612abc5..a77010af 100644 --- a/test/stress/scripts/index.js +++ b/test/stress/scripts/index.js @@ -1,4 +1,4 @@ -const exec = require('child_process') +const exec = require('child_process').exec /** * Execute simple shell command (async wrapper). @@ -7,15 +7,25 @@ const exec = require('child_process') */ async function execCommand (cmd) { return new Promise(function (resolve, reject) { - exec.exec(cmd, (err, stdout, stderr) => { + const proc = exec(cmd, (err, stdout, stderr) => { if (err) { reject(err) } else { - console.log(stdout) - console.error(stderr) resolve({ stdout, stderr }) } }) + + proc.stdout.on('data', data => { + console.log(data) + }) + + proc.stderr.on('data', data => { + console.log(data) + }) + + proc.on('exit', code => { + console.log('child process exited with code ' + code.toString()) + }) }) } diff --git a/test/stress/test-server.js b/test/stress/test-server.js new file mode 100644 index 00000000..56a7811e --- /dev/null +++ b/test/stress/test-server.js @@ -0,0 +1,7 @@ +const cmd = require('./scripts') + +process.on('message', async (msg) => { + console.log('Message from parent:', msg) + + await cmd.execCommand(`node --prof ../../bin/lamassu-server --mockSms --testDB`) +})