diff options
author | Alexandre Flament <alex@al-f.net> | 2019-07-17 10:38:45 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-17 10:38:45 +0200 |
commit | 554a21e1d07f3b434b5097b4e3d49e1403be7527 (patch) | |
tree | e4917091b8e32690256fabf64addfc1ea187ba67 /searx/webapp.py | |
parent | cfcbc3a5c344037fb5423c14223e72578170a234 (diff) | |
download | searxng-554a21e1d07f3b434b5097b4e3d49e1403be7527.tar.gz searxng-554a21e1d07f3b434b5097b4e3d49e1403be7527.zip |
[enh] Add Server-Timing header (#1637)
Server Timing specification: https://www.w3.org/TR/server-timing/
In the browser Dev Tools, focus on the main request, there are the responses per engine in the Timing tab.
Diffstat (limited to 'searx/webapp.py')
-rw-r--r-- | searx/webapp.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/searx/webapp.py b/searx/webapp.py index 0e1fffe3f..856b013af 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -43,6 +43,7 @@ except: exit(1) from cgi import escape from datetime import datetime, timedelta +from time import time from werkzeug.contrib.fixers import ProxyFix from flask import ( Flask, request, render_template, url_for, Response, make_response, @@ -402,6 +403,8 @@ def render(template_name, override_theme=None, **kwargs): @app.before_request def pre_request(): + request.start_time = time() + request.timings = [] request.errors = [] preferences = Preferences(themes, list(categories.keys()), engines, plugins) @@ -437,6 +440,21 @@ def pre_request(): request.user_plugins.append(plugin) +@app.after_request +def post_request(response): + total_time = time() - request.start_time + timings_all = ['total;dur=' + str(round(total_time * 1000, 3))] + if len(request.timings) > 0: + timings = sorted(request.timings, key=lambda v: v['total']) + timings_total = ['total_' + str(i) + '_' + v['engine'] + + ';dur=' + str(round(v['total'] * 1000, 3)) for i, v in enumerate(timings)] + timings_load = ['load_' + str(i) + '_' + v['engine'] + + ';dur=' + str(round(v['load'] * 1000, 3)) for i, v in enumerate(timings)] + timings_all = timings_all + timings_total + timings_load + response.headers.add('Server-Timing', ', '.join(timings_all)) + return response + + def index_error(output_format, error_message): if output_format == 'json': return Response(json.dumps({'error': error_message}), @@ -515,6 +533,9 @@ def index(): # UI advanced_search = request.form.get('advanced_search', None) + # Server-Timing header + request.timings = result_container.get_timings() + # output for result in results: if output_format == 'html': |