-
Notifications
You must be signed in to change notification settings - Fork 58
/
benchmark.py
85 lines (68 loc) · 2.62 KB
/
benchmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from time import time
from utils import run_comparison_benchmark
from django.test.client import Client
from django.conf import global_settings
from django.conf import settings
from django.core.handlers.wsgi import WSGIRequest
from django.core.handlers.wsgi import WSGIHandler
from default_middleware.views import index
class RequestFactory(Client):
"""
Class that lets you create mock Request objects for use in testing.
Usage:
rf = RequestFactory()
get_request = rf.get('/hello/')
post_request = rf.post('/submit/', {'foo': 'bar'})
This class re-uses the django.test.client.Client interface, docs here:
http://www.djangoproject.com/documentation/testing/#the-test-client
Once you have a request object you can pass it to any view function,
just as if that view had been hooked up using a URLconf.
Author: Simon (http://djangosnippets.org/users/simon/)
djangosnippet URL: (http://djangosnippets.org/snippets/963/)
"""
def request(self, **request):
"""
Similar to parent class, but returns the request object as soon as it
has created it.
"""
environ = {
'HTTP_COOKIE': self.cookies,
'PATH_INFO': '/',
'QUERY_STRING': '',
'REQUEST_METHOD': 'GET',
'SCRIPT_NAME': '',
'SERVER_NAME': 'testserver',
'SERVER_PORT': 80,
'SERVER_PROTOCOL': 'HTTP/1.1',
}
environ.update(self.defaults)
environ.update(request)
return WSGIRequest(environ)
def setup():
global req_factory, handler_default_middleware, handler_no_middleware
req_factory = RequestFactory()
settings.MIDDLEWARE_CLASSES = global_settings.MIDDLEWARE_CLASSES
handler_default_middleware = WSGIHandler()
handler_default_middleware.load_middleware()
settings.MIDDLEWARE_CLASSES = []
handler_no_middleware = WSGIHandler()
handler_no_middleware.load_middleware()
def benchmark_request(middleware_classes):
settings.MIDDLEWARE_CLASSES = middleware_classes
req_factory = RequestFactory()
handler = WSGIHandler()
handler.load_middleware()
handler.get_response(req_factory.get('/'))
def benchmark_default_middleware():
global req_factory, handler_default_middleware
handler_default_middleware.get_response(req_factory.get('/'))
def benchmark_no_middleware():
global req_factory, handler_no_middleware
handler_no_middleware.get_response(req_factory.get('/'))
run_comparison_benchmark(
benchmark_default_middleware,
benchmark_no_middleware,
setup = setup,
syncdb = False,
trials = 50
)