-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmark.py
More file actions
103 lines (85 loc) · 3.03 KB
/
Copy pathbenchmark.py
File metadata and controls
103 lines (85 loc) · 3.03 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
import gevent.monkey
gevent.monkey.patch_all()
import collections
import datetime
import gevent
import httplib
import optparse
SERVER = 'localhost:8000'
Halt = False
counters = collections.defaultdict(int)
class ClientBase(gevent.Greenlet):
def __init__(self):
gevent.Greenlet.__init__(self)
self.conn = httplib.HTTPConnection(SERVER)
def _run(self):
while not Halt:
self.act()
gevent.sleep(0)
def act(self):
raise NotImplementedError
class Producer(ClientBase):
def act(self):
self.conn.request('POST', '/queue/foo/',
body='{"do":"something"}',
headers={'content-type': 'application/json',
'x-httpqueue-priority': datetime.datetime.utcnow().isoformat(),
})
resp = self.conn.getresponse()
resp.read() # Discard the body
if resp.status not in (200, 204):
print "%s failed with %s" % (type(self).__name__, resp.status)
else:
counters['produced'] += 1
class Consumer(ClientBase):
def act(self):
self.conn.request('POP', '/queue/foo/')
resp = self.conn.getresponse()
resp.read() # Discard the body
id = resp.getheader('x-httpqueue-id')
if resp.status == 200:
counters['consumed'] += 1
elif resp.status == 204:
counters['no-content'] += 1
return
else:
print "%s (POP) failed with %s" % (type(self).__name__, resp.status)
return
self.conn.request('ACK', '/queue/foo/id/%s' % id)
resp = self.conn.getresponse()
resp.read() # Discard the body
if resp.status in (200, 204):
counters['acked'] += 1
else:
print "%s (ACK) failed with %s" % (type(self).__name__, resp.status)
parser = optparse.OptionParser(description='Run performance testing on httPQueue')
parser.add_option('--producers', '-p', type=int, help='Number of producer threads to run', default=5)
parser.add_option('--consumers', '-c', type=int, help='Number of consume/ack threads to run', default=10)
parser.add_option('--duration', '-d', type=int, default=None,
help='Number of seconds to run the test for (default is to run indefinitely')
options, args = parser.parse_args()
start = datetime.datetime.now()
actors = set()
for i in range(options.producers):
a = Producer()
actors.add(a)
a.start()
for i in range(options.consumers):
a = Consumer()
actors.add(a)
a.start()
try:
if options.duration is None:
gevent.joinall(actors)
else:
gevent.sleep(options.duration)
Halt = True
except KeyboardInterrupt:
Halt = True
gevent.joinall(actors)
period = datetime.datetime.now() - start
print '\nSpent %s; %s seconds' % (period, period.seconds)
print 'producers:', options.producers
print 'consumers:', options.consumers
for k, v in counters.items():
print '%s = %s; %s per second' % (k, v, v/float(period.seconds))