Skip to content

Commit

Permalink
Loadtest the usernames API.
Browse files Browse the repository at this point in the history
Uses a Waffle Sample to make a tunable percentage of requests (start with 1%)
repeatedly hit the username API.
  • Loading branch information
James Socol committed Apr 22, 2011
1 parent 5bf66d4 commit a6927e1
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 8 deletions.
6 changes: 4 additions & 2 deletions apps/users/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ def usernames(request):
if not pre:
return HttpResponse(json.dumps([]), mimetype=mimetype)

q = Q(username__istartswith=pre) | Q(profile__name__istartswith=pre)

# Eventually, when display name becomes more prominent, we'll want to
# include that. Don't just OR this with Q(profile__name__startswith=pre).
# That way lies horrid performance.
q = Q(username__istartswith=pre)
users = User.objects.filter(q).values_list('username', flat=True)[0:5]
# json.dumps won't serialize a QuerySet, so list comp.
return HttpResponse(json.dumps([u for u in users]), mimetype=mimetype)
38 changes: 38 additions & 0 deletions media/js/loadtest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* JavaScript load tests.
*
* Tests _must_ be behind some tunable flag or switch.
*/
(function() {
/**
* Hit the usernames autocomplete API with a random request
* every few seconds. This is tuned with the
* 'usernames-test' sample in Waffle.
*/
function loadTestUsernamesAPI() {
if (waffle.sample("usernames-test")) {
var chars = "abcdefghijklmnopqrstuvwxyz",
ival, i = 0, MAX = 20;
ival = setInterval(function() {
if (i >= MAX) {
clearInterval(ival);
return;
}
i++;
var a = Math.floor(Math.random() * chars.length),
b = Math.floor(Math.random() * chars.length),
prefix = chars.substring(a, a+1) + chars.substring(b, b+1);
$.ajax(
$("body").data("usernames-api"),
{
cache: false,
data: {"u": prefix},
error: function() {
clearInterval(ival);
}
});
}, 20000);
}
}
$(document).ready(loadTestUsernamesAPI);
})();
5 changes: 1 addition & 4 deletions media/js/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,5 @@
});
}

$(document).ready(function () {
makeEmailsClickable();
});

$(document).ready(makeEmailsClickable);
}());
3 changes: 2 additions & 1 deletion settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@

# Paths that don't require a locale prefix.
SUPPORTED_NONLOCALES = ('media', 'admin', 'robots.txt', 'services', '1',
'postcrash')
'postcrash', 'wafflejs')

# Make this unique, and don't share it with anybody.
SECRET_KEY = '#%tc(zja8j01!r#h_y)=hy!^k)9az74k+-ib&ij&+**s3-e^_z'
Expand Down Expand Up @@ -376,6 +376,7 @@ def JINJA_CONFIG():
'global/menu.js',
'js/main.js',
'js/format.js',
'js/loadtest.js',
),
'libs/jqueryui': (
'js/libs/jqueryui.min.js',
Expand Down
4 changes: 3 additions & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
<body class="moz-header-slim {{ classes }}"
data-readonly="{{ settings.READ_ONLY|json }}"
data-for-os="{{ for_os|json }}"
data-for-version="{{ for_version|json }}">
data-for-version="{{ for_version|json }}"
data-usernames-api="{{ url('users.api.usernames') }}">

{% include 'layout/header.html' %}

Expand Down Expand Up @@ -131,6 +132,7 @@ <h1>{{ _('Check your <mark>plugins</mark>')|safe }}</h1>
</footer>{# /#footer #}

<script src="{{ url('jsi18n') }}build:{{ BUILD_ID_JS }}"></script>
<script src="{{ url('wafflejs') }}"></script>
{{ js('common') }}
{% for script in scripts %}
{{ js(script) }}
Expand Down
3 changes: 3 additions & 0 deletions urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from adminplus import AdminSitePlus
import authority
from waffle.views import wafflejs


admin.site = AdminSitePlus()
Expand All @@ -31,6 +32,8 @@
# Javascript translations.
url(r'^jsi18n/.*$', cache_page(60 * 60 * 24 * 365)(javascript_catalog),
{'domain': 'javascript', 'packages': ['kitsune']}, name='jsi18n'),
# JavaScript Waffle.
url(r'^wafflejs$', wafflejs, name='wafflejs'),

# Deprecated URLs.
(r'^forum', include('forums.old_urls')),
Expand Down

0 comments on commit a6927e1

Please sign in to comment.