This repository has been archived by the owner on Feb 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tester.py
executable file
·89 lines (61 loc) · 2.18 KB
/
tester.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
#!/usr/bin/env python3
import argparse
from collections import Counter
from twisted.internet import epollreactor
# Should do this before any connections,
# listeners or connectors are added
epollreactor.install()
import treq
from twisted.internet import reactor, task
from twisted.web.client import HTTPConnectionPool
pool = HTTPConnectionPool(reactor)
cooperator = task.Cooperator()
class Tester:
def __init__(self, url):
self._stat = Counter(generated=0, sent=0, received=0)
self.url = url
@property
def stat(self):
return self._stat
def body_received(self, body):
self._stat['received'] += 1
def request_sent(self, response):
self._stat['sent'] += 1
d = treq.text_content(response)
d.addCallback(self.body_received)
d.addErrback(lambda x: None) # ignore errors
return d
def request(self):
"""Make a GET request"""
d = treq.get(self.url, pool=pool)
d.addCallback(self.request_sent)
d.addErrback(lambda x: None) # ignore errors
return d
def generate_requests(self):
"""Generate tens of thousands of requests"""
while True:
self.request()
self._stat['generated'] += 1
# do not yield deferred here so cooperator won't pause until
# response is received
yield None
def start(self):
"""Make cooperator work on spawning requests"""
iterator = self.generate_requests()
cooperator.cooperate(iterator)
def show_progress(stat):
"""Print the progress at one second intervals"""
print(', '.join('{} {}'.format(k, v) for k, v in stat.items()))
reactor.callLater(1, show_progress, stat)
if __name__ == '__main__':
# parse args from command-line interface
parser = argparse.ArgumentParser()
parser.add_argument('--url', type=str, help='A custom URL to bombard')
args = parser.parse_args()
# start a long-running cooperative task
tester = Tester(args.url)
tester.start()
# run the counter that will be reporting sending speed once a second
reactor.callLater(1, show_progress, tester.stat)
# run the reactor
reactor.run()