This repository has been archived by the owner on Feb 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 338
/
fetch-timeout.py
executable file
·162 lines (128 loc) · 4.02 KB
/
fetch-timeout.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
import json
import os.path
import random
import sys
from argparse import ArgumentParser
from collections import defaultdict
from contextlib import closing
from datetime import datetime
from multiprocessing.dummy import Pool as ParallelPool
from socket import AF_INET, IPPROTO_TCP, SOCK_STREAM, TCP_NODELAY, socket
from time import time
from io import open
if sys.version_info[0] == 2:
from urlparse import urlparse
str = unicode
else:
from urllib.parse import urlparse
def check_requirements():
def check_python_version():
if 0x2000000 <= sys.hexversion <= 0x2070000:
print('your "python" lower than 2.7.0 upgrade.')
return False
if 0x3000000 <= sys.hexversion <= 0x3040000:
print('your "python" lower than 3.4.0 upgrade.')
return False
return True
return check_python_version()
def request_with_socket(host, port, timeout):
with closing(socket(AF_INET, SOCK_STREAM)) as connection:
connection.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)
connection.settimeout(timeout)
connection.connect((host, port))
def timeit(callback):
begin_time = time()
callback()
end_time = time()
return end_time - begin_time
def request(target):
host, port, timeout = target
try:
rtt = timeit(lambda: request_with_socket(host, port, timeout))
return host, rtt * 1000
except:
return host, None
def fetch(payload, timeout, concurrent, testing_times):
if not payload:
return
def handle_ip(target):
address = urlparse('http://%s' % str(target))
return address.hostname, address.port or 80, timeout
def handle_ipset(ips):
ips *= testing_times
random.shuffle(ips)
return ips
with closing(ParallelPool(concurrent)) as pool:
for service_item in payload:
print(str(service_item['title']))
print(', '.join(service_item['domains']))
iptable = service_item['ips']
for name, ips in iptable.items():
print('\t%s' % name)
iptable[name] = defaultdict(list)
request_payload = map(handle_ip, handle_ipset(ips))
for ip, delta in pool.imap_unordered(request, request_payload):
iptable[name][ip].append(delta)
if delta:
print('\t\t%-15s\t%.3fms' % (ip, delta))
save_result(payload)
def load_payload(path):
if os.path.exists(path):
with open(path, encoding='UTF-8') as fp:
return json.loads(fp.read())
else:
print('"%s" file not found.' % path)
sys.exit(1)
def save_result(payload):
target_filename = 'apple-cdn-speed.report'
with open(target_filename, 'w', encoding='UTF-8') as fp:
report_data = json.dumps(
payload,
sort_keys=True,
indent=4,
ensure_ascii=False
)
fp.write(str(report_data))
def main():
parser = ArgumentParser()
parser.add_argument(
'payload',
type=str,
help='payload'
)
parser.add_argument(
'--timeout',
type=int,
help='timeout (default: %(default)s) (unit ms)',
dest='timeout',
default=400
)
parser.add_argument(
'--concurrent',
type=int,
help='concurrent (default: %(default)s)',
dest='concurrent',
default=10
)
parser.add_argument(
'--testing_times',
type=int,
help='testing times (default: %(default)s)',
dest='testing_times',
default=20
)
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
fetch(
load_payload(args.payload),
timeout=args.timeout / 1000.0,
concurrent=args.concurrent,
testing_times=args.testing_times
)
if __name__ == '__main__' and check_requirements():
main()