Browse Source

Add very simple benchmarking support when debugging

cached-url-for
TheAMM 2 years ago
parent
commit
b0c51e9fa0
  1. 13
      nyaa/__init__.py
  2. 29
      utils/simple_bench.py

13
nyaa/__init__.py

@ -34,6 +34,19 @@ def create_app(config):
request.headers['Expires'] = '0'
return request
# Add a timer header to the requests when debugging
# This gives us a simple way to benchmark requests off-app
import time
@app.before_request
def timer_before_request():
flask.g.request_start_time = time.time()
@app.after_request
def timer_after_request(request):
request.headers['X-Timer'] = time.time() - flask.g.request_start_time
return request
else:
app.logger.setLevel(logging.WARNING)

29
utils/simple_bench.py

@ -0,0 +1,29 @@
#!/usr/bin/env python3
# Simple benchmark tool, requires X-Timer header in the response headers
import requests
BASE_URL = 'http://127.0.0.1:5500/'
PAGES = 10
PER_PAGE = 20
def do_time(url):
r = requests.get(url)
return float(r.headers['X-Timer'])
print('Warmup:', do_time(BASE_URL))
for i in range(1, PAGES + 1):
page_url = BASE_URL + '?=' + str(i)
page_times = [
do_time(page_url) for _ in range(PER_PAGE)
]
print('Page {:3d}: min:{:5.1f}ms max:{:5.1f}ms avg:{:5.1f}ms'.format(
i,
min(page_times) * 1000,
max(page_times) * 1000,
sum(page_times) / len(page_times) * 1000
))
Loading…
Cancel
Save