Skip to content

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

Add load testing script for frontend (#4392)

Olga Bulatffad366a4965
diff --git a/utilities/load_testing/README.md b/utilities/load_testing/README.md
index bea8d3e7c..26103d8dd 100644
--- a/utilities/load_testing/README.md
+++ b/utilities/load_testing/README.md
@@ -30,6 +30,16 @@ 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.
+#### Frontend
+
+By default, the frontend load tests run against `https://openverse.org`. It is
+important to disable the Cloudflare cache and WAF security settings for the
+duration of the test to test the actual performance of the application instead
+of testing Cloudflare cache or getting blocked by WAF. By default, the tests
+request only the English static pages. You can specify other scenarios to run,
+with `just k6 all` being the most comprehensive test that requests static and
+search pages in English. The `locales` tests request pages in 3 other locales.
+
### Running
All load tests are accessible through `just` scripts in this directory's
@@ -39,3 +49,10 @@ 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-staging.openverse.org` will run the load tests against the
staging API.
+
+To run the frontend load tests, use `just k6-frontend`. You can optionally
+specify the scenarios to run, for example, `just k6-frontend all`. To specify a
+host (the default is to point to the `openverse.org`), set the `FRONTEND_URL`
+environment variable. For example,
+`FRONTEND_URL=https://staging.openverse.org just k6-frontend` will run the load
+tests against the staging frontend.
diff --git a/utilities/load_testing/justfile b/utilities/load_testing/justfile
index e09d38393..d2fcad217 100644
--- a/utilities/load_testing/justfile
+++ b/utilities/load_testing/justfile
@@ -26,7 +26,13 @@ k6 script="main.js" +extra_args="": build
--rm \
${ACCESS_TOKEN:+ --env ACCESS_TOKEN=$ACCESS_TOKEN} \
${API_URL:+ --env API_URL=$API_URL} \
+ ${FRONTEND_URL:+ --env FRONTEND_URL=$FRONTEND_URL} \
--volume {{ invocation_directory() }}/k6:/app \
{{ extra_args }} \
openverse-load_testing:latest \
run {{ script }}
+
+
+# Run the frontend script for one of the scenarios (static-en, static-locales, search-en, search-locales, all)
+k6-frontend scenarios="static-en":
+ @just k6 frontend.js --env SCENARIOS={{scenarios}}
diff --git a/utilities/load_testing/k6/frontend.js b/utilities/load_testing/k6/frontend.js
new file mode 100644
index 000000000..de1ab034d
--- /dev/null
+++ b/utilities/load_testing/k6/frontend.js
@@ -0,0 +1,146 @@
+import { group } from "k6"
+import exec from "k6/execution"
+import http from "k6/http"
+import {
+ FRONTEND_URL,
+ getRandomWord,
+ makeResponseFailedCheck,
+} from "./utils.js"
+
+const STATIC_PAGES = [
+ "about",
+ "sources",
+ "search-help",
+ "privacy",
+ "sensitive-content",
+]
+const TEST_LOCALES = ["en", "ru", "es", "ar"]
+const TEST_PARAMS = "&license=by&extension=jpg,mp3&source=flickr,jamendo"
+
+const localePrefix = (locale) => {
+ return locale === "en" ? "" : locale + "/"
+}
+
+const visitUrl = (url, locale, action) => {
+ const response = http.get(url, { headers: { "cache-control": "no-cache" } })
+ const checkResponseFailed = makeResponseFailedCheck("", url)
+ if (checkResponseFailed(response, action)) {
+ console.error(`Failed URL: ${url}`)
+ return 0
+ }
+}
+
+const parseEnvLocales = (locales) => {
+ return locales ? locales.split(",") : ["en"]
+}
+
+export function visitStaticPages() {
+ const locales = parseEnvLocales(__ENV.LOCALES)
+ console.log(
+ `VU: ${exec.vu.idInTest} - ITER: ${exec.vu.iterationInInstance}`
+ )
+ for (let locale of locales) {
+ group(`visit static pages for locale ${locale}`, () => {
+ for (let page of STATIC_PAGES) {
+ visitUrl(
+ `${FRONTEND_URL}${localePrefix(locale)}${page}`,
+ locale,
+ "visitPage"
+ )
+ }
+ })
+ }
+}
+
+export function visitSearchPages() {
+ let locales = parseEnvLocales(__ENV.LOCALES)
+ let params = __ENV.PARAMS
+ const paramsString = params ? ` with params ${params}` : ""
+ console.log(
+ `VU: ${exec.vu.idInTest} - ITER: ${exec.vu.iterationInInstance}`
+ )
+ for (let MEDIA_TYPE of ["image", "audio"]) {
+ for (let locale of locales) {
+ let q = getRandomWord()
+ group(
+ `${MEDIA_TYPE} search for ${q} on locale ${locale}${paramsString}`,
+ () => {
+ return visitUrl(
+ `${FRONTEND_URL}${localePrefix(locale)}search/${MEDIA_TYPE}?q=${q}${params}`,
+ locale,
+ "visitSearchPage"
+ )
+ }
+ )
+ }
+ }
+}
+
+const createScenario = (env, funcName) => {
+ return {
+ executor: "per-vu-iterations",
+ env,
+ exec: funcName,
+ vus: 10,
+ iterations: 10,
+ }
+}
+
+const optionToScenario = {
+ all: [
+ "staticPages",
+ "localeStaticPages",
+ "englishSearchPages",
+ "localesSearchPages",
+ "englishSearchPagesWithFilters",
+ "localesSearchPagesWithFilters",
+ ],
+ "static-en": ["staticPages"],
+ "static-locales": ["localeStaticPages"],
+ "search-en": ["englishSearchPages", "englishSearchPagesWithFilters"],
+ "search-locales": ["localesSearchPages", "localesSearchPagesWithFilters"],
+}
+const getScenariosToRun = () => {
+ let scenariosToRun = __ENV.SCENARIOS
+
+ if (
+ !scenariosToRun ||
+ !Object.keys(optionToScenario).includes(scenariosToRun)
+ ) {
+ scenariosToRun = "static-en"
+ }
+ const allScenarios = {
+ staticPages: createScenario({ LOCALES: "en" }, "visitStaticPages"),
+ localeStaticPages: createScenario(
+ { LOCALES: TEST_LOCALES.join(",") },
+ "visitStaticPages"
+ ),
+ englishSearchPages: createScenario(
+ { LOCALES: "en", PARAMS: "" },
+ "visitSearchPages"
+ ),
+ localesSearchPages: createScenario(
+ { LOCALES: TEST_LOCALES.join(","), PARAMS: "" },
+ "visitSearchPages"
+ ),
+ englishSearchPagesWithFilters: createScenario(
+ { LOCALES: "en", PARAMS: TEST_PARAMS },
+ "visitSearchPages"
+ ),
+ localesSearchPagesWithFilters: createScenario(
+ { LOCALES: TEST_LOCALES.join(","), PARAMS: TEST_PARAMS },
+ "visitSearchPages"
+ ),
+ }
+
+ return Object.keys(allScenarios).reduce((acc, key) => {
+ if (optionToScenario[scenariosToRun].includes(key)) {
+ acc[key] = allScenarios[key]
+ }
+ return acc
+ }, {})
+}
+
+export const options = {
+ scenarios: getScenariosToRun(),
+}
diff --git a/utilities/load_testing/k6/utils.js b/utilities/load_testing/k6/utils.js
index a93240327..966706a50 100644
--- a/utilities/load_testing/k6/utils.js
+++ b/utilities/load_testing/k6/utils.js
@@ -3,6 +3,8 @@ import http from "k6/http"
import { randomItem } from "https://jslib.k6.io/k6-utils/1.2.0/index.js"
export const API_URL = __ENV.API_URL || "https://api-staging.openverse.org/v1/"
+export const FRONTEND_URL = __ENV.FRONTEND_URL || "https://openverse.org/"
+
export const SLEEP_DURATION = 0.1
// Use the random words list available locally, but filter any words that end with apostrophe-s
const WORDS = open("/usr/share/dict/words")
@@ -32,14 +34,13 @@ export const getUrlBatch = (urls, type = "detail_url") => {
export const makeResponseFailedCheck = (param, page) => {
return (response, action) => {
+ const requestDetail = `${param ? `for param "${param} "` : ""}at page ${page} for ${action}`
if (check(response, { "status was 200": (r) => r.status === 200 })) {
- console.log(
- `Checked status 200 ✓ for param "${param}" at page ${page} for ${action}.`
- )
+ console.log(`Checked status 200 ✓ ${requestDetail}.`)
return false
} else {
console.error(
- `Request failed ⨯ for param "${param}" at page ${page} for ${action}: ${response.body}`
+ `Request failed ⨯ ${requestDetail}: ${response.status}\n${response.body}`
)
return true
}