1
0
mirror of https://github.com/searxng/searxng.git synced 2024-11-22 12:10:11 +01:00

[enh] new stat: average request duration

This commit is contained in:
asciimoo 2013-10-27 01:23:13 +02:00
parent 66d9619862
commit 43ac547bc1

View File

@ -25,6 +25,7 @@ from urlparse import urlparse
from searx import settings from searx import settings
import ConfigParser import ConfigParser
import sys import sys
from datetime import datetime
engine_dir = dirname(realpath(__file__)) engine_dir = dirname(realpath(__file__))
searx_dir = join(engine_dir, '../../') searx_dir = join(engine_dir, '../../')
@ -67,7 +68,7 @@ for section in engines_config.sections():
print '[E] Engine config error: Missing attribute "{0}.{1}"'.format(engine.name, engine_attr) print '[E] Engine config error: Missing attribute "{0}.{1}"'.format(engine.name, engine_attr)
sys.exit(1) sys.exit(1)
engines[engine.name] = engine engines[engine.name] = engine
engine.stats = {'result_count': 0, 'search_count': 0} engine.stats = {'result_count': 0, 'search_count': 0, 'page_load_time': 0}
if hasattr(engine, 'categories'): if hasattr(engine, 'categories'):
for category_name in engine.categories: for category_name in engine.categories:
categories.setdefault(category_name, []).append(engine) categories.setdefault(category_name, []).append(engine)
@ -81,6 +82,7 @@ def make_callback(engine_name, results, callback, params):
def process_callback(response, **kwargs): def process_callback(response, **kwargs):
cb_res = [] cb_res = []
response.search_params = params response.search_params = params
engines[engine_name].stats['page_load_time'] += (datetime.now() - params['started']).total_seconds()
for result in callback(response): for result in callback(response):
result['engine'] = engine_name result['engine'] = engine_name
cb_res.append(result) cb_res.append(result)
@ -104,6 +106,7 @@ def search(query, request, selected_categories):
request_params = default_request_params() request_params = default_request_params()
request_params['headers']['User-Agent'] = user_agent request_params['headers']['User-Agent'] = user_agent
request_params['category'] = selected_engine['category'] request_params['category'] = selected_engine['category']
request_params['started'] = datetime.now()
request_params = engine.request(query, request_params) request_params = engine.request(query, request_params)
callback = make_callback(selected_engine['name'], results, engine.response, request_params) callback = make_callback(selected_engine['name'], results, engine.response, request_params)
if request_params['method'] == 'GET': if request_params['method'] == 'GET':
@ -161,6 +164,10 @@ def get_engines_stats():
for engine in engines.values(): for engine in engines.values():
if engine.stats['search_count'] == 0: if engine.stats['search_count'] == 0:
continue continue
stats[engine.name] = {'Average number of results': engine.stats['result_count']/float(engine.stats['search_count'])} results_num = engine.stats['result_count']/float(engine.stats['search_count'])
load_times = engine.stats['page_load_time']/float(engine.stats['search_count'])
stats[engine.name] = {'Average number of results': results_num
,'Average page load time': load_times
}
return stats return stats