add headers
This commit is contained in:
50
src/main.py
50
src/main.py
@@ -8,11 +8,12 @@ from rich import box
|
|||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
from rich.table import Table
|
from rich.table import Table
|
||||||
from thread import StressThread
|
from thread import StressThread
|
||||||
|
from parameters import Options
|
||||||
import thread
|
import thread
|
||||||
import basic_producer_consumer
|
import basic_producer_consumer
|
||||||
|
|
||||||
console = Console(highlight=False)
|
console = Console(highlight=False)
|
||||||
VERSION = "1.3.2"
|
VERSION = "1.4"
|
||||||
thread.console = console
|
thread.console = console
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -24,33 +25,35 @@ def main():
|
|||||||
show_version()
|
show_version()
|
||||||
return
|
return
|
||||||
|
|
||||||
host = get_args("-h", True, 1001, 1002)
|
options = Options()
|
||||||
allow_ssl = get_flag("--ssl")
|
options.host = get_args("-h", True, 1001, 1002)
|
||||||
port = int(get_args("--port", False, 1003, 1004, "80"))
|
options.allow_ssl = get_flag("--ssl")
|
||||||
path = get_args("-p", True, 1005, 1006)
|
options.port = int(get_args("--port", False, 1003, 1004, "80"))
|
||||||
thread_number = int(get_args("-t", False, 1007, 1008, "5"))
|
options.path = get_args("-p", False, 1005, 1006, "/")
|
||||||
timeout = int(get_args("-tm", False, 1009, 1010, "10"))
|
options.thread_number = int(get_args("-t", False, 1007, 1008, "5"))
|
||||||
one_by_one = get_flag("--one-by-one")
|
options.timeout = int(get_args("-tm", False, 1009, 1010, "10"))
|
||||||
ignore_available_threads = get_flag("--ignore-available-threads")
|
options.one_by_one = get_flag("--one-by-one")
|
||||||
self_signed = get_flag("--allow-self-signed")
|
options.ignore_available_threads = get_flag("--ignore-available-threads")
|
||||||
|
options.self_signed = get_flag("--allow-self-signed")
|
||||||
|
options.headers = get_headers()
|
||||||
|
|
||||||
if one_by_one and ignore_available_threads:
|
if options.one_by_one and options.ignore_available_threads:
|
||||||
console.print("[red]Error[/]: ambigous arguments, --one-by-one and --ignore-available-threads cannot be in the same command", style="bold")
|
console.print("[red]Error[/]: ambigous arguments, --one-by-one and --ignore-available-threads cannot be in the same command", style="bold")
|
||||||
return
|
return
|
||||||
|
|
||||||
start(host, port, path, timeout, thread_number, allow_ssl, self_signed, one_by_one, ignore_available_threads)
|
start(options)
|
||||||
|
|
||||||
def start(host, port, path, timeout, thread_number, allow_ssl, self_signed, one_by_one = False, ignore_available_threads = False):
|
def start(options):
|
||||||
thread_array = []
|
thread_array = []
|
||||||
for i in range(0, thread_number):
|
for i in range(0, options.thread_number):
|
||||||
thread_array.append(StressThread(host, port, path, timeout, i, allow_ssl, self_signed, VERSION))
|
thread_array.append(StressThread(options, i, VERSION))
|
||||||
if one_by_one:
|
if options.one_by_one:
|
||||||
start_one_by_one(thread_array)
|
start_one_by_one(thread_array)
|
||||||
elif ignore_available_threads:
|
elif options.ignore_available_threads:
|
||||||
start_all(thread_array)
|
start_all(thread_array)
|
||||||
else:
|
else:
|
||||||
basic_producer_consumer.start(thread_array)
|
basic_producer_consumer.start(thread_array)
|
||||||
show_stat(thread_array, (timeout * 1000))
|
show_stat(thread_array, (options.timeout * 1000))
|
||||||
|
|
||||||
# Run requests in one thread
|
# Run requests in one thread
|
||||||
def start_one_by_one(threads):
|
def start_one_by_one(threads):
|
||||||
@@ -113,6 +116,7 @@ def show_help():
|
|||||||
console.print(" --ignore-available-threads Ignore physical number of threads and start all request in the same time")
|
console.print(" --ignore-available-threads Ignore physical number of threads and start all request in the same time")
|
||||||
console.print(" --ssl Use HTTPS/SSL")
|
console.print(" --ssl Use HTTPS/SSL")
|
||||||
console.print(" --allow-self-signed Allow self signed SSL certificate")
|
console.print(" --allow-self-signed Allow self signed SSL certificate")
|
||||||
|
console.print(" --header key=value Send a custom header (To add several headers, add several times the argument --header)")
|
||||||
console.print(" --help")
|
console.print(" --help")
|
||||||
console.print(" /? Show this page")
|
console.print(" /? Show this page")
|
||||||
console.print(" --version Get information about the application and the system")
|
console.print(" --version Get information about the application and the system")
|
||||||
@@ -150,6 +154,16 @@ def get_args(header, important, notfoundcode, valuenotfoundcode, default = ""):
|
|||||||
sys.exit(notfoundcode)
|
sys.exit(notfoundcode)
|
||||||
return default
|
return default
|
||||||
|
|
||||||
|
def get_headers():
|
||||||
|
headers = {}
|
||||||
|
indices = [i for i, x in enumerate(sys.argv) if x == "--header"]
|
||||||
|
for i in indices:
|
||||||
|
if (len(sys.argv) > (i + 1)):
|
||||||
|
h_raw = sys.argv[i + 1]
|
||||||
|
key, value = h_raw.split('=', 1)
|
||||||
|
headers[key] = value
|
||||||
|
return headers
|
||||||
|
|
||||||
def get_flag(header):
|
def get_flag(header):
|
||||||
return (header in sys.argv)
|
return (header in sys.argv)
|
||||||
|
|
||||||
|
|||||||
17
src/parameters.py
Normal file
17
src/parameters.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
|
||||||
|
|
||||||
|
class Options:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.host = ""
|
||||||
|
self.allow_ssl = False
|
||||||
|
self.port = 80
|
||||||
|
self.path = "/"
|
||||||
|
self.thread_number = 5
|
||||||
|
self.timeout = 10
|
||||||
|
self.one_by_one = False
|
||||||
|
self.ignore_available_threads = False
|
||||||
|
self.self_signed = False
|
||||||
|
self.headers = {}
|
||||||
|
|
||||||
|
|
||||||
@@ -11,16 +11,17 @@ console = None
|
|||||||
|
|
||||||
class StressThread (threading.Thread):
|
class StressThread (threading.Thread):
|
||||||
|
|
||||||
def __init__(self, host, port, path, timeout, n, allow_ssl, self_signed, version):
|
def __init__(self, options, n, version):
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
self.user_agent = "PyStressTest/{}({} {} {})".format(version, platform.system(), os.name, platform.release())
|
self.user_agent = "PyStressTest/{}({} {} {})".format(version, platform.system(), os.name, platform.release())
|
||||||
self.host = host
|
self.host = options.host
|
||||||
self.port = port
|
self.port = options.port
|
||||||
self.path = path
|
self.path = options.path
|
||||||
self.timeout = timeout
|
self.timeout = options.timeout
|
||||||
self.n = n
|
self.n = n
|
||||||
self.allow_ssl = allow_ssl
|
self.allow_ssl = options.allow_ssl
|
||||||
self.self_signed = self_signed
|
self.self_signed = options.self_signed
|
||||||
|
self.headers = options.headers
|
||||||
self.success = False
|
self.success = False
|
||||||
self.time = 0
|
self.time = 0
|
||||||
|
|
||||||
@@ -35,8 +36,8 @@ class StressThread (threading.Thread):
|
|||||||
c = http.client.HTTPSConnection(self.host, self.port, timeout=self.timeout, key_file=None, cert_file=None)
|
c = http.client.HTTPSConnection(self.host, self.port, timeout=self.timeout, key_file=None, cert_file=None)
|
||||||
else:
|
else:
|
||||||
c = http.client.HTTPConnection(self.host, self.port, timeout=self.timeout)
|
c = http.client.HTTPConnection(self.host, self.port, timeout=self.timeout)
|
||||||
headers = {"User-Agent": self.user_agent}
|
self.headers["User-Agent"] = self.user_agent
|
||||||
c.request(method="GET", url=self.path, headers=headers)
|
c.request(method="GET", url=self.path, headers=self.headers)
|
||||||
res = c.getresponse()
|
res = c.getresponse()
|
||||||
processed = round((time.time() - now) * 1000)
|
processed = round((time.time() - now) * 1000)
|
||||||
self.print_result(res.status, res.reason, processed)
|
self.print_result(res.status, res.reason, processed)
|
||||||
|
|||||||
Reference in New Issue
Block a user