From 1773ce888b24de49354430f9eebbd1baf063893e Mon Sep 17 00:00:00 2001 From: Reggie Cushing Date: Mon, 12 Aug 2024 10:48:30 +0200 Subject: [PATCH] parallelizing script on multiple cores. --- server_test/start.sh | 50 +++++++++++++++++++++++++++++++++++ server_test/test_user_flow.js | 38 ++++++++++++++++---------- 2 files changed, 74 insertions(+), 14 deletions(-) create mode 100755 server_test/start.sh diff --git a/server_test/start.sh b/server_test/start.sh new file mode 100755 index 0000000..c4932fc --- /dev/null +++ b/server_test/start.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +# Check if parameters are provided +if [ "$#" -ne 4 ] || ! [[ "$1" =~ ^[0-9]+$ ]]; then + echo "Usage: $0 " + exit 1 +fi + +# Determine the number of CPU cores based on the operating system +if [[ "$OSTYPE" == "linux-gnu"* ]]; then + num_cores=$(nproc) +elif [[ "$OSTYPE" == "darwin"* ]]; then + num_cores=$(sysctl -n hw.ncpu) +else + echo "Unsupported OS: $OSTYPE" + exit 1 +fi + +# Parameters +max_users=$1 +hostname=$2 +port=$3 +experimentId=$4 + +# Calculate base number of users per process +users_per_core=$((max_users / num_cores)) +remainder=$((max_users % num_cores)) + +# Function to handle users using a Node.js script +handle_users() { + local process_id=$1 + local start_user_id=$2 + local users=$3 + node test_user_flow.js -s $start_user_id -n $users -hn $hostname -p $port -e $experimentId +} + +# Launch processes, distributing the users +start_user_id=1 +for i in $(seq 1 $num_cores); do + if [ $i -le $remainder ]; then + users_for_this_core=$((users_per_core + 1)) + else + users_for_this_core=$users_per_core + fi + handle_users $i $start_user_id $users_for_this_core & + start_user_id=$((start_user_id + users_for_this_core)) +done + +wait + diff --git a/server_test/test_user_flow.js b/server_test/test_user_flow.js index 82118a2..2720e80 100644 --- a/server_test/test_user_flow.js +++ b/server_test/test_user_flow.js @@ -1,20 +1,30 @@ const VirtualUser = require("./virtual-user-ws") -const fs = require("fs") +const { program } = require("commander") const http = require("http") -function randomBetween(min, max) { - return Math.floor(Math.random() * (max - min) + min) -} +program + .option("-p, --port ", "waiting-room server listen port.") + .option("-hn, --host ", "waiting-room server host.") + .option("-s, --start ", "start userId range.") + .option("-n, --num ", "number of participants.") + .option("-e, --experimentId ", "experiment name.") + +program.parse(process.argv) +const options = program.opts() -const maxUsers = parseInt(process.argv[2]) ? parseInt(process.argv[2]) : 100 -const experimentId = "DropOutTest" -const url = "http://localhost:8060" +const host = options.host || "localhost" +const port = options.port || 8060 +const start = parseInt(options.start) || 0 +const maxUsers = parseInt(options.num) || 99 +const experimentId = options.experimentId || "DropOutTest" +const url = `http://${host}:${port}` const virtUsers = {} +console.log(`Trying server url: ${url} from id ${start} to ${start + maxUsers} for experiment ${experimentId}.`) + function runTest() { - for (let i = 0; i < maxUsers; i++) { - const id = 1000 + i - //const id = randomBetween(1, 9999999) + for (let i = start; i < (start + maxUsers); i++) { + const id = i vu = new VirtualUser(id, experimentId, url) virtUsers[id] = vu } @@ -28,14 +38,14 @@ function runTest() { }) } -const options = { - hostname: "localhost", - port: 8060, +const Urloptions = { + hostname: host, + port: port, path: "/", method: "GET", } -const req = http.request(options, (res) => { +const req = http.request(Urloptions, (res) => { if (res.statusCode > 0) { console.log("Server is up and running.") runTest()