forked from S2-group/android-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
136 lines (118 loc) · 7.38 KB
/
Copy pathserver.py
File metadata and controls
136 lines (118 loc) · 7.38 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
import threading
import time
import csv
import os
import http
from http.server import HTTPServer
import logging
navigationTime= {'fetchTime':0 , 'workerTime':0 , 'totalTime':0, 'downloadTime':0, 'timeToFirstByte':0, 'headerSize':0, 'dnsLookupTime': 0}
networkInf = {'downlink': 0, 'effectiveType': 0, 'rtt':0, 'saveData':False}
storageEstimate = {'quota':0, 'usage':0, 'caches':0, 'indexedDB':0, 'serviceWorker':0}
fpResult = {'fp':0}
fcpResult = {'fcp':0}
fidResult = {'fid':0}
lcpResult = {'lcp':0}
clsResult = {'cls':0}
tbtResult = {'tbt':0}
httpd = 0
class HTTPHandler(http.server.SimpleHTTPRequestHandler):
def do_POST(self):
logging.getLogger(self.__class__.__name__).info("POST request received")
length = int(self.headers['Content-Length']) # <--- Gets the size of data
data_string = self.rfile.read(length)
data_string = data_string.decode("utf-8")
logging.getLogger(self.__class__.__name__).debug(f"Data: {data_string}")
if 'perfumeResults' in data_string:
if 'navigationTiming' in data_string:
navigationTime['fetchTime'] = data_string.split("fetchTime\":")[1].split(",\"workerTime")[0]
navigationTime['workerTime'] = data_string.split("workerTime\":")[1].split(",\"totalTime")[0]
navigationTime['totalTime'] = data_string.split("totalTime\":")[1].split(",\"downloadTime")[0]
navigationTime['downloadTime'] = data_string.split("downloadTime\":")[1].split(",\"timeToFirstByte")[0]
navigationTime['timeToFirstByte'] = data_string.split("timeToFirstByte\":")[1].split(",\"headerSize")[0]
navigationTime['headerSize'] = data_string.split("headerSize\":")[1].split(",\"dnsLookupTime")[0]
navigationTime['dnsLookupTime'] = data_string.split("dnsLookupTime\":")[1].split("},\"eventProperties",1)[0]
if 'networkInformation' in data_string:
networkInf['downlink'] = data_string.split("downlink\":")[1].split(",\"effectiveType")[0]
networkInf['effectiveType'] = data_string.split("effectiveType\":\"")[1].split("\",\"rtt")[0]
networkInf['rtt'] = data_string.split("rtt\":")[1].split(",\"saveData")[0]
networkInf['saveData'] = data_string.split("saveData\":")[1].split("},\"eventProperties",1)[0]
if 'storageEstimate' in data_string:
storageEstimate['quota'] = data_string.split("quota\":")[1].split(",\"usage")[0]
storageEstimate['usage'] = data_string.split("usage\":")[1].split(",\"caches")[0]
storageEstimate['caches'] = data_string.split("caches\":")[1].split(",\"indexedDB")[0]
storageEstimate['indexedDB'] = data_string.split("indexedDB\":")[1].split(",\"serviceWorker")[0]
storageEstimate['serviceWorker'] = data_string.split("serviceWorker\":")[1].split("},\"eventProperties",1)[0]
if 'fp' in data_string:
fpResult['fp'] = data_string.split("fp\",\"data\":")[1].split(",\"eventProperties",1)[0]
if 'fcp' in data_string:
fcpResult['fcp'] = data_string.split("fcp\",\"data\":")[1].split(",\"eventProperties",1)[0]
if 'fid' in data_string:
fidResult['fid'] = data_string.split("fid\",\"data\":")[1].split(",\"eventProperties",1)[0]
if 'lcp' in data_string:
lcpResult['lcp'] = data_string.split("lcp\",\"data\":")[1].split(",\"eventProperties",1)[0]
if 'cls' in data_string:
clsResult['cls'] = data_string.split("cls\",\"data\":")[1].split(",\"eventProperties",1)[0]
if 'tbt' in data_string:
tbtResult['tbt'] = data_string.split("tbt\",\"data\":")[1].split(",\"eventProperties",1)[0]
outputDir = "perfume-output"
if not os.path.exists(outputDir):
os.makedirs(outputDir)
else:
logging.getLogger(self.__class__.__name__).debug(f"The directory {outputDir} already exists")
try:
file2 = open(outputDir + '/MyFile2_results_{}.txt'.format(time.strftime('%Y.%m.%d_%H%M%S')),"w+")
file2.write(data_string)
with open(outputDir + '/navigationTiming_results_{}.csv'.format(time.strftime('%Y.%m.%d_%H%M%S')), 'w') as f: # Just use 'w' mode in 3.x 'w' mode in 3.x
w = csv.DictWriter(f, navigationTime.keys())
w.writeheader()
w.writerow(navigationTime)
with open(outputDir + '/networkInformation_results_{}.csv'.format(time.strftime('%Y.%m.%d_%H%M%S')), 'w') as f: # Just use 'w' mode in 3.x 'w' mode in 3.x
w = csv.DictWriter(f, networkInf.keys())
w.writeheader()
w.writerow(networkInf)
with open(outputDir + '/storageEstimate_results_{}.csv'.format(time.strftime('%Y.%m.%d_%H%M%S')), 'w') as f: # Just use 'w' mode in 3.x 'w' mode in 3.x
w = csv.DictWriter(f, storageEstimate.keys())
w.writeheader()
w.writerow(storageEstimate)
with open(outputDir + '/fp_results_{}.csv'.format(time.strftime('%Y.%m.%d_%H%M%S')), 'w') as f: # Just use 'w' mode in 3.x 'w' mode in 3.x
w = csv.DictWriter(f, fpResult.keys())
w.writeheader()
w.writerow(fpResult)
with open(outputDir + '/fcp_results_{}.csv'.format(time.strftime('%Y.%m.%d_%H%M%S')), 'w') as f: # Just use 'w' mode in 3.x 'w' mode in 3.x
w = csv.DictWriter(f, fcpResult.keys())
w.writeheader()
w.writerow(fcpResult)
with open(outputDir + '/fid_results_{}.csv'.format(time.strftime('%Y.%m.%d_%H%M%S')), 'w') as f: # Just use 'w' mode in 3.x 'w' mode in 3.x
w = csv.DictWriter(f, fidResult.keys())
w.writeheader()
w.writerow(fidResult)
with open(outputDir + '/lcp_results_{}.csv'.format(time.strftime('%Y.%m.%d_%H%M%S')), 'w') as f: # Just use 'w' mode in 3.x 'w' mode in 3.x
w = csv.DictWriter(f, lcpResult.keys())
w.writeheader()
w.writerow(lcpResult)
with open(outputDir + '/cls_results_{}.csv'.format(time.strftime('%Y.%m.%d_%H%M%S')), 'w') as f: # Just use 'w' mode in 3.x 'w' mode in 3.x
w = csv.DictWriter(f, clsResult.keys())
w.writeheader()
w.writerow(clsResult)
with open(outputDir + '/tbt_results_{}.csv'.format(time.strftime('%Y.%m.%d_%H%M%S')), 'w') as f: # Just use 'w' mode in 3.x 'w' mode in 3.x
w = csv.DictWriter(f, tbtResult.keys())
w.writeheader()
w.writerow(tbtResult)
except Exception as ex:
logging.getLogger(self.__class__.__name__).error('error: ' + str(ex))
self.wfile.write(b'')
def start_server():
"""Start the server."""
server_address = ('', 8080)
global httpd
httpd = http.server.HTTPServer(server_address, HTTPHandler)
logging.getLogger("PerfumeJS").info(f"Listening on all interfaces, on port 8080, and serving directory: {os.getcwd()}")
httpd.serve_forever()
def stop_server():
"""Stop the server."""
global httpd
logging.getLogger("PerfumeJS").info("Shutting down server")
httpd.server_close()
if __name__ == '__main__':
start_server()
stop_server()