|
5 | 5 | import os |
6 | 6 | import subprocess |
7 | 7 | import tempfile |
| 8 | +from collections import namedtuple |
| 9 | +from functools import reduce |
| 10 | +from tqdm import tqdm |
8 | 11 |
|
9 | 12 | # Own |
10 | 13 | from report import LighthouseReport |
@@ -70,3 +73,30 @@ def _get_report(self): |
70 | 73 |
|
71 | 74 | def _clean(self): |
72 | 75 | os.remove(self.__report_path) |
| 76 | + |
| 77 | + |
| 78 | +class LighthouseRepeatRunner(object): |
| 79 | + def __init__(self, url, form_factor='mobile', quiet=True, |
| 80 | + additional_settings=None, repeats=3): |
| 81 | + reports = [] |
| 82 | + |
| 83 | + progress = tqdm(range(1, repeats + 1), desc='Repeating test') |
| 84 | + |
| 85 | + for i in progress: |
| 86 | + progress.set_description('Run {0}/{1}'.format(i, repeats)) |
| 87 | + reports.append(LighthouseRunner(url, form_factor=form_factor, |
| 88 | + quiet=quiet, |
| 89 | + additional_settings=additional_settings).report) # noqa: E501 |
| 90 | + |
| 91 | + report = namedtuple('LighthouseAveragedReport', 'timings, score') |
| 92 | + self.report = report( |
| 93 | + timings=self._get_average([getattr(x, 'timings') for x in reports]), # noqa: E501 |
| 94 | + score=self._get_average([getattr(x, 'score') for x in reports]) |
| 95 | + ) |
| 96 | + |
| 97 | + def _get_average(self, obj_lst): |
| 98 | + ret = {} |
| 99 | + for key in obj_lst[0].keys(): |
| 100 | + lst = [x.get(key) for x in obj_lst] |
| 101 | + ret[key] = reduce(lambda a, b: a + b, lst) / len(lst) |
| 102 | + return ret |
0 commit comments