Skip to content

Commit

Permalink
parallelizing script on multiple cores.
Browse files Browse the repository at this point in the history
  • Loading branch information
recap committed Aug 12, 2024
1 parent dbca058 commit 1773ce8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 14 deletions.
50 changes: 50 additions & 0 deletions server_test/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash

# Check if parameters are provided
if [ "$#" -ne 4 ] || ! [[ "$1" =~ ^[0-9]+$ ]]; then
echo "Usage: $0 <max_users> <hostname> <port> <experimentId>"
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

38 changes: 24 additions & 14 deletions server_test/test_user_flow.js
Original file line number Diff line number Diff line change
@@ -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 <int>", "waiting-room server listen port.")
.option("-hn, --host <type>", "waiting-room server host.")
.option("-s, --start <int>", "start userId range.")
.option("-n, --num <int>", "number of participants.")
.option("-e, --experimentId <type>", "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
}
Expand All @@ -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()
Expand Down

0 comments on commit 1773ce8

Please sign in to comment.