commit 3e75c5bd69414dc7b9d067cba197f7a6fc8537f7 Author: Alexis Delhaie Date: Thu Feb 6 19:29:05 2020 +0100 First commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..d943415 --- /dev/null +++ b/README.md @@ -0,0 +1,32 @@ +# Basic HTTP Stress test +This is a little script wrote in Python 3. It's just for simulating multiple connection in same time. +## How to use it + + 1. Install Python 3 + +Debian, Ubuntu +```sh +sudo apt install python3 +``` +Fedora +```sh +sudo dnf install python37 +``` +Redhat, CentOS +```sh +sudo yum install python3 +``` +Windows and macOS +You can download the installer on [here for Windows](www.python.org/downloads/windows/) and [here for macOS](https://www.python.org/downloads/mac-osx/) + +## Example and minimal arguments +```sh +python3 http_stress_test.py -h host [-p port] -pth path [-t number_of_thread] [-tm timeout_in_second] +``` +Your command must have -h (host IP or domain) and -pth (path to the resource) + +Example: +```sh +python3 http_stress_test.py -h www.google.fr -pth / -t 10 +``` + diff --git a/src/http_stress_test.py b/src/http_stress_test.py new file mode 100644 index 0000000..b6fce7a --- /dev/null +++ b/src/http_stress_test.py @@ -0,0 +1,114 @@ +#!/usr/bin/python3 + +import sys +import http.client +import threading +import time + + +class stressThread (threading.Thread): + def __init__(self, host, port, path, timeout, n): + threading.Thread.__init__(self) + self.host = host + self.port = port + self.path = path + self.timeout = timeout + self.n = n + self.success = False + self.time = 0 + + def run(self): + try: + print("Request {}: GET on {}".format(self.n, self.path)) + now = time.time() + c = http.client.HTTPConnection(self.host, self.port, timeout=self.timeout) + c.request("GET", self.path) + res = c.getresponse() + processed = round((time.time() - now) * 1000) + print("Request {}: {} {} (in {} ms)".format(self.n, res.status, res.reason, processed)) + self.time = processed + if res.status < 400: + self.success = True + except Exception as e: + print("Request {}: {}".format(self.n, e)) + + def isSucceeded(self): + return self.success + + def getTime(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: + t.start() + for t in threadArray: + t.join() + showStat(threadArray, (timeout * 1000)) + +def showStat(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(): + 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(): + 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") + print("") + print("Available arguments:") + print(" -h host The server IP or domain name") + print(" -p port The server HTTP port") + print(" -pth path The path of the HTTP resource") + print(" -t thread Number of threads") + print(" -tm second Timeout of the request") + +def getArgs(header, important, notfoundcode, valuenotfoundcode, default = ""): + if (header in sys.argv): + try: + i = sys.argv.index(header) + if (len(sys.argv) > (i + 1)): + return sys.argv[i + 1] + elif important: + print ("Error: argument {} found but not the value".format(header)) + showHelp() + sys.exit(valuenotfoundcode) + except ValueError: + print ("Error: argument {} not found".format(header)) + if important: + showHelp() + sys.exit(notfoundcode) + else: + if important: + print ("Error: argument {} not found".format(header)) + showHelp() + sys.exit(notfoundcode) + return default + +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() + 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")) + +start(host, port, path, timeout, tnumber) \ No newline at end of file