Ejemplo n.º 1
0
def get_results_sync(
    browser,
    h3: bool,
    endpoint: Endpoint,
    warmup: bool,
) -> json:
    # set up the browser context and page
    context = browser.new_context()
    page = context.new_page()
    url = endpoint.get_url()
    logger.debug(f"Navigating to url: {url}")

    # warm up the browser
    warmup_if_specified_sync(page, url, warmup)
    # attempt to navigate to the url
    try:
        # set the timeout to be 1 min, because under some bad network condition,
        # connection and data transfer take longer
        page.set_default_timeout(60000)
        response = page.goto(url)
        # getting performance timing data
        # if we don't stringify and parse, things break
        timing_function = '''JSON.stringify(window.performance.getEntriesByType("navigation")[0])'''
        performance_timing = json.loads(page.evaluate(timing_function))
        performance_timing['server'] = response.headers['server']
        if response.status == 404:
            logger.error("404 Response Code")
            performance_timing = {'error': '404'}
    except Exception as e:
        # if we run into error, write it in the database
        logger.error(str(e))
        performance_timing = {'error': str(e)}
        pass
    browser.close()
    return performance_timing