New Mode + Change func name

Updating function name + Adding request by request mode
This commit is contained in:
Alexis
2020-02-14 13:53:51 +01:00
committed by GitHub
parent ff75e0982f
commit cb8fbaba9b

View File

@@ -6,7 +6,7 @@ import threading
import time
class stressThread (threading.Thread):
class StressThread (threading.Thread):
def __init__(self, host, port, path, timeout, n):
threading.Thread.__init__(self)
self.host = host
@@ -32,40 +32,44 @@ class stressThread (threading.Thread):
except Exception as e:
print("Request {}: {}".format(self.n, e))
def isSucceeded(self):
def is_succeeded(self):
return self.success
def getTime(self):
def get_time(self):
return self.time
def start(host, port, path, timeout, threadnumber):
threadArray = []
for i in range(0, threadnumber):
threadArray.append(stressThread(host, port, path, timeout, i))
for t in threadArray:
def start(host, port, path, timeout, thread_number, one_by_one = False):
thread_array = []
for i in range(0, thread_number):
thread_array.append(StressThread(host, port, path, timeout, i))
for t in thread_array:
if one_by_one:
t.run()
else:
t.start()
for t in threadArray:
if not one_by_one:
for t in thread_array:
t.join()
showStat(threadArray, (timeout * 1000))
show_stat(thread_array, (timeout * 1000))
def showStat(tArray, timeoutInMs):
def show_stat(tArray, timeoutInMs):
total, succeeded = [0, 0]
Tmax, Tmin, Tavg = [0, timeoutInMs, 0]
for t in tArray:
total += 1
Tavg += t.getTime()
if t.getTime() > Tmax:
Tmax = t.getTime()
if t.getTime() < Tmin:
Tmin = t.getTime()
if t.isSucceeded():
Tavg += t.get_time()
if t.get_time() > Tmax:
Tmax = t.get_time()
if t.get_time() < Tmin:
Tmin = t.get_time()
if t.is_succeeded():
succeeded += 1
Tavg = round(Tavg / total)
print("====== FINISHED ======")
print("Failed : {}, Succeeded : {}, Total : {}".format((total - succeeded), succeeded, total))
print("Min : {} ms, Max : {} ms, Average : {} ms".format(Tmin, Tmax, Tavg))
def showHelp():
def show_help():
print("")
print("Usage: python http_stress_test.py -h host [-p port] -pth path [-t number_of_thread] [-tm timeout_in_second]")
print("Exemple: python http_stress_test.py -h www.google.fr -pth / -t 10")
@@ -76,8 +80,9 @@ def showHelp():
print(" -pth path The path of the HTTP resource")
print(" -t thread Number of threads")
print(" -tm second Timeout of the request")
print(" --one-by-one Send request one by one")
def getArgs(header, important, notfoundcode, valuenotfoundcode, default = ""):
def get_args(header, important, notfoundcode, valuenotfoundcode, default = ""):
if (header in sys.argv):
try:
i = sys.argv.index(header)
@@ -85,30 +90,34 @@ def getArgs(header, important, notfoundcode, valuenotfoundcode, default = ""):
return sys.argv[i + 1]
elif important:
print ("Error: argument {} found but not the value".format(header))
showHelp()
show_help()
sys.exit(valuenotfoundcode)
except ValueError:
print ("Error: argument {} not found".format(header))
if important:
showHelp()
show_help()
sys.exit(notfoundcode)
else:
if important:
print ("Error: argument {} not found".format(header))
showHelp()
show_help()
sys.exit(notfoundcode)
return default
def get_flag(header):
return (header in sys.argv)
if ((len(sys.argv) >= 2) and (("--help" in sys.argv) or ("/?" in sys.argv))):
print("Basic HTTP Stress test")
print("by Alexis Delhaie (@alexlegarnd)")
showHelp()
show_help()
sys.exit(0);
host = getArgs("-h", True, 1001, 1002)
port = int(getArgs("-p", False, 1003, 1004, "80"))
path = getArgs("-pth", True, 1005, 1006)
tnumber = int(getArgs("-t", False, 1007, 1008, "5"))
timeout = int(getArgs("-tm", False, 1009, 1010, "10"))
host = get_args("-h", True, 1001, 1002)
port = int(get_args("-p", False, 1003, 1004, "80"))
path = get_args("-pth", True, 1005, 1006)
thread_number = int(get_args("-t", False, 1007, 1008, "5"))
timeout = int(get_args("-tm", False, 1009, 1010, "10"))
one_by_one = get_flag("--one-by-one")
start(host, port, path, timeout, tnumber)
start(host, port, path, timeout, thread_number, one_by_one)