Klaus Demo bjoern / 2d34a6a
timeouts tests Ionut Negru authored 27 days ago Jonas Haag committed 25 days ago
2 changed file(s) with 113 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 import logging
1 import requests
2 import time
3 import timeit
4 import sys
5 import signal
6
7 sleep_time = 2
8 port = int(sys.argv[1]) if len(sys.argv) == 2 else 8081
9 host = f"http://localhost:{port}/"
10
11 print(f"Making requests on {host}")
12
13 s = requests.Session()
14 number_of_calls = 10
15 server_pids = {}
16
17 def shutdown(sig, frame):
18 logging.warning(f"Requests {server_pids}")
19 sys.exit()
20
21 signal.signal(signal.SIGINT, shutdown)
22
23 request_no = 0
24 error_request_no = 0
25 max_duration = 0
26 min_duration = 9999
27 errors = {}
28
29 def http_call():
30 global request_no
31 global error_request_no
32 r = s.get(host)
33 server_pids[r.text] = server_pids.get(r.text, 0) + 1
34 if r.status_code != 200:
35 error_request_no += 1
36 raise Exception(r)
37 else:
38 request_no += 1
39
40 while True:
41 t = timeit.timeit(http_call, number=number_of_calls)
42
43 max_duration = max(max_duration, t/number_of_calls)
44 min_duration = min(min_duration, t/number_of_calls)
45 sys.stdout.write("\r{0}".format(
46 f"r: {request_no:10}, er: {error_request_no:5}, min: {min_duration:2.6}s, max: {max_duration:2.6}s, pids {server_pids}"))
47 sys.stdout.flush()
0 #!/usr/bin/python
1
2 import os
3 import signal
4 import sys
5 import time
6 import bjoern
7
8 host = "0.0.0.0"
9 port = int(sys.argv[1]) if len(sys.argv) == 2 else 8081
10 workers = 3
11
12 worker_pids = []
13
14 socket = bjoern.bind_and_listen(host=host, port=port, listen_backlog=1)
15
16 requests_count = 0
17
18 print(f"http://{host}:{port} with {workers} workers")
19
20 parent_pid = os.getpid()
21
22 def shutdown(sig, frame):
23 print("Wait for children to exit")
24 for child_pid in worker_pids:
25 print(f"killing child with pid {child_pid}.")
26 os.kill(child_pid, signal.SIGINT)
27
28 def application(environ, start_response):
29 global requests_count
30 requests_count += 1
31 status = '200 OK'
32 output = bytes(str(os.getpid()), "utf-8")
33 response_headers = [('Content-type', 'text/plain'),
34 ('Content-Length', str(len(output)))]
35 start_response(status, response_headers)
36 return [output]
37
38 for i in range(workers):
39 pid = os.fork()
40
41 if pid > 0:
42 worker_pids.append(pid)
43 elif pid == 0:
44 worker_pids = []
45 # Run HTTP server
46 print(f"Start listening for {os.getpid()}")
47 try:
48 bjoern.server_run(socket, application)
49 except KeyboardInterrupt as _:
50 print(f"{os.getpid()}: {requests_count}")
51 sys.exit()
52
53 if parent_pid == os.getpid():
54 signal.signal(signal.SIGINT, shutdown)
55
56 while len(worker_pids) > 0:
57 child_pid, exit_status = os.wait()
58 if child_pid in worker_pids:
59 worker_pids.remove(child_pid)
60 else:
61 print(f"Child with pid {child_pid} not found in parent's process hash.")
62 print(f"Child with pid {child_pid} exited")
63
64 sys.exit()