Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tracemalloc utility view to API and improve k6 scripts for memory leak debugging #3046

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions api/conf/urls/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.contrib import admin
from django.urls import include, path
from django.views.generic import RedirectView
from rest_framework.decorators import api_view
from rest_framework.routers import SimpleRouter

from api.views.audio_views import AudioViewSet
Expand All @@ -17,6 +18,34 @@
from conf.urls.auth_tokens import urlpatterns as auth_tokens_urlpatterns
from conf.urls.deprecations import urlpatterns as deprecations_urlpatterns
from conf.urls.openapi import urlpatterns as openapi_urlpatterns
from conf.wsgi import TRACING


if TRACING:
prev_snapshot = None

@api_view(["GET"])
def _trace_view(request):
global prev_snapshot
import pprint
import time
import tracemalloc

from django.http.response import HttpResponse

snapshot = tracemalloc.take_snapshot()
if prev_snapshot:
top_stats = snapshot.compare_to(prev_snapshot, "lineno", cumulative=True)[
:10
]
else:
top_stats = snapshot.statistics("lineno", cumulative=True)[:10]

prev_snapshot = snapshot

content = f"{time.time()}\n" f"{pprint.pformat(top_stats, indent=4)}"

return HttpResponse(content=content, status=200, content_type="text/plain")


versioned_paths = [
Expand All @@ -37,5 +66,8 @@
path("v1/", include(versioned_paths)),
]

if TRACING:
urlpatterns.append(path("_trace/", _trace_view))

if settings.ENVIRONMENT == "local":
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
9 changes: 9 additions & 0 deletions api/conf/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@

import os


if os.getenv("ENABLE_TRACE_VIEW", "0") == "1":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using decouple.config would allow direct cast to bool.

from decouple import config

TRACING = config('ENABLE_TRACE_VIEW', default=False, cast=bool)

TRACING = True
import tracemalloc

tracemalloc.start()
else:
TRACING = False

from django.core.wsgi import get_wsgi_application


Expand Down
1 change: 1 addition & 0 deletions api/env.template
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ IS_PROXIED=False

FILTER_DEAD_LINKS_BY_DEFAULT=False
ENABLE_FILTERED_INDEX_QUERIES=True
ENABLE_TRACE_VIEW=0
3 changes: 3 additions & 0 deletions utilities/load_testing/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ api host=("http://" + DOCKER_HOST + ":8000"):

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

k6-local:
@API_TOKEN="" just k6 http://localhost:50280/v1/ --net=host
Comment on lines +17 to +18
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is excellent, thanks for adding it!

5 changes: 2 additions & 3 deletions utilities/load_testing/k6/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { group } from "k6"
import { searchBy } from "./search.js"
import { getProvider, getRandomWord } from "./utils.js"
import { getProvider } from "./utils.js"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 40-41:

    provider_image_page_20: createScenario("images", "20", "searchByProvider"),
    provider_image_page_500: createScenario("images", "500", "searchByProvider"),


const createScenario = (mediaType, pageSize, funcName) => {
return {
Expand Down Expand Up @@ -64,7 +64,6 @@ const searchByField = (paramFunc, followLinks = false) => {
)
}

export const searchByRandomWord = () =>
searchByField(() => `q=${getRandomWord()}`, true)
export const searchByRandomWord = () => searchByField(() => `randomWord`, true)
export const searchByProvider = () =>
searchByField((media_type) => `source=${getProvider(media_type)}`, false)
11 changes: 10 additions & 1 deletion utilities/load_testing/k6/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ import {
makeResponseFailedCheck,
REQUEST_HEADERS,
SLEEP_DURATION,
discoveredUnconsumedTags,
getRandomWord,
} from "./utils.js"

export const searchBy = (param, page, media_type, page_size, followLinks) => {
let url = `${API_URL}${media_type}/?${param}&page=${page}&page_size=${page_size}&filter_dead=false`
if (param === "randomWord") {
param = `q=${getRandomWord()}`
}

let url = `${API_URL}${media_type}/?${param}&page=${page}&page_size=${page_size}&filter_dead=true`
const response = http.get(url, { headers: REQUEST_HEADERS })

const checkResponseFailed = makeResponseFailedCheck(param, page)
Expand All @@ -23,6 +29,9 @@ export const searchBy = (param, page, media_type, page_size, followLinks) => {
const pageCount = parsedResp["page_count"]
const detailUrls = parsedResp["results"].map((i) => i.detail_url)
const relatedUrls = parsedResp["results"].map((i) => i.related_url)
parsedResp["results"].forEach((i) =>
i["tags"].forEach((t) => discoveredUnconsumedTags.add(t["name"]))
)

// Don't view details/related if not requested
if (!followLinks) {
Expand Down
15 changes: 10 additions & 5 deletions utilities/load_testing/k6/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ import { randomItem } from "https://jslib.k6.io/k6-utils/1.2.0/index.js"
export const API_URL =
__ENV.API_URL || "https://api-dev.openverse.engineering/v1/"
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")
.split("\n")
.filter((w) => !w.endsWith("'s"))

export const getRandomWord = () => randomItem(WORDS)
export const discoveredUnconsumedTags = new Set()

export const getRandomWord = () => {
if (discoveredUnconsumedTags.size) {
const tag = randomItem(Array.from(discoveredUnconsumedTags))
discoveredUnconsumedTags.delete(tag)
return tag
}
return randomItem(["dog", "run", "cat", "honey", "happy"])
}

export const getProvider = (media_type) => {
let url = `${API_URL}${media_type}/stats`
Expand Down