forked from zihuaye/3xsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpt.py
97 lines (83 loc) · 2.39 KB
/
pt.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
#!/usr/bin/env python
#
# A bench program to test HTTP performance, supporting pipelining request
#
import getopt, sys, time, select, threading
import _socket as socket
r = 'GET / HTTP/1.1\r\nHost: vm0:8000\r\nConnection: keep-alive\r\n\r\n'
request = b''
_n = 0
lock = threading.Lock()
def new_sock(addr,port):
sock = socket.socket()
sock.connect((addr, port))
return sock
def dp(sock, multi, pipeline):
global r, _n, lock
x=0
for j in xrange(multi):
sock.send(request)
while 1:
b = sock.recv(32768)
_bn = b.count('200 OK')
if _bn > 0:
with lock:
_n += _bn
x += _bn
if x >= pipeline:
x=0
break
if __name__ == '__main__':
port = 8000
n=1000
concurrent = 100
pipeline=1
addr='localhost'
opts, args = getopt.getopt(sys.argv[1:], "hp:c:n:o:l:")
for o, a in opts:
if o == "-p":
pipeline = int(a)
elif o == "-c":
concurrent = int(a)
elif o == "-n":
n = int(a)
elif o == "-o":
port = int(a)
elif o == "-l":
addr = a
elif o == "-h":
print "Usage: pt.py <options>"
print ""
print "Options:"
print "-p <num> : pipeline requests to make per request(default 1)"
print "-c <num> : concurrent connections(default 100)"
print "-n <num> : total requests to perform(default 1000)"
print "-o <num> : http server port number(default 8000)"
print "-l <addr> : http server ip to connect(default localhost)"
print "-h : this help information"
sys.exit(0)
multi=int((n/concurrent)/pipeline)
for i in xrange(pipeline):
request = ''.join([request, r])
ts = []
for x in xrange(concurrent):
ts.append(threading.Thread(target=dp, args=(new_sock(addr, port),multi,pipeline)))
start = time.time()
for x in xrange(concurrent):
ts[x].start()
for x in xrange(concurrent):
ts[x].join()
end = time.time()
t = end - start
print "HTTP bench testing host", addr, "port", str(port)
print "----------------------"
print "Concurrent:", concurrent
print "Pipelining:", pipeline
print "Requests/Conn:", int(n/concurrent)
print "----------------------"
print "Sent requests:", n
print "Get responses:", _n
print "----------------------"
print "Timing:", t, "secs,", t/n, "secs/r"
print "Estimate:", int(n/t), "reqs/s"
print ""