mirror of
				https://github.com/searxng/searxng.git
				synced 2025-11-04 03:27:06 -05:00 
			
		
		
		
	[typing] add results.Timing
This commit is contained in:
		
							parent
							
								
									1ed618222f
								
							
						
					
					
						commit
						fdf562bc32
					
				@ -2,7 +2,9 @@ import re
 | 
				
			|||||||
from collections import defaultdict
 | 
					from collections import defaultdict
 | 
				
			||||||
from operator import itemgetter
 | 
					from operator import itemgetter
 | 
				
			||||||
from threading import RLock
 | 
					from threading import RLock
 | 
				
			||||||
 | 
					from typing import List, NamedTuple
 | 
				
			||||||
from urllib.parse import urlparse, unquote
 | 
					from urllib.parse import urlparse, unquote
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from searx import logger
 | 
					from searx import logger
 | 
				
			||||||
from searx.engines import engines
 | 
					from searx.engines import engines
 | 
				
			||||||
from searx.metrics import histogram_observe, counter_add, count_error
 | 
					from searx.metrics import histogram_observe, counter_add, count_error
 | 
				
			||||||
@ -137,6 +139,12 @@ def result_score(result):
 | 
				
			|||||||
    return sum((occurences * weight) / position for position in result['positions'])
 | 
					    return sum((occurences * weight) / position for position in result['positions'])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Timing(NamedTuple):
 | 
				
			||||||
 | 
					    engine: str
 | 
				
			||||||
 | 
					    total: float
 | 
				
			||||||
 | 
					    load: float
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ResultContainer:
 | 
					class ResultContainer:
 | 
				
			||||||
    """docstring for ResultContainer"""
 | 
					    """docstring for ResultContainer"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -169,7 +177,7 @@ class ResultContainer:
 | 
				
			|||||||
        self._closed = False
 | 
					        self._closed = False
 | 
				
			||||||
        self.paging = False
 | 
					        self.paging = False
 | 
				
			||||||
        self.unresponsive_engines = set()
 | 
					        self.unresponsive_engines = set()
 | 
				
			||||||
        self.timings = []
 | 
					        self.timings: List[Timing] = []
 | 
				
			||||||
        self.redirect_url = None
 | 
					        self.redirect_url = None
 | 
				
			||||||
        self.on_result = lambda _: True
 | 
					        self.on_result = lambda _: True
 | 
				
			||||||
        self._lock = RLock()
 | 
					        self._lock = RLock()
 | 
				
			||||||
@ -405,13 +413,8 @@ class ResultContainer:
 | 
				
			|||||||
        if engines[engine_name].display_error_messages:
 | 
					        if engines[engine_name].display_error_messages:
 | 
				
			||||||
            self.unresponsive_engines.add((engine_name, error_type, error_message, suspended))
 | 
					            self.unresponsive_engines.add((engine_name, error_type, error_message, suspended))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def add_timing(self, engine_name, engine_time, page_load_time):
 | 
					    def add_timing(self, engine_name: str, engine_time: float, page_load_time: float):
 | 
				
			||||||
        timing = {
 | 
					        self.timings.append(Timing(engine_name, total=engine_time, load=page_load_time))
 | 
				
			||||||
            'engine': engines[engine_name].shortcut,
 | 
					 | 
				
			||||||
            'total': engine_time,
 | 
					 | 
				
			||||||
            'load': page_load_time,
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        self.timings.append(timing)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def get_timings(self):
 | 
					    def get_timings(self):
 | 
				
			||||||
        return self.timings
 | 
					        return self.timings
 | 
				
			||||||
 | 
				
			|||||||
@ -56,6 +56,7 @@ from searx import (
 | 
				
			|||||||
    searx_debug,
 | 
					    searx_debug,
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
from searx.data import ENGINE_DESCRIPTIONS
 | 
					from searx.data import ENGINE_DESCRIPTIONS
 | 
				
			||||||
 | 
					from searx.results import Timing
 | 
				
			||||||
from searx.settings_defaults import OUTPUT_FORMATS
 | 
					from searx.settings_defaults import OUTPUT_FORMATS
 | 
				
			||||||
from searx.settings_loader import get_default_settings_path
 | 
					from searx.settings_loader import get_default_settings_path
 | 
				
			||||||
from searx.exceptions import SearxParameterException
 | 
					from searx.exceptions import SearxParameterException
 | 
				
			||||||
@ -234,7 +235,7 @@ class ExtendedRequest(flask.Request):
 | 
				
			|||||||
    form: Dict[str, str]
 | 
					    form: Dict[str, str]
 | 
				
			||||||
    start_time: float
 | 
					    start_time: float
 | 
				
			||||||
    render_time: float
 | 
					    render_time: float
 | 
				
			||||||
    timings: List[dict]
 | 
					    timings: List[Timing]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
request = typing.cast(ExtendedRequest, flask.request)
 | 
					request = typing.cast(ExtendedRequest, flask.request)
 | 
				
			||||||
@ -585,15 +586,14 @@ def post_request(response):
 | 
				
			|||||||
        'render;dur=' + str(round(request.render_time * 1000, 3)),
 | 
					        'render;dur=' + str(round(request.render_time * 1000, 3)),
 | 
				
			||||||
    ]
 | 
					    ]
 | 
				
			||||||
    if len(request.timings) > 0:
 | 
					    if len(request.timings) > 0:
 | 
				
			||||||
        timings = sorted(request.timings, key=lambda v: v['total'])
 | 
					        timings = sorted(request.timings, key=lambda t: t.total)
 | 
				
			||||||
        timings_total = [
 | 
					        timings_total = [
 | 
				
			||||||
            'total_' + str(i) + '_' + v['engine'] + ';dur=' + str(round(v['total'] * 1000, 3))
 | 
					            'total_' + str(i) + '_' + t.engine + ';dur=' + str(round(t.total * 1000, 3)) for i, t in enumerate(timings)
 | 
				
			||||||
            for i, v in enumerate(timings)
 | 
					 | 
				
			||||||
        ]
 | 
					        ]
 | 
				
			||||||
        timings_load = [
 | 
					        timings_load = [
 | 
				
			||||||
            'load_' + str(i) + '_' + v['engine'] + ';dur=' + str(round(v['load'] * 1000, 3))
 | 
					            'load_' + str(i) + '_' + t.engine + ';dur=' + str(round(t.load * 1000, 3))
 | 
				
			||||||
            for i, v in enumerate(timings)
 | 
					            for i, t in enumerate(timings)
 | 
				
			||||||
            if v.get('load')
 | 
					            if t.load
 | 
				
			||||||
        ]
 | 
					        ]
 | 
				
			||||||
        timings_all = timings_all + timings_total + timings_load
 | 
					        timings_all = timings_all + timings_total + timings_load
 | 
				
			||||||
    response.headers.add('Server-Timing', ', '.join(timings_all))
 | 
					    response.headers.add('Server-Timing', ', '.join(timings_all))
 | 
				
			||||||
 | 
				
			|||||||
@ -3,6 +3,7 @@
 | 
				
			|||||||
import json
 | 
					import json
 | 
				
			||||||
from urllib.parse import ParseResult
 | 
					from urllib.parse import ParseResult
 | 
				
			||||||
from mock import Mock
 | 
					from mock import Mock
 | 
				
			||||||
 | 
					from searx.results import Timing
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import searx.search.processors
 | 
					import searx.search.processors
 | 
				
			||||||
from searx.search import Search
 | 
					from searx.search import Search
 | 
				
			||||||
@ -46,7 +47,10 @@ class ViewsTestCase(SearxTestCase):
 | 
				
			|||||||
            },
 | 
					            },
 | 
				
			||||||
        ]
 | 
					        ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        timings = [{'engine': 'startpage', 'total': 0.8, 'load': 0.7}, {'engine': 'youtube', 'total': 0.9, 'load': 0.6}]
 | 
					        timings = [
 | 
				
			||||||
 | 
					            Timing(engine='startpage', total=0.8, load=0.7),
 | 
				
			||||||
 | 
					            Timing(engine='youtube', total=0.9, load=0.6),
 | 
				
			||||||
 | 
					        ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        def search_mock(search_self, *args):
 | 
					        def search_mock(search_self, *args):
 | 
				
			||||||
            search_self.result_container = Mock(
 | 
					            search_self.result_container = Mock(
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user