mirror of
https://gitlab.com/SIGBUS/nyaa.git
synced 2024-12-30 18:01:54 +00:00
Add very simple benchmarking support when debugging
This commit is contained in:
parent
db83989d5d
commit
b0c51e9fa0
|
@ -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
Executable file
29
utils/simple_bench.py
Executable file
|
@ -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…
Reference in a new issue