Merge lp:~mpontillo/maas/basic-speed-test-endpoint into lp:maas/trunk

Proposed by Mike Pontillo on 2016-12-07
Status: Rejected
Rejected by: MAAS Lander on 2017-06-22
Proposed branch: lp:~mpontillo/maas/basic-speed-test-endpoint
Merge into: lp:maas/trunk
Diff against target: 98 lines (+78/-0)
2 files modified
src/maasserver/api/speed_test.py (+76/-0)
src/maasserver/urls_api.py (+2/-0)
To merge this branch: bzr merge lp:~mpontillo/maas/basic-speed-test-endpoint
Reviewer Review Type Date Requested Status
MAAS Maintainers 2016-12-07 Pending
Review via email: mp+312692@code.launchpad.net

Commit message

Add basic speed-test API endpoint.

To post a comment you must log in.
MAAS Lander (maas-lander) wrote :

Transitioned to Git.

lp:maas has now moved from Bzr to Git.
Please propose your branches with Launchpad using Git.

git clone https://git.launchpad.net/maas

Unmerged revisions

5594. By Mike Pontillo on 2016-12-07

Add basic speed-test API endpoint.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'src/maasserver/api/speed_test.py'
2--- src/maasserver/api/speed_test.py 1970-01-01 00:00:00 +0000
3+++ src/maasserver/api/speed_test.py 2016-12-07 16:32:54 +0000
4@@ -0,0 +1,76 @@
5+# Copyright 2016 Canonical Ltd. This software is licensed under the
6+# GNU Affero General Public License version 3 (see the file LICENSE).
7+
8+"""Speed test API handler."""
9+
10+from django.http import HttpResponse
11+from django.http.response import HttpResponseBadRequest
12+from formencode.validators import (
13+ Number,
14+ StringBool,
15+)
16+from maasserver.api.utils import get_optional_param
17+
18+
19+def get_random_testdata(size):
20+ with open('/dev/urandom', 'rb') as dev_random:
21+ return dev_random.read(size)
22+
23+
24+def get_zero_testdata(size):
25+ with open('/dev/zero', 'rb') as dev_zero:
26+ return dev_zero.read(size)
27+
28+
29+# Cache one megabyte of random test data.
30+DEFAULT_SIZE = (1024 * 1024)
31+# Don't allow the user to ask for more than 64 megabytes.
32+MAX_SIZE = (1024 * 1024 * 64)
33+RANDOM_TESTDATA = dict()
34+RANDOM_TESTDATA[DEFAULT_SIZE] = get_random_testdata(DEFAULT_SIZE)
35+
36+
37+def speed_test(request):
38+ """API endpoint that can be used for simple speed tests.
39+
40+ No authentication is required in order to use this end point.
41+
42+ Optional parameters
43+ -------------------
44+ random
45+ If False, uses zeroes as a test payload, rather than the default
46+ of using random test data. (Default: True)
47+
48+ size
49+ Specifies the number of bytes to use as a test payload.
50+ Default is 1048576 (one megabyte).
51+
52+ pattern
53+ If specified, uses the specified string for a test payload instead of
54+ random (or zero) data.
55+ """
56+ size = int(get_optional_param(
57+ request.GET, 'size', default=DEFAULT_SIZE, validator=Number))
58+ random = get_optional_param(
59+ request.GET, 'random', default=True, validator=StringBool)
60+ pattern = get_optional_param(
61+ request.GET, 'pattern', default=None)
62+ # If the user specified a pattern, that imples they don't want to use
63+ # random data for the test payload.
64+ if pattern is not None:
65+ random = False
66+ if size > MAX_SIZE:
67+ return HttpResponseBadRequest(
68+ content_type="text/plain",
69+ content="Invalid size; maximum is %d bytes." % MAX_SIZE)
70+ if random:
71+ if size in RANDOM_TESTDATA:
72+ content = RANDOM_TESTDATA[size]
73+ else:
74+ content = get_random_testdata(size)
75+ elif pattern:
76+ content = pattern * int(size / len(pattern))
77+ else:
78+ content = get_zero_testdata(size)
79+ return HttpResponse(
80+ content_type="application/octet-stream", content=content)
81
82=== modified file 'src/maasserver/urls_api.py'
83--- src/maasserver/urls_api.py 2016-12-07 12:46:14 +0000
84+++ src/maasserver/urls_api.py 2016-12-07 16:32:54 +0000
85@@ -137,6 +137,7 @@
86 SpaceHandler,
87 SpacesHandler,
88 )
89+from maasserver.api.speed_test import speed_test
90 from maasserver.api.ssh_keys import (
91 SSHKeyHandler,
92 SSHKeysHandler,
93@@ -325,6 +326,7 @@
94 url(r'doc/$', api_doc, name='api-doc'),
95 url(r'describe/$', describe, name='describe'),
96 url(r'version/$', version_handler, name='version_handler'),
97+ url(r'speed-test/$', speed_test, name='speed_test_handler'),
98 )
99
100