forked from Mohabeldiin/test-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiSpeedFinal.py
More file actions
219 lines (185 loc) · 7.9 KB
/
ApiSpeedFinal.py
File metadata and controls
219 lines (185 loc) · 7.9 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"""a Module to interact with Google PageSpeed Insights API"""
from urllib import request
import requests
import json
import urllib3
def ApiSpeed(website: str):
"""Test Website Performance
Args:
website: URL of WebSite to Test
Returns:
Print Website Performance.
Example:
ApiSpeed(URL)
"""
url: str = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url={}&strategy=desktop&key=AIzaSyCcDNbRIvDPnuOM1TdCwoyzmP6NiGOkcLU".format(website)
"""API Request
parameter:
url: WebSite Link to Test
strategy: testing environment either {mobile, desktop}
key: API Key
"""
response = requests.get(url)
"""Requesting Performance Evaluation"""
data = json.loads(response.text)
"""Fetch JSON Response"""
le_fcp = data["loadingExperience"]["metrics"]["FIRST_CONTENTFUL_PAINT_MS"]["category"]
"""First Contentful Paint of loadingExperience
:returns: Four type of performances: “Slow“, “Average”, “None” and “Fast“.
"""
le_fid = data["loadingExperience"]["metrics"]["FIRST_INPUT_DELAY_MS"]["category"]
"""First Input Delay of loadingExperience
:returns: Four type of performances: “Slow“, “Average”, “None” and “Fast“.
"""
le_lcp = data["loadingExperience"]["metrics"]["LARGEST_CONTENTFUL_PAINT_MS"]["category"]
"""Largest Contentful Paint of loadingExperience
:returns: Four type of performances: “Slow“, “Average”, “None” and “Fast“.
"""
le_cls = data["loadingExperience"]["metrics"]["CUMULATIVE_LAYOUT_SHIFT_SCORE"]["category"]
"""Cumulative Layout Shift Score of loadingExperience
:returns: Four type of performances: “Slow“, “Average”, “None” and “Fast“.
"""
le_overall = data["loadingExperience"]["overall_category"]
"""Overall Performance of loadingExperience
:returns: Four type of performances: “Slow“, “Average”, “None” and “Fast“.
"""
print("\n")
print("loadingExperience:")
print("First Contentful Paint: {}".format(le_fcp))
print("First Input Delay: {}".format(le_fid))
print("Largest Contentful Paint: {}".format(le_lcp))
print("Cumulative Layout Shift Score: {}".format(le_cls))
print("Overall Performance: {}".format(le_overall))
print("")
ole_fcp = data["originLoadingExperience"]["metrics"]["FIRST_CONTENTFUL_PAINT_MS"]["category"]
"""First Contentful Paint of OriginLoadingExperience
:returns: Four type of performances: “Slow“, “Average”, “None” and “Fast“.
"""
ole_fid = data["originLoadingExperience"]["metrics"]["FIRST_INPUT_DELAY_MS"]["category"]
"""First Input Delay of OriginLoadingExperience
:returns: Four type of performances: “Slow“, “Average”, “None” and “Fast“.
"""
ole_lcp = data["originLoadingExperience"]["metrics"]["LARGEST_CONTENTFUL_PAINT_MS"]["category"]
"""Largest Contentful Paint of OriginLoadingExperience
:returns: Four type of performances: “Slow“, “Average”, “None” and “Fast“.
"""
ole_cls = data["originLoadingExperience"]["metrics"]["CUMULATIVE_LAYOUT_SHIFT_SCORE"]["category"]
"""Cumulative Layout Shift Score of OriginLoadingExperience
:returns: Four type of performances: “Slow“, “Average”, “None” and “Fast“.
"""
ole_overall = data["originLoadingExperience"]["overall_category"]
"""Overall Performance of OriginLoadingExperience
:returns: Four type of performances: “Slow“, “Average”, “None” and “Fast“.
"""
print("\n")
print("originLoadingExperience:")
print("First Contentful Paint: {}".format(ole_fcp))
print("First Input Delay: {}".format(ole_fid))
print("Largest Contentful Paint: {}".format(ole_lcp))
print("Cumulative Layout Shift Score: {}".format(ole_cls))
print("Overall Performance: {}".format(ole_overall))
print("")
lh_fcp = data["lighthouseResult"]["audits"]["first-contentful-paint"]["displayValue"]
"""First Contentful Paint of lighthouseResult
:returns: in Seconds.
"""
lh_tti = data["lighthouseResult"]["audits"]["interactive"]["displayValue"]
"""Time to Interactive of lighthouseResult
:returns: in Seconds.
"""
lh_lcp = data["lighthouseResult"]["audits"]["largest-contentful-paint"]["displayValue"]
"""Largest Contentful Paint of lighthouseResult
:returns: in Seconds.
"""
lh_cls = data["lighthouseResult"]["audits"]["cumulative-layout-shift"]["displayValue"]
"""Cumulative Layout Shift Score of lighthouseResult
:returns: in Seconds.
"""
lh_tbt = data["lighthouseResult"]["audits"]["total-blocking-time"]["displayValue"]
"""Total Blocking Time of lighthouseResult
:returns: in Seconds.
"""
lh_si = data["lighthouseResult"]["audits"]["speed-index"]["displayValue"]
"""Speed Index of lighthouseResult
:returns: in Seconds.
"""
for i in lh_fcp:
if i == " " or i == "s" or i == "ms"or i == "m":
lh_fcp =lh_fcp.replace(i,"")
for i in lh_tti:
if i == " " or i == "s" or i == "ms"or i == "m":
lh_tti =lh_tti.replace(i,"")
for i in lh_lcp:
if i == " " or i == "s" or i == "ms"or i == "m":
lh_lcp =lh_lcp.replace(i,"")
for i in lh_cls:
if i == " " or i == "s" or i == "ms"or i == "m":
lh_cls =lh_cls.replace(i,"")
for i in lh_tbt:
if i == " " or i == "s" or i == "ms"or i == "m":
lh_tbt =lh_tbt.replace(i,"")
for i in lh_si:
if i == " " or i == "s" or i == "ms" or i == "m":
lh_si =lh_si.replace(i,"")
print("\n")
print("lighthouseResult:")
print("First Contentful Paint: {}".format(lh_fcp))
print("Time to Interactive: {}".format(lh_tti))
print("Largest Contentful Paint: {}".format(lh_lcp))
print("Cumulative Layout Shift Score: {}".format(lh_cls))
print("Total Blocking Time: {}".format(lh_tbt))
print("Speed Index: {}".format(lh_si))
print("")
overall_performance = data["lighthouseResult"]["categories"]["performance"]["score"] * 100
"""Overall Performance of lighthouseResult
:returns: performances: in Percentage.
"""
print("Overall Performance: {}".format(overall_performance))
print("")
urlPost = "https://a5r-testing.herokuapp.com/createNewtestSpeed"
data = {
"loadingExperince":le_overall,
"LE_FCP":le_fcp,
"LE_FID":le_fid,
"LE_CLS":le_cls,
"LE_LCP":le_lcp,
"OriginLoadingExperince":ole_overall,
"OLE_FCP":ole_fcp,
"OLE_FID":ole_fid,
"OLE_CLS":ole_cls,
"OLE_LCP":ole_lcp,
"LH_TBT":lh_tbt,
"LH_SI":lh_si,
"LH_FCP":lh_fcp,
"LH_TTI=":lh_tti,
"LH_CLS":lh_cls,
"LH_LCP":lh_lcp,
"PR_Precentage":overall_performance
}
requests.post(url = urlPost , json=data , headers= { "Content-Type": "application/json" })
"""
post_url = "https://a5r-testing.herokuapp.com/createNewtestSpeed"
#print(len(data)-1)
#print(len(data['get']))
data = {
"loadingExperince": "ok",
"LE_FCP": "ok",
"LE_FID": "ok",
"LE_CLS": "ok",
"LE_LCP": "ok",
"OriginLoadingExperince": "ok",
"OLE_FCP": "ok",
"OLE_FID": "ok",
"OLE_CLS": "ok",
"OLE_LCP": "ok",
"LH_TBT": "1",
"LH_SI": "1",
"LH_FCP": "1",
"LH_TTI": "1",
"LH_CLS": "1",
"LH_LCP": "1",
"PR_Precentage": 1.5
}
requests.post(url=post_url, json=data, headers={'Content-Type': 'application/json'})"""
if __name__ == "__main__":
ApiSpeed("https://facebook.com")