-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathlighthouse-parallel.tpl
More file actions
90 lines (68 loc) · 2.25 KB
/
lighthouse-parallel.tpl
File metadata and controls
90 lines (68 loc) · 2.25 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
import argparse
import json
import sys
import time
import boto3
parser = argparse.ArgumentParser(
description='Invoke lighthouse in parallel in AWS Lambda')
parser.add_argument('--runs', default=1, type=int,
help='Number of runs per url')
parser.add_argument(
'--region',
type=str,
default="${lambda_init_region}",
help='AWS region')
parser.add_argument('urls', type=argparse.FileType('r'),
help='JSON file: an array of urls to test')
args = parser.parse_args()
lambda_client = boto3.client('lambda', region_name=args.region)
ddb_client = boto3.client('dynamodb', region_name=args.region)
lambda_payload = {
'urls': json.load(args.urls),
'runsPerUrl': args.runs,
# punch in your own options, from
# github.com/GoogleChrome/lighthouse/blob/888bd6dc9d927a734a8e20ea8a0248baa5b425ed/typings/externs.d.ts#L82-L119
'lighthouseOpts': {
'chromeFlags': [
'--headless',
'--disable-dev-shm-usage',
'--disable-gpu',
'--no-zygote',
'--no-sandbox',
'--single-process',
'--hide-scrollbars',
]
}
}
lambda_response = lambda_client.invoke(
FunctionName='${lambda_init_arn}',
Payload=json.dumps(lambda_payload),
)
lambda_response_payload = json.loads(
lambda_response['Payload'].read().decode("utf-8"))
job_id = lambda_response_payload['jobId']
def progress(count, total, status=''):
bar_len = 60
filled_len = int(round(bar_len * count / float(total)))
percents = round(100.0 * count / float(total), 1)
bar = '=' * filled_len + '-' * (bar_len - filled_len)
sys.stdout.write('[%s] %s%s | %s\r' % (bar, percents, '%', status))
sys.stdout.flush()
print(f'Job id: {job_id}')
while True:
response = ddb_client.get_item(
TableName='${jobs_table_name}',
Key={'JobId': {'S': job_id}}
)
total = int(response['Item']['PageCountTotal']['N'])
success = int(response['Item']['PageCountSuccess']['N'])
error = int(response['Item']['PageCountError']['N'])
progress(
success + error,
total,
f'Total runs: {total} | Succeeded: {success} | Failed: {error}')
if success + error >= total:
print('\n')
break
time.sleep(.5)