Skip to content

Commit

Permalink
chore: PRT - add request counter for debugging the populator
Browse files Browse the repository at this point in the history
  • Loading branch information
ranlavanet committed Jan 12, 2025
1 parent bac5c8e commit f9a9d62
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions ecosystem/cache_populator/scripts/request_counter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import argparse
from http.server import BaseHTTPRequestHandler, HTTPServer
import time

# Global variables to keep track of requests and timing
request_count = 0
start_time = None

class RequestCounterHandler(BaseHTTPRequestHandler):
# Suppress default server log messages
def log_message(self, format, *args):
return

def _handle_request(self, request_type):
global request_count, start_time

if start_time is None:
start_time = time.time()

request_count += 1
current_time = time.time()
elapsed_time = current_time - start_time
requests_per_second = request_count / elapsed_time if elapsed_time > 0 else 0

# Unified single line output
print(f"{request_type} requests: {request_count}, Rate: {requests_per_second:.2f} req/s")

self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes(f"Number of requests: {request_count}\nRequests per second: {requests_per_second:.2f}", "utf8"))

def do_GET(self):
self._handle_request("GET")

def do_POST(self):
self._handle_request("POST")

def run(server_class=HTTPServer, handler_class=RequestCounterHandler, port=8080):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print(f'Starting httpd server on port {port}')
httpd.serve_forever()

if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Request Counter Server')
parser.add_argument('--port', type=int, default=8080, help='Port to listen on')
args = parser.parse_args()
run(port=args.port)

0 comments on commit f9a9d62

Please sign in to comment.