Skip to content

Instantly share code, notes, and snippets.

@theSage21
Created June 20, 2015 13:46
Show Gist options
  • Save theSage21/65480feaf5c2532a1b2e to your computer and use it in GitHub Desktop.
Save theSage21/65480feaf5c2532a1b2e to your computer and use it in GitHub Desktop.
Speedtesting
from time import time
# dummy functions
def html_minify(content, ignore_comments, parser):
"Constant time function"
return content
class Request:
def __init__(self):
self._hit_htmlmin = True
class Response:
def __init__(self):
self.status_code = 200
self.content = None
# normal class emulator
class HtmlMinifyMiddleware(object):
def can_minify_response(self, request, response):
"Constant time function"
try:
req_ok = request._hit_htmlmin
except AttributeError:
return False
resp_ok = response.status_code == 200
return req_ok and resp_ok
def process_response(self, request, response):
"Constant time function"
minify = True
keep_comments = False
parser = None
if minify and self.can_minify_response(request, response):
response.content = html_minify(response.content,
ignore_comments=not keep_comments,
parser=parser)
return response
# different class emulator
def outside_can_minify_response(request, response):
"Constant time function"
try:
req_ok = request._hit_htmlmin
except AttributeError:
return False
resp_ok = response.status_code == 200
return req_ok and resp_ok
class MyMini(object):
def process_response(self, request, response):
"Constant time function uses normal function"
minify = True
keep_comments = False
parser = None
if minify and outside_can_minify_response(request, response):
response.content = html_minify(response.content,
ignore_comments=not keep_comments,
parser=parser)
return response
# test_ function
def test_speed(fn, iterations):
timelist = []
for i in range(iterations):
req = Request()
resp = Response()
start = time()
fn(req, resp)
end = time()
timelist.append(end - start)
return timelist
if __name__ == '__main__':
mini1 = HtmlMinifyMiddleware()
fn1 = mini1.process_response
mini2 = MyMini()
fn2 = mini2.process_response
# ---------
time_list_1 = test_speed(fn1, 50000)
time_list_2 = test_speed(fn2, 50000)
print(sum(time_list_1))
print(sum(time_list_2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment