Skip to content

Commit 77537d4

Browse files
authored
added locust facilities with more workers (#2314)
1 parent ccc86f7 commit 77537d4

File tree

6 files changed

+134
-3
lines changed

6 files changed

+134
-3
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ SWARM_HOSTS = $(shell docker node ls --format="{{.Hostname}}" 2>$(if $(IS_WIN),N
102102
define _docker_compose_build
103103
export BUILD_TARGET=$(if $(findstring -devel,$@),development,$(if $(findstring -cache,$@),cache,production));\
104104
$(if $(findstring -x,$@),\
105-
pushd services; docker buildx bake --file docker-compose-build.yml; popd;,\
105+
pushd services; docker buildx bake --file docker-compose-build.yml $(if $(target),$(target),); popd;,\
106106
docker-compose --file services/docker-compose-build.yml build $(if $(findstring -nc,$@),--no-cache,) $(if $(target),,--parallel)\
107107
)
108108
endef
@@ -126,7 +126,7 @@ ifeq ($(findstring webserver,$(target)),webserver)
126126
endif
127127
# Building service $(target)
128128
$(if $(findstring -kit,$@),export DOCKER_BUILDKIT=1;export COMPOSE_DOCKER_CLI_BUILD=1;,) \
129-
$(_docker_compose_build) $(target)
129+
$(_docker_compose_build)
130130
endif
131131

132132

tests/performance/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM locustio/locust:1.4.4
2+
3+
RUN pip3 --version && \
4+
pip3 install \
5+
faker \
6+
python-dotenv

tests/performance/Makefile

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
#
44
include ../../scripts/common.Makefile
55

6+
# Check that given variables are set and all have non-empty values,
7+
# die with an error otherwise.
8+
#
9+
# Params:
10+
# 1. Variable name(s) to test.
11+
# 2. (optional) Error message to print.
12+
check_defined = \
13+
$(strip $(foreach 1,$1, \
14+
$(call __check_defined,$1,$(strip $(value 2)))))
15+
__check_defined = \
16+
$(if $(value $1),, \
17+
$(error Undefined $1$(if $2, ($2))))
18+
619
.PHONY: requirements
720
requirements: ## compiles pip requirements (.in -> .txt)
821
@$(MAKE_C) requirements reqs
@@ -13,17 +26,30 @@ requirements: ## compiles pip requirements (.in -> .txt)
1326
@echo "WARNING ##### $< is newer than $@ ####"; diff -uN $@ $<; false;,\
1427
@echo "WARNING ##### $@ does not exist, cloning $< as $@ ############"; cp $< $@)
1528

29+
.PHONY: build
30+
build:
31+
docker buildx build --tag local/locust:latest .
32+
33+
.PHONY: up
34+
up:
35+
@$(call check_defined, target, please define target file when calling $@ - e.g. ```make $@ target=MY_LOCUST_FILE.py```)
36+
@export TARGET=$(target); \
37+
docker-compose --file docker-compose.yml up
38+
39+
down:
40+
docker-compose --file docker-compose.yml down
1641

1742
.PHONY: install
1843
install-dev: _check_venv_active ## installs dependencies
1944
# installing requirements
2045
pip-sync requirements/dev.txt
2146

47+
locusfile=$(if $(target),$(target),locusfile.py)
2248

2349
.PHONY: start
2450
start: _check_venv_active .env ## starts locust, a scriptable and scalable performance testing tool
2551
# Open http://localhost:8089/
26-
locust --locustfile locustfile.py --host http://127.0.0.1:9081 --users 3 --spawn-rate 1
52+
locust --locustfile $(locusfile) --host http://127.0.0.1:9081 --users 3 --spawn-rate 1
2753

2854

2955
.PHONY: list
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#
2+
# SEE https://docs.locust.io/en/stable/quickstart.html
3+
#
4+
5+
import logging
6+
import os
7+
8+
import faker
9+
from dotenv import load_dotenv
10+
from locust import task
11+
from locust.contrib.fasthttp import FastHttpUser
12+
13+
logging.basicConfig(level=logging.INFO)
14+
15+
fake = faker.Faker()
16+
17+
load_dotenv() # take environment variables from .env
18+
19+
20+
class WebApiUser(FastHttpUser):
21+
def __init__(self, *args, **kwargs):
22+
super().__init__(*args, **kwargs)
23+
24+
self.email = fake.email()
25+
26+
# @task
27+
# def health_check(self):
28+
# self.client.get("/v0/health")
29+
30+
@task(weight=5)
31+
def get_services(self):
32+
self.client.get(
33+
f"/v0/catalog/services",
34+
)
35+
36+
def on_start(self):
37+
print("Created User ", self.email)
38+
self.client.post(
39+
"/v0/auth/register",
40+
json={
41+
"email": self.email,
42+
"password": "my secret",
43+
"confirm": "my secret",
44+
},
45+
)
46+
self.client.post(
47+
"/v0/auth/login", json={"email": self.email, "password": "my secret"}
48+
)
49+
50+
def on_stop(self):
51+
self.client.post("/v0/auth/logout")
52+
print("Stopping", self.email)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#
2+
# SEE https://docs.locust.io/en/stable/quickstart.html
3+
#
4+
5+
import logging
6+
7+
from dotenv import load_dotenv
8+
from locust import task
9+
from locust.contrib.fasthttp import FastHttpUser
10+
11+
logging.basicConfig(level=logging.INFO)
12+
13+
14+
load_dotenv() # take environment variables from .env
15+
16+
17+
class WebApiUser(FastHttpUser):
18+
def __init__(self, *args, **kwargs):
19+
super().__init__(*args, **kwargs)
20+
21+
@task()
22+
def get_services(self):
23+
self.client.get(
24+
f"/v0/services",
25+
)
26+
27+
def on_start(self):
28+
print("Created User ")
29+
30+
def on_stop(self):
31+
print("Stopping")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: '3.8'
2+
3+
services:
4+
master:
5+
image: local/locust:latest
6+
ports:
7+
- "8089:8089"
8+
volumes:
9+
- ./:/mnt/locust
10+
command: -f /mnt/locust/${TARGET} --master -H http://master:8089
11+
12+
worker:
13+
image: local/locust:latest
14+
volumes:
15+
- ./:/mnt/locust
16+
command: -f /mnt/locust/${TARGET} --worker --master-host master

0 commit comments

Comments
 (0)