Skip to content

Commit 43e3a7b

Browse files
committed
added: batch runners
1 parent 3a26898 commit 43e3a7b

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,4 @@ dmypy.json
122122
.venv/
123123

124124
# End of https://www.gitignore.io/api/python
125+
*.csv

lighthouse/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
from .runner import LighthouseRunner
1+
from .runner import LighthouseRunner, LighthouseRepeatRunner
2+
from .batch import BatchRunner

lighthouse/batch.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from runner import LighthouseRepeatRunner
2+
from itertools import product
3+
from tqdm import tqdm
4+
5+
class BatchRunner(object):
6+
def __init__(self, urls, form_factors, quiet=True,
7+
additional_settings=None, repeats=3):
8+
9+
if not isinstance(form_factors, (list, )):
10+
form_factors = list(form_factors)
11+
if not isinstance(urls, (list, )):
12+
urls = list(urls)
13+
14+
all_combinations = tqdm(list(product(urls, form_factors)),
15+
desc='Running urls')
16+
17+
self.reports = []
18+
19+
for url, factor in all_combinations:
20+
all_combinations.set_description('{0} | {1}'.format(url, factor))
21+
report = LighthouseRepeatRunner(url, factor, quiet,
22+
additional_settings,
23+
repeats).report
24+
self.reports.append((url, factor, report))
25+

lighthouse/runner.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import os
66
import subprocess
77
import tempfile
8+
from collections import namedtuple
9+
from functools import reduce
10+
from tqdm import tqdm
811

912
# Own
1013
from report import LighthouseReport
@@ -70,3 +73,30 @@ def _get_report(self):
7073

7174
def _clean(self):
7275
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

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99
author_email='cupial.adam@gmail.com',
1010
url='n/a',
1111
packages=find_packages(),
12-
install_requires=[]
12+
install_requires=[
13+
'tqdm>=4.30',
14+
]
1315
)

0 commit comments

Comments
 (0)