-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpywbt.py
executable file
·165 lines (125 loc) · 5.35 KB
/
pywbt.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
163
164
#!/usr/bin/python
"""
Python Web Benchark Tool
Example usage:
debian:~/$ python pywbt.py -n 100 -c 30 http://google.com
> Requests between 0 and 30 sent
> Requests between 30 and 60 sent
> Requests between 60 and 90 sent
> Requests between 90 and 100 sent
-------------------------------------------------
Process took 0.990706205368 seconds ( 0.00990706205368 per/request)
Number of succeeded requests 100
Number of failed requests 0
Osman Yuksel <yuxel |ET| sonsuzdongu (DOT).com
"""
__appName__ = "Python Web Benchark Tool"
__version__ = '0.0.1'
import sys
import time, math
from threading import Thread
from optparse import OptionParser
if (sys.version_info > (3, 0)):
from urllib.parse import urlparse
import http.client as httplib
else:
from urlparse import urlparse
import httplib
class PyWBT:
""" parse options and run threads """
""" TODO: split into some methods """
def __init__(self):
options, args, urlData = self.parseOptions()
numOfRequestBlocks = float(options.numberOfRequests) / float(options.concurentRequestCount)
""" FIXME : its too complicated here, need to be simplified """
requestBlockLimit = int(math.ceil( numOfRequestBlocks ))
numOfRequestBlocks = int(numOfRequestBlocks)
numOfRequestsForLastBlock = (options.numberOfRequests % options.concurentRequestCount)
startTime = time.time()
requestSucceed = 0
requestFailed = 0
threadList = {}
for requestBlock in range(0, requestBlockLimit ):
threadList[requestBlock] = []
concurentRequestCount = options.concurentRequestCount
if requestBlock == numOfRequestBlocks :
concurentRequestCount = numOfRequestsForLastBlock
for count in range(0,concurentRequestCount):
currentThread = PyWBThread(count, urlData)
threadList[requestBlock].append(currentThread)
currentThread.start()
# join threads
for thread in threadList[requestBlock]:
try:
thread.join()
if thread.completed :
requestSucceed = requestSucceed + 1
else:
requestFailed = requestFailed + 1
except Exception:
print(sys.exc_info())
startFrom = requestBlock * options.concurentRequestCount
endTo = startFrom + concurentRequestCount
print(" > Requests between ", startFrom, "and", endTo ," sent")
# end time tracker
endTime = time.time()
""" print results """
totalRequstTime = endTime - startTime
perRequst = totalRequstTime / options.numberOfRequests
print(" ------------------------------------------------- ")
print("Process took ", totalRequstTime , "seconds (", perRequst, " per/request)")
print("Number of succeeded requests " , requestSucceed)
print("Number of failed requests " , requestFailed)
""" print usage and help """
def parseOptions(self):
usage = "usage: %prog -n numberOfRequests -c concurentRequestCount http://URL"
parser = OptionParser(usage=usage, version=__appName__+" "+__version__)
parser.add_option("-n", "--numberOfRequests", action="store", type="int",
default=1, dest="numberOfRequests", help="input number of requests to URL")
parser.add_option("-c", "--concurentRequestCount", action="store", type="int",
default=1, dest="concurentRequestCount", help="input number of concurent requests")
(options, args) = parser.parse_args()
""" only one url """
if len(args) != 1:
parser.error('You have to specify a URL')
else:
wrong_url = 'You have to specify valid URL starts with http:// or https://'
""" check if url is valid
TODO: bad """
url = urlparse(args[0])
if url.scheme not in ["https", "http"]:
parser.error(wrong_url)
elif len(url.netloc) < 1:
parser.error(wrong_url)
""" get url and query string """
queryString = None
if url.path or url.params:
queryString = url.path + "?" + url.params
urlData = {
"host": url.netloc,
"queryString" : queryString,
"protocol": url.scheme
}
return options, args, urlData
""" Threads to send requests """
class PyWBThread(Thread):
def __init__ (self,count, urlData):
Thread.__init__(self)
self.host = urlData['host']
self.queryString = urlData['queryString']
self.protocol = urlData['protocol']
self.count = count
def run(self):
try:
if self.protocol == 'http':
connection = httplib.HTTPConnection(self.host)
else:
connection = httplib.HTTPSConnection(self.host)
connection.request("GET", self.queryString)
response = connection.getresponse()
connection.close()
self.completed = True
except Exception:
self.completed = False
if __name__ == '__main__':
PyWBT()