channy14

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
import time
import os
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

def get_clear_browsing_button(driver):
    """Find the "CLEAR BROWSING BUTTON" on the Chrome settings page."""
    return driver.find_element_by_css_selector('* /deep/ #clearBrowsingDataConfirm')

def clear_cache(driver, timeout=60):
    """Clear the cookies and cache for the ChromeDriver instance."""
    # navigate to the settings page
    driver.get('chrome://settings/clearBrowserData')
    # wait for the button to appear
    wait = WebDriverWait(driver, timeout)
    wait.until(get_clear_browsing_button)
    # click the button to clear the cache
    get_clear_browsing_button(driver).click()
    # wait for the button to be gone before returning
    wait.until_not(get_clear_browsing_button)

"""
Use Selenium to Measure Web Timing
Performance Timing Events flow
navigationStart -> redirectStart -> redirectEnd -> fetchStart -> domainLookupStart -> domainLookupEnd
-> connectStart -> connectEnd -> requestStart -> responseStart -> responseEnd
-> domLoading -> domInteractive -> domContentLoaded -> domComplete -> loadEventStart -> loadEventEnd
https://www.w3.org/TR/navigation-timing/#sec-window.performance-attribute
"""
def access_url(driver, url):
    # clear_cache(driver)
    driver.get(url)
    navigation_start = driver.execute_script("return window.performance.timing.navigationStart")
    load_end = driver.execute_script("return window.performance.timing.loadEventEnd")
    return load_end - navigation_start;

def test_url(driver, log_file, url, count):
    data = []
    for idx in range(count):
        load_time = access_url(driver, url)
        data.append(load_time)
    soted_data = sorted(data)
    min_val = soted_data[0]
    mid_val = soted_data[count//2]
    max_val = soted_data[-1]
    avg_val = sum(data)/count

    log_file.write("test '%s' %d times min:%d mid:%d max:%d avg:%d data:%s\n" % 
        (url, count, min_val, mid_val, max_val, avg_val, str(data)))

if __name__ == "__main__":
    pwd_path = os.getcwd()
    options = webdriver.chrome.options.Options()
    #options.add_argument("--load-extension=" + os.path.join(pwd_path, "Adblock-Plus_v1.13.4"))
    options.add_argument("--user-data-dir=" + os.path.join(pwd_path, "chrome_user_data"))
    options.add_argument("--start-maximized")
    #options.add_extension(os.path.join(pwd_path, "AdBlock_v3.22.1.crx"))
    options.add_extension(os.path.join(pwd_path, "Adblock-Plus_v1.13.4.crx"))
    driver = webdriver.Chrome(chrome_options=options)
    
    url_list = [
        #"北京时间",
        "https://www.btime.com",
        "https://www.btime.com/sports",
        "https://item.btime.com/m_2s21ridqq1l",
        #"军事头条",
        "http://www.tiexue.net/",
        "http://bbs.tiexue.net/post2_12837139_1.html",
        "http://www.tiexue.net/ShowPicClass_1485_1.html",
    ]
    test_times = 20;
    
    with open('chrome_Adblock-Plus.txt', 'w') as log_file:
        for url in url_list:
            test_url(driver, log_file, url, test_times)
    
    driver.close()

 

posted on 2018-05-18 10:00  channy14  阅读(922)  评论(0编辑  收藏  举报