-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-library-database.py
64 lines (51 loc) · 1.88 KB
/
test-library-database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
import time
import argparse
from selenium import webdriver
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.chrome.options import Options as ChromeOptions
from tqdm import tqdm
import json
def get_test_target() -> list[dict]:
"get test target site from BIT library"
try:
with open("db_list_output.json") as f:
return json.loads(f.read())
except:
print("plz run get-library-database.py")
exit(0)
def get_driver(browser: str) -> webdriver.remote.webdriver.WebDriver:
"get browser driver, set proxy"
if browser == 'firefox':
options = FirefoxOptions()
options.headless = True
return webdriver.Firefox(options=options)
else:
options = ChromeOptions()
options.headless = True
return webdriver.Chrome(options=options)
def do_test_single(browser: webdriver.remote.webdriver.WebDriver, url: dict):
"open a single page, print the time"
start_time = time.time()
browser.get(url["url"])
used_time = time.time() - start_time
tqdm.write(f"{url["name"]} {url["url"]} load finish in {used_time}s")
return url["name"], url["url"], used_time
def do_test_all(browser: webdriver.remote.webdriver.WebDriver, urls: list[dict]):
"test all urls"
test_results = []
for url in tqdm(urls):
test_results.append(do_test_single(browser, url))
return test_results
def main():
"main program"
parser = argparse.ArgumentParser()
parser.add_argument('browser', choices=['chrome', 'firefox'])
args = parser.parse_args()
urls = get_test_target()
with get_driver(args.browser) as driver:
test_results = do_test_all(driver, urls)
with open("library_results.txt", "w") as f:
f.writelines((repr(result) for result in test_results))
if __name__ == '__main__':
main()