diff options
author | drebs <drebs@riseup.net> | 2017-10-20 17:18:18 -0200 |
---|---|---|
committer | drebs <drebs@riseup.net> | 2017-11-02 09:36:31 -0200 |
commit | e3879ac206e66437cefbe968e77f757239640681 (patch) | |
tree | e48bbde2774e58d2b74bc992d645f8e170dfe0a5 | |
parent | bba22c8439b56df4a74583f2582a89045ee43182 (diff) |
[benchmarks] add tokens setup to server scalability test controller
-rw-r--r-- | scripts/scalability/makefile | 3 | ||||
-rw-r--r-- | scripts/scalability/test_controller/server/server.tac | 6 | ||||
-rwxr-xr-x | scripts/scalability/test_controller/server/user_dbs.py | 57 |
3 files changed, 57 insertions, 9 deletions
diff --git a/scripts/scalability/makefile b/scripts/scalability/makefile index e99717fc..0a248d37 100644 --- a/scripts/scalability/makefile +++ b/scripts/scalability/makefile @@ -3,6 +3,7 @@ LOGFILE = /tmp/test_controller.log TACFILE = ./test_controller/server/server.tac HTTP_PORT = 7001 RESOURCE = cpu +CREATE = 1000 start: twistd --pidfile=$(PIDFILE) --logfile=$(LOGFILE) --python=$(TACFILE) @@ -25,4 +26,4 @@ get: curl -X GET http://127.0.0.1:$(HTTP_PORT)/$(RESOURCE) setup: - curl -X POST http://127.0.0.1:$(HTTP_PORT)/setup + curl -X POST http://127.0.0.1:$(HTTP_PORT)/setup?create=$(CREATE) diff --git a/scripts/scalability/test_controller/server/server.tac b/scripts/scalability/test_controller/server/server.tac index c525e3ed..3d5ea8cc 100644 --- a/scripts/scalability/test_controller/server/server.tac +++ b/scripts/scalability/test_controller/server/server.tac @@ -206,7 +206,8 @@ class MonitorResource(resource.Resource): class SetupResource(resource.Resource): def render_POST(self, request): - d = ensure_dbs() + create = request.args.get('create') or [1000] + d = ensure_dbs(create=int(create.pop())) d.addCallback(self._success, request) d.addErrback(self._error, request) return server.NOT_DONE_YET @@ -216,7 +217,8 @@ class SetupResource(resource.Resource): request.finish() def _error(self, e, request): - logger.error('Error processing request: %s' % e.getErrorMessage()) + message = e.getErrorMessage() if e.getErrorMessage() else repr(e) + logger.error('Error processing request: %s' % message) request.setResponseCode(500) request.write(json.dumps({'error': str(e)})) request.finish() diff --git a/scripts/scalability/test_controller/server/user_dbs.py b/scripts/scalability/test_controller/server/user_dbs.py index a1a9c222..70fbd96d 100755 --- a/scripts/scalability/test_controller/server/user_dbs.py +++ b/scripts/scalability/test_controller/server/user_dbs.py @@ -3,8 +3,12 @@ # Handle creation of user databases for scalability tests. import argparse +import json +import os +import time import treq +from hashlib import sha512 from functools import partial from urlparse import urljoin @@ -38,17 +42,25 @@ def get_db_names(create): semaphore = defer.DeferredSemaphore(20) -def _log(db, action, res): - logger.info('table %s %s' % (db, action)) +def _log(db, item, action, res): + logger.info('%s %s %s' % (item, db, action)) return res +def _req(method, *args, **kwargs): + method = getattr(treq, method) + auth = os.environ.get('HTTP_AUTH') + if auth: + kwargs.update({'auth': tuple(auth.split(':'))}) + return method(*args, **kwargs) + + @defer.inlineCallbacks def delete_dbs(dbs): deferreds = [] for db in dbs: - d = semaphore.run(treq.delete, urljoin(COUCH_URL, db)) - logfun = partial(_log, db, 'deleted') + d = semaphore.run(_req, 'delete', urljoin(COUCH_URL, db)) + logfun = partial(_log, 'table', db, 'deleted') d.addCallback(logfun) deferreds.append(d) responses = yield defer.gatherResults(deferreds) @@ -60,8 +72,8 @@ def delete_dbs(dbs): def create_dbs(dbs): deferreds = [] for db in dbs: - d = semaphore.run(treq.put, urljoin(COUCH_URL, db)) - logfun = partial(_log, db, 'created') + d = semaphore.run(_req, 'put', urljoin(COUCH_URL, db)) + logfun = partial(_log, 'table', db, 'created') d.addCallback(logfun) deferreds.append(d) responses = yield defer.gatherResults(deferreds) @@ -69,11 +81,44 @@ def create_dbs(dbs): assert all(map(lambda c: c == 201, codes)) +def _get_tokens_db_name(): + prefix = 'tokens_' + expire = 30 * 24 * 3600 + db_name = prefix + str(int(time.time() / expire)) + return db_name + + +@defer.inlineCallbacks +def _create_token(res, url, user_id): + data = {'user_id': user_id, 'type': 'Token'} + if res.code == 200: + current = yield res.json() + data['_rev'] = current['_rev'] + res = yield _req('put', url, json.dumps(data)) + defer.returnValue(res) + + +def create_tokens(create): + deferreds = [] + tokens_db = _get_tokens_db_name() + for i in xrange(create): + user_id = str(i) + token = sha512('%s-token' % user_id).hexdigest() + url = '/'.join([COUCH_URL, tokens_db, token]) + d = semaphore.run(_req, 'get', url) + d.addCallback(_create_token, url, user_id) + logfun = partial(_log, 'token', user_id, 'created') + d.addCallback(logfun) + deferreds.append(d) + return defer.gatherResults(deferreds) + + @defer.inlineCallbacks def ensure_dbs(couch_url=COUCH_URL, create=CREATE): dbs = get_db_names(create) yield delete_dbs(dbs) yield create_dbs(dbs) + yield create_tokens(create) @defer.inlineCallbacks |