Skip to content

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

Add a script to observe the API during Redis migration (#3575)

Dhruv Bhanushalid93d935bacb6
diff --git a/utilities/load_testing/Dockerfile b/utilities/load_testing/Dockerfile
index fd95bd1a9..62a087087 100644
--- a/utilities/load_testing/Dockerfile
+++ b/utilities/load_testing/Dockerfile
@@ -1,14 +1,30 @@
# syntax=docker/dockerfile:1
-FROM docker.io/python:3.11-slim
+######
+# K6 #
+######
-# See https://github.com/grafana/k6#linux
-RUN apt-get update && \
- apt-get install -y wamerican apache2-utils gnupg2 && \
- apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69 && \
- echo "deb https://dl.k6.io/deb stable main" | tee /etc/apt/sources.list.d/k6.list && \
- apt-get update && \
- apt-get install k6 && \
- rm -rf /var/lib/apt/lists/*
+FROM docker.io/grafana/k6:latest as k6
+
+###############
+# Observation #
+###############
+
+FROM docker.io/python:3.11-slim AS lt
+
+LABEL org.opencontainers.image.source="https://github.com/WordPress/openverse"
+
+# Copy `k6` binary
+COPY --from=k6 /usr/bin/k6 /usr/bin/k6
+
+# Install system packages needed by the project
+# - wamerican: American words dictionary
+# - apache2-utils: Apache HTTP server benchmarking tool `ab`
+# - gnupg2: TODO why?
+RUN apt-get update \
+ && apt-get install -y wamerican apache2-utils gnupg2 \
+ && rm -rf /var/lib/apt/lists/*
WORKDIR /app
+
+ENTRYPOINT ["k6"]
diff --git a/utilities/load_testing/justfile b/utilities/load_testing/justfile
index a6b3f56ad..a4c48cf9d 100644
--- a/utilities/load_testing/justfile
+++ b/utilities/load_testing/justfile
@@ -1,9 +1,18 @@
set dotenv-load := true
+COLOR := "\\033[0;32m"
+NO_COLOR := "\\033[0m"
+
+# Show all available recipes
+@_default:
+ printf "\n{{ COLOR }}# Load testing (path: \`api/\`)\n"
+ printf "============================={{ NO_COLOR }}\n"
+ just --list --unsorted
+
DOCKER_HOST := if os() == "macos" { "host.docker.internal" } else { "172.17.0.1" }
build:
- docker build . -t openverse-load-testing:latest
+ docker build . -t openverse-load_testing:latest
run script host: build
docker run --rm -it -v {{ invocation_directory() }}:/app openverse-load-testing:latest ./{{ script }}.sh {{ host }}
@@ -11,5 +20,13 @@ run script host: build
api host=("http://" + DOCKER_HOST + ":8000"):
@just run api {{ host }}
-k6 url="https://api-dev.openverse.engineering" +extra_args="": build
- docker run --rm -e API_TOKEN=$API_TOKEN -e API_URL={{ url }} -v {{ invocation_directory() }}/k6:/app {{ extra_args }} openverse-load-testing:latest k6 run main.js
+# Run the specified script in a load testing container
+k6 script="main.js" +extra_args="": build
+ docker run \
+ --rm \
+ ${ACCESS_TOKEN:+ --env ACCESS_TOKEN=$ACCESS_TOKEN} \
+ ${API_URL:+ --env API_URL=$API_URL} \
+ --volume {{ invocation_directory() }}/k6:/app \
+ {{ extra_args }} \
+ openverse-load_testing:latest \
+ run {{ script }}
diff --git a/utilities/load_testing/k6/observation.js b/utilities/load_testing/k6/observation.js
new file mode 100644
index 000000000..97896d42f
--- /dev/null
+++ b/utilities/load_testing/k6/observation.js
@@ -0,0 +1,88 @@
+/**
+ * This module defines tests to run on the API to observe that everything is
+ * working normally while we migrate Redis. These are not load tests.
+ */
+
+import { sleep } from "k6"
+import http from "k6/http"
+import { Counter, Trend } from "k6/metrics"
+
+import { API_URL, REQUEST_HEADERS } from "./utils.js"
+
+const count200 = new Counter("count_200")
+const count424 = new Counter("count_424")
+const count429 = new Counter("count_429")
+const count4xx = new Counter("count_4xx")
+const count5xx = new Counter("count_5xx")
+const countNoneUsage = new Counter("count_none_usage")
+
+const trendBurst = new Trend("trend_usage")
+const trendSustained = new Trend("trend_sustained")
+
+const createScenario = (env, exec) => {
+ return {
+ executor: "constant-vus",
+ duration: __ENV.DURATION || "5m",
+ vus: 1,
+ env,
+ exec,
+ }
+}
+
+export const options = {
+ scenarios: {
+ imageStats: createScenario({ MEDIA_TYPE: "images" }, "stats"),
+ audioStats: createScenario({ MEDIA_TYPE: "audio" }, "stats"),
+ imageSearch: createScenario({ MEDIA_TYPE: "images" }, "search"),
+ audioSearch: createScenario({ MEDIA_TYPE: "audio" }, "search"),
+ authInfo: createScenario({}, "auth"),
+ },
+}
+
+const updateMetrics = (response) => {
+ if (response.status === 200) {
+ count200.add(1)
+ } else if (response.status === 424) {
+ count424.add(1)
+ } else if (response.status === 429) {
+ count429.add(1)
+ } else if (response.status >= 400 && response.status < 500) {
+ count4xx.add(1)
+ } else if (response.status >= 500 && response.status < 600) {
+ count5xx.add(1)
+ }
+}
+
+export const auth = () => {
+ let response = http.get(`${API_URL}rate_limit/`, { headers: REQUEST_HEADERS })
+ updateMetrics(response)
+ if (response.status === 200 || response.status === 424) {
+ const burst = JSON.parse(response.body).requests_this_minute
+ const sustained = JSON.parse(response.body).requests_today
+ if (burst === null || sustained === null) {
+ countNoneUsage.add(1)
+ } else {
+ trendBurst.add(burst)
+ trendSustained.add(sustained)
+ }
+ }
+ sleep(3) // seconds
+}
+
+export const stats = () => {
+ const mediaType = __ENV.MEDIA_TYPE
+ let response = http.get(`${API_URL}${mediaType}/stats`, {
+ headers: REQUEST_HEADERS,
+ })
+ updateMetrics(response)
+ sleep(3) // seconds
+}
+
+export const search = () => {
+ const mediaType = __ENV.MEDIA_TYPE
+ let response = http.get(`${API_URL}${mediaType}/?q=cat`, {
+ headers: REQUEST_HEADERS,
+ })
+ updateMetrics(response)
+ sleep(3) // seconds
+}
diff --git a/utilities/load_testing/k6/utils.js b/utilities/load_testing/k6/utils.js
index f3595a49e..872189492 100644
--- a/utilities/load_testing/k6/utils.js
+++ b/utilities/load_testing/k6/utils.js
@@ -20,7 +20,7 @@ export const getProvider = (media_type) => {
}
export const REQUEST_HEADERS = {
- Authorization: `Bearer ${__ENV.API_TOKEN}`,
+ Authorization: `Bearer ${__ENV.ACCESS_TOKEN}`,
"User-Agent": "k6",
}