|
| 1 | +from bs4 import BeautifulSoup |
| 2 | +import re |
| 3 | +import sys |
| 4 | +import json |
| 5 | + |
| 6 | +ACCEPTANCE_RANGE=0.2 |
| 7 | + |
| 8 | + |
| 9 | +def analyze_sleepgraph_file(file, thresholds, acceptance_range=ACCEPTANCE_RANGE): |
| 10 | + with open(file, 'r', encoding='utf-8') as f: |
| 11 | + soup = BeautifulSoup(f, 'lxml') |
| 12 | + complete_results={} |
| 13 | + test_passed=True |
| 14 | + |
| 15 | + components=thresholds.keys() |
| 16 | + for component in components: |
| 17 | + results = {} |
| 18 | + divs = soup.find_all("div", title=lambda t: t and component in t) |
| 19 | + |
| 20 | + for div in divs: |
| 21 | + title = div.get('title') |
| 22 | + # print(title) |
| 23 | + match = re.search(r'\((\d+(?:\.\d+)?)\s*ms\)\s+(\S+)$', title) |
| 24 | + if match: |
| 25 | + time_ms = float(match.group(1)) |
| 26 | + measurement_name = match.group(2) |
| 27 | + if measurement_name in thresholds[component]: |
| 28 | + results[measurement_name] = {"value": time_ms, "pass": True} |
| 29 | + threshold = float(thresholds[component][measurement_name]) |
| 30 | + if time_ms<(threshold * (1-acceptance_range)) and time_ms>(threshold * (1+acceptance_range)): |
| 31 | + results[measurement_name]["pass"] = False |
| 32 | + test_passed = False |
| 33 | + |
| 34 | + complete_results[component]=results |
| 35 | + |
| 36 | + print(complete_results) |
| 37 | + return test_passed, complete_results |
| 38 | + |
| 39 | + |
| 40 | +if __name__ == "__main__": |
| 41 | + if len(sys.argv) != 3: |
| 42 | + print("Incorrect args. Usage: python3 analyze-sleepgraph-results.py <path_to_html_file> <thresholds_as_json_str>") |
| 43 | + sys.exit(1) |
| 44 | + |
| 45 | + sleepgraph_file = sys.argv[1] |
| 46 | + thresholds = json.loads(sys.argv[2]) |
| 47 | + |
| 48 | + test_passed, results = analyze_sleepgraph_file(sleepgraph_file, thresholds) |
| 49 | + sys.exit(0 if test_passed else 1) |
0 commit comments