Skip to content

Commit

Permalink
Some scripts I use to manually test locust locally
Browse files Browse the repository at this point in the history
  • Loading branch information
mboutet committed Jul 9, 2021
1 parent b9c5dd4 commit f9adde1
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 0 deletions.
81 changes: 81 additions & 0 deletions scripts/locustfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import math

from locust import HttpUser, task, constant
from locust import LoadTestShape


class UserA(HttpUser):
wait_time = constant(600)

# host = "https://example.com"

@task
def get_root(self):
self.client.get("/", name="UserA")


class UserB(HttpUser):
wait_time = constant(600)

# host = "https://example.com"

@task
def get_root(self):
self.client.get("/", name="UserB")


class UserC(HttpUser):
wait_time = constant(600)

# host = "https://example.com"

@task
def get_root(self):
self.client.get("/", name="UserC")


# class StepLoadShape(LoadTestShape):
# step_time = 30
# step_load = 10
# spawn_rate = 1
# time_limit = 300
#
# def tick(self):
# run_time = self.get_run_time()
#
# if run_time > self.time_limit:
# return None
#
# current_step = math.floor(run_time / self.step_time) + 1
# return current_step * self.step_load, self.spawn_rate


class RampUpThenDownLoadShape(LoadTestShape):
stages = [
{"duration": 5, "users": 1, "spawn_rate": 1},
{"duration": 35, "users": 30, "spawn_rate": 1},
{"duration": 35, "users": 1, "spawn_rate": 1},
{"duration": 35, "users": 73, "spawn_rate": 6},
{"duration": 35, "users": 1, "spawn_rate": 6},
{"duration": 35, "users": 153, "spawn_rate": 17},
{"duration": 10, "users": 145, "spawn_rate": 1},
{"duration": 60, "users": 130, "spawn_rate": 0.25},
{"duration": 15, "users": 50, "spawn_rate": 25},
{"duration": 20, "users": 1, "spawn_rate": 5},
]

for previous_stage, stage in zip(stages[:-1], stages[1:]):
stage["duration"] += previous_stage["duration"]

for previous_stage, stage in zip(stages[:-1], stages[1:]):
assert stage["duration"] > previous_stage["duration"]

def tick(self):
run_time = self.get_run_time()

for stage in self.stages:
if run_time < stage["duration"]:
tick_data = (stage["users"], stage["spawn_rate"])
return tick_data

return None
58 changes: 58 additions & 0 deletions scripts/run-disributed-headless.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env bash

set -o errexit
set -o pipefail
set -o nounset

script_dir="$(realpath "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )")"
root_dir="$(realpath "${script_dir}/..")"

pushd "${root_dir}"
"${VIRTUAL_ENV}/bin/pip" install --editable .
popd

pushd "${script_dir}"

pids=()

function cleanup(){
for pid in ${pids}
do
echo "Killing worker with pid ${pid}"
kill -9 "${pid}" || echo "Failed killing worker with pid ${pid}"
done
}

trap cleanup EXIT

export LOCUST_WORKER_ADDITIONAL_WAIT_BEFORE_READY_AFTER_STOP=5

n_workers=4

for n in $(seq 1 ${n_workers})
do
tmp_file="$(mktemp /tmp/locust-worker-${n}.log.XXXXXX)"
echo "Starting worker ${n} (logs in ${tmp_file})"
"${VIRTUAL_ENV}/bin/python" -m locust \
--locustfile "${script_dir}/locustfile.py" \
--host "https://example.com" \
--worker \
--master-host "0.0.0.0" \
--master-port "5557" \
--logfile "${tmp_file}" \
--loglevel "DEBUG" &
pids+=($!)
done

echo "Starting master"
"${VIRTUAL_ENV}/bin/python" -m locust \
--locustfile "${script_dir}/locustfile.py" \
--host "https://example.com" \
--headless \
--master \
--master-bind-host "0.0.0.0" \
--master-bind-port "5557" \
--expect-workers ${n_workers} \
--loglevel "DEBUG"

popd
57 changes: 57 additions & 0 deletions scripts/run-disributed-web.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env bash

set -o errexit
set -o pipefail
set -o nounset

script_dir="$(realpath "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )")"
root_dir="$(realpath "${script_dir}/..")"

pushd "${root_dir}"
"${VIRTUAL_ENV}/bin/pip" install --editable .
popd

pushd "${script_dir}"

pids=()

function cleanup(){
for pid in ${pids}
do
echo "Killing worker with pid ${pid}"
kill -9 "${pid}" || echo "Failed killing worker with pid ${pid}"
done
}

trap cleanup EXIT

export LOCUST_WORKER_ADDITIONAL_WAIT_BEFORE_READY_AFTER_STOP=5

n_workers=13

for n in $(seq 1 ${n_workers})
do
tmp_file="$(mktemp /tmp/locust-worker-${n}.log.XXXXXX)"
echo "Starting worker ${n} (logs in ${tmp_file})"
"${VIRTUAL_ENV}/bin/python" -m locust \
--locustfile "${script_dir}/locustfile.py" \
--host "https://example.com" \
--worker \
--master-host "0.0.0.0" \
--master-port "5557" \
--logfile "${tmp_file}" \
--loglevel "DEBUG" &
pids+=($!)
done

echo "Starting master"
"${VIRTUAL_ENV}/bin/python" -m locust \
--locustfile "${script_dir}/locustfile.py" \
--host "https://example.com" \
--master \
--master-bind-host "0.0.0.0" \
--master-bind-port "5557" \
--expect-workers ${n_workers} \
--loglevel "DEBUG"

popd
20 changes: 20 additions & 0 deletions scripts/run-local-headless.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

set -o errexit
set -o pipefail
set -o nounset

script_dir="$(realpath "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )")"
root_dir="$(realpath "${script_dir}/..")"

pushd "${root_dir}"
"${VIRTUAL_ENV}/bin/pip" install --editable .
popd

pushd "${script_dir}"
"${VIRTUAL_ENV}/bin/python" -m locust \
--locustfile "${script_dir}/locustfile.py" \
--headless \
--host "https://example.com" \
--loglevel "INFO"
popd
19 changes: 19 additions & 0 deletions scripts/run-local-web.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

set -o errexit
set -o pipefail
set -o nounset

script_dir="$(realpath "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )")"
root_dir="$(realpath "${script_dir}/..")"

pushd "${root_dir}"
"${VIRTUAL_ENV}/bin/pip" install --editable .
popd

pushd "${script_dir}"
"${VIRTUAL_ENV}/bin/python" -m locust \
--locustfile "${script_dir}/locustfile.py" \
--host "https://example.com" \
--loglevel "DEBUG"
popd

0 comments on commit f9adde1

Please sign in to comment.