"""Local fixed-arrival-rate HTTP experiment; synthetic downstream delay."""
from concurrent.futures import ThreadPoolExecutor
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import json
import math
import os
import platform
import threading
import time
import tracemalloc
from urllib.error import HTTPError
from urllib.request import urlopen


def percentile(values, quantile):
    if not values:
        return None
    return sorted(values)[max(0, math.ceil(len(values) * quantile) - 1)]


def run(rate, delay, duration=0.4):
    slots = threading.BoundedSemaphore(2)
    class Handler(BaseHTTPRequestHandler):
        def log_message(self, *args):
            pass
        def do_GET(self):
            if not slots.acquire(blocking=False):
                self.send_error(503, "capacity exhausted")
                return
            try:
                time.sleep(delay)  # Simulated downstream; not a DB benchmark.
                body = b'{"count": 2}'
                self.send_response(200)
                self.send_header("Content-Length", str(len(body)))
                self.end_headers()
                self.wfile.write(body)
            finally:
                slots.release()
    server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
    server.daemon_threads = False
    serving = threading.Thread(target=server.serve_forever, kwargs={"poll_interval": .01})
    serving.start()
    samples, rejected = [], 0
    tracemalloc.start()
    cpu_started = time.process_time()
    start = time.perf_counter()
    def request(scheduled):
        sent = time.perf_counter()
        try:
            with urlopen(f"http://127.0.0.1:{server.server_port}/counts", timeout=2) as response:
                assert json.load(response) == {"count": 2}
                status = response.status
        except HTTPError as error:
            status = error.code
        except (OSError, TimeoutError):
            status = 0
        ended = time.perf_counter()
        return status, ended-scheduled, ended-sent, sent-scheduled
    try:
        with ThreadPoolExecutor(max_workers=8) as clients:
            pending = []
            for index in range(int(rate * duration)):
                scheduled = start + index / rate
                time.sleep(max(0, scheduled-time.perf_counter()))
                pending = [future for future in pending if not future.done()]
                if len(pending) >= 16:  # Bounded client submission, record shed arrivals.
                    rejected += 1
                    continue
                future = clients.submit(request, scheduled)
                pending.append(future)
                samples.append(future)
            records = [future.result(timeout=3) for future in samples]
        elapsed = time.perf_counter()-start
        successes = [record[1] for record in records if record[0] == 200]
        _, peak = tracemalloc.get_traced_memory()
        return {"rate": rate, "delay_s": delay, "planned": int(rate*duration),
                "observed": len(records), "client_shed": rejected,
                "errors": sum(row[0] != 200 for row in records),
                "success_rps_whole_run": len(successes)/elapsed,
                "p50_s": percentile(successes, .5), "p95_s": percentile(successes, .95),
                "p99_s": percentile(successes, .99), "raw": records,
                "whole_process_cpu_s": time.process_time()-cpu_started,
                "peak_python_traced_bytes": peak}
    finally:
        tracemalloc.stop()
        server.shutdown(); server.server_close(); serving.join(timeout=3)
        assert not serving.is_alive()


def main():
    print(json.dumps({"python": platform.python_version(), "os": platform.platform(),
                      "processor": platform.processor(), "logical_cpus": os.cpu_count()}, ensure_ascii=False))
    for delay in (.02, .005):
        for rate in (40, 160):
            for _ in range(2):
                print(json.dumps(run(rate, delay)))


if __name__ == "__main__":
    main()
