-
Notifications
You must be signed in to change notification settings - Fork 1
/
crux
executable file
·200 lines (183 loc) · 6.51 KB
/
crux
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/bin/bash
usage() {
echo -e "Usage: $0 [options...] <url> <key> \n\
-h, --help Display help
-o, --output <path> Output path where to save results \n\
-a, --append Append results to output file \n\
-O, --origin Collect data for origin-level \n\
-f, --full Include histogram data (grouped as Good, NI and Poor) \n\
-F, --format Output format (csv, md) \n\
-V, --verbose Make the operation more talkative" 1>&2
exit 1
}
output=
verbose=0
append=0
full=0
query=""
origin=0
format="csv"
while [[ $# -gt 0 ]]; do
case $1 in
-h | --help)
usage
shift
;;
-V | --verbose)
verbose=1
shift
;;
-a | --append)
append=1
shift
;;
-O | --origin)
origin=1
shift
;;
-F | --format)
format=$2
shift 2
;;
-f | --full)
full=1
shift
;;
-o | --output)
output=$2
shift 2
;;
*)
url=$1
key=$2
break
shift
;;
esac
done
if [ -z "$key" ]; then
echo "CrUX API key is missing or invalid."
exit 1
fi
if [ -z "$url" ]; then
usage
fi
if [ "$verbose" == 1 ]; then
echo -e "Running CrUX on $url"
echo -e "...\n"
fi
if [ "$append" == 0 ] && [ -f "$output" ]; then
read -p "Output file exists. Overwrite? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Nn]$ ]] || [[ -z $REPLY ]]; then
exit
fi
fi
if [ ! -z "$output" ]; then
if [ $append == 0 ]; then
if [ "$verbose" == 1 ]; then
echo "Overwriting contents in $output"
echo
fi
if [ "$full" == 1 ]; then
if [ "$format" == "md" ]; then
echo "Date|URL|Form|LCP|LCP (good)|LCP (NI)|LCP (poor)|CLS|CLS (good)|CLS (NI)|CLS (poor)|FCP|FCP (good)|FCP (NI)|FCP (poor)|INP|INP (good)|INP (NI)|INP (poor)|TTFB|TTFB (good)|TTFB (NI)|TTFB (poor)|RTT" >$output
echo "-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-" >$output
else
echo "Date,URL,Form,LCP,LCP (good),LCP (NI),LCP (poor),CLS,CLS (good),CLS (NI),CLS (poor),FCP,FCP (good),FCP (NI),FCP (poor),INP,INP (good),INP (NI),INP (poor),TTFB,TTFB (good),TTFB (NI),TTFB (poor),RTT" >$output
fi
else
if [ "$format" == "md" ]; then
echo "Date|URL|Form|LCP|CLS|FCP|INP|TTFB|RTT" >$output
echo "-|-|-|-|-|-|-|-|-" >$output
else
echo "Date,URL,Form,LCP,CLS,FCP,INP,TTFB,RTT" >$output
fi
fi
fi
else
if [ "$full" == 1 ]; then
if [ "$format" == "md" ]; then
echo "Date|URL|Form|LCP|LCP (good)|LCP (NI)|LCP (poor)|CLS|CLS (good)|CLS (NI)|CLS (poor)|FCP|FCP (good)|FCP (NI)|FCP (poor)|INP|INP (good)|INP (NI)|INP (poor)|TTFB|TTFB (good)|TTFB (NI)|TTFB (poor)|RTT"
echo "-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-"
else
echo "Date,URL,Form,LCP,LCP (good),LCP (NI),LCP (poor),CLS,CLS (good),CLS (NI),CLS (poor),FCP,FCP (good),FCP (NI),FCP (poor),INP,INP (good),INP (NI),INP (poor),TTFB,TTFB (good),TTFB (NI),TTFB (poor),RTT"
fi
else
if [ "$format" == "md" ]; then
echo "Date|URL|Form|LCP|CLS|FCP|INP|TTFB|RTT"
echo "-|-|-|-|-|-|-|-|-"
else
echo "Date,URL,Form,LCP,CLS,FCP,INP,TTFB,RTT"
fi
fi
fi
if [ "$origin" == 1 ]; then
data="{'origin': '$url', 'formFactor': 'PHONE'}"
else
data="{'url': '$url', 'formFactor': 'PHONE'}"
fi
if [ "$verbose" == 1 ]; then
echo -e "curl https://chromeuxreport.googleapis.com/v1/records:queryRecord?key=$key\n\
--header 'Content-Type: application/json'\n\
--data $data"
echo
fi
if [ "$full" == 1 ]; then
query='[(now | strftime("%Y/%m/%d")), .record.key.url + .record.key.origin, .record.key.formFactor, .record.metrics.largest_contentful_paint.percentiles.p75, .record.metrics.largest_contentful_paint.histogram[0].density, .record.metrics.largest_contentful_paint.histogram[1].density, .record.metrics.largest_contentful_paint.histogram[2].density, .record.metrics.cumulative_layout_shift.percentiles.p75, .record.metrics.cumulative_layout_shift.histogram[0].density, .record.metrics.cumulative_layout_shift.histogram[1].density, .record.metrics.cumulative_layout_shift.histogram[2].density, .record.metrics.first_contentful_paint.percentiles.p75, .record.metrics.first_contentful_paint.histogram[0].density, .record.metrics.first_contentful_paint.histogram[1].density, .record.metrics.first_contentful_paint.histogram[2].density, .record.metrics.interaction_to_next_paint.percentiles.p75, .record.metrics.interaction_to_next_paint.histogram[0].density, .record.metrics.interaction_to_next_paint.histogram[1].density, .record.metrics.interaction_to_next_paint.histogram[2].density, .record.metrics.experimental_time_to_first_byte.percentiles.p75, .record.metrics.experimental_time_to_first_byte.histogram[0].density, .record.metrics.experimental_time_to_first_byte.histogram[1].density, .record.metrics.experimental_time_to_first_byte.histogram[2].density, .record.metrics.round_trip_time.percentiles.p75]'
else
query='[(now | strftime("%Y/%m/%d")), .record.key.url + .record.key.origin, .record.key.formFactor, .record.metrics.largest_contentful_paint.percentiles.p75, .record.metrics.cumulative_layout_shift.percentiles.p75, .record.metrics.first_contentful_paint.percentiles.p75, .record.metrics.interaction_to_next_paint.percentiles.p75, .record.metrics.experimental_time_to_first_byte.percentiles.p75, .record.metrics.round_trip_time.percentiles.p75]'
fi
curl -s https://chromeuxreport.googleapis.com/v1/records:queryRecord?key=$key \
--header 'Content-Type: application/json' \
--data "$data" |
jq -r "$query |
@csv" |
if [ ! -z "$output" ]; then
if [ "$format" == "md" ]; then
sed -r 's/,/|/g' | tee 1>>$output 2>&1
else
tee 1>>$output 2>&1
fi
else
if [ "$format" == "md" ]; then
sed -r 's/,/|/g' | cat
else
cat
fi
fi
if [ "$origin" == 1 ]; then
data="{'origin': '$url', 'formFactor': 'DESKTOP'}"
else
data="{'url': '$url', 'formFactor': 'DESKTOP'}"
fi
if [ "$verbose" == 1 ]; then
echo -e "curl https://chromeuxreport.googleapis.com/v1/records:queryRecord?key=$key\n\
--header 'Content-Type: application/json'\n\
--data $data"
echo
fi
curl -s https://chromeuxreport.googleapis.com/v1/records:queryRecord?key=$key \
--header 'Content-Type: application/json' \
--data "$data" |
jq -r "$query |
@csv" |
if [ ! -z "$output" ]; then
if [ "$format" == "md" ]; then
sed -r 's/,/|/g' | tee 1>>$output 2>&1
else
tee 1>>$output 2>&1
fi
else
if [ "$format" == "md" ]; then
sed -r 's/,/|/g' | cat
else
cat
fi
fi
if [ ! -z "$output" ] && [ "$verbose" == 1 ]; then
echo -e "Saved results to $output\n"
fi
if [ "$verbose" == 1 ]; then
echo "Done."
fi