Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Add basic API load testing script (#257)

sarayourfriendb774a6fa8a74
diff --git a/.gitignore b/.gitignore
index 1fd8cc12f..dafa7684e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,3 +11,4 @@ env/
.vscode/
grafana.db
+__pycache__
diff --git a/load_testing/.env.sh.template b/load_testing/.env.sh.template
new file mode 100644
index 000000000..2dda0a781
--- /dev/null
+++ b/load_testing/.env.sh.template
@@ -0,0 +1,3 @@
+#! /usr/bin/env sh
+
+export ACCESS_TOKEN="<not_set>"
diff --git a/load_testing/.gitignore b/load_testing/.gitignore
new file mode 100644
index 000000000..3814618cd
--- /dev/null
+++ b/load_testing/.gitignore
@@ -0,0 +1,2 @@
+.env.sh
+output.html
diff --git a/load_testing/Dockerfile b/load_testing/Dockerfile
new file mode 100644
index 000000000..c5279a77b
--- /dev/null
+++ b/load_testing/Dockerfile
@@ -0,0 +1,5 @@
+FROM python:3.10-slim
+
+RUN apt-get update && apt-get install -y wamerican apache2-utils
+
+WORKDIR /app
diff --git a/load_testing/README.md b/load_testing/README.md
new file mode 100644
index 000000000..f8280fb00
--- /dev/null
+++ b/load_testing/README.md
@@ -0,0 +1,21 @@
+# Load testing
+
+This directory contains a collection of locust files for load testing the Openverse application. Currently, only an API load test exists but in the future we can also create load tests for the frontend or any other service we stand up. These exist in this repository rather than one of the service specific ones for this reason. We can re-use the base docker image we create for all our load testing scripts.
+
+**Note:** Production is throttled, so these scripts cannot be used as a shortcut to DDoS our production stack.
+
+## Running the tests
+
+### Setup
+
+#### API
+
+The API load tests assume that you will be using an API key as it is impossible to make unauthenticated requests large enough (`page_size=500`) to facilitate easy flooding. Likewise, unless throttling is temporarily disabled in the environment being tested, you'll probably want to exempt the API key from throttling by adding it to the [API Key throttle exemption](https://github.com/WordPress/openverse-api/blob/c09fd7e16a8eb104c311e8d4f0da08238570067c/api/catalog/api/utils/throttle.py#L77). Please ping `@WordPress/openverse-maintainers` for help doing this in live environments as it requires adding the key to a set in that environment's Redis cache.
+
+To make the API key accessible to the load testing script, copy the `.env.sh.template` file and name it `.env.sh` and fill in the `ACCESS_TOKEN` variable.
+
+### Running
+
+All load tests are accessible through `just` scripts in this directory's `justfile`.
+
+To run API load tests against a local API instance, use `just api`. You can optionally specify a host (the default is to point to local). For example `just api https://api-dev.openverse.engineering` will run the load tests against the staging API.
diff --git a/load_testing/api.sh b/load_testing/api.sh
new file mode 100755
index 000000000..65cc21f30
--- /dev/null
+++ b/load_testing/api.sh
@@ -0,0 +1,31 @@
+#! /usr/bin/env bash
+
+set -e
+
+source ./.env.sh || (echo 'Please create a `.env.sh` file based on the `.env.sh.template` file.' > /dev/stderr && false)
+
+host=$1
+
+auth_header="Authorization: Bearer $ACCESS_TOKEN"
+
+concurrency=4
+
+requests=100
+
+q=$(./get_word.sh)
+
+set -x
+
+ab \
+ -w \
+ -v 3 \
+ -c $concurrency \
+ -n $requests \
+ -T "application/json" \
+ -H "$auth_header" \
+ "$host/v1/images/?$q&page_size=500" > output.html
+
+set +x
+
+# fix root permissions
+chown 1000:1000 output.html
diff --git a/load_testing/get_word.sh b/load_testing/get_word.sh
new file mode 100755
index 000000000..3b7361927
--- /dev/null
+++ b/load_testing/get_word.sh
@@ -0,0 +1,3 @@
+#! /usr/bin/env sh
+
+python -c "from urllib import parse; print(parse.urlencode({'q': \"$(shuf -n1 /usr/share/dict/words)\"}), end='')"
diff --git a/load_testing/justfile b/load_testing/justfile
new file mode 100644
index 000000000..3d547f32a
--- /dev/null
+++ b/load_testing/justfile
@@ -0,0 +1,12 @@
+set dotenv-load := false
+
+DOCKER_HOST := if os() == "darwin" { "host.docker.internal" } else { "172.17.0.1" }
+
+build:
+ docker build . -t openverse-load-testing:latest
+
+run script host: build
+ docker run -it -v {{invocation_directory()}}:/app openverse-load-testing:latest ./{{ script }}.sh {{ host }}
+
+api host=("http://" + DOCKER_HOST + ":8000"):
+ @just run api {{ host }}