#!/usr/bin/env python3 """Time the S3 calls the no-follow resolution adds syscalls to. S3_ENDPOINT=... S3_KEY=... S3_SECRET=... S3_REGION=... S3REQ=.../s3req.py \\ bench.py PUTs 1 KiB objects at bench/a/b/c/obj- (four directories deep), then GETs and HEADs each, then lists the prefix 10 times (ListObjectsV2, 1000 keys a page), then DELETEs them. Prints the median and the 90th percentile of each call in milliseconds, one line per call. One connection per request, as s3req.py makes them. Python standard library only. """ import http.client import importlib.util import os import statistics import sys import time import urllib.parse def main(argv): if len(argv) != 2: sys.exit(__doc__) bucket, count = argv[0], int(argv[1]) spec = importlib.util.spec_from_file_location("s3req", os.environ["S3REQ"]) s3req = importlib.util.module_from_spec(spec) spec.loader.exec_module(s3req) endpoint = urllib.parse.urlsplit(os.environ["S3_ENDPOINT"]) key, secret = os.environ["S3_KEY"], os.environ["S3_SECRET"] region = os.environ.get("S3_REGION", "us-east-1") def request(method, path, query="", body=b""): headers = {} s3req.sign(method, endpoint.netloc, path, query, headers, body, key, secret, region) conn = http.client.HTTPConnection(endpoint.hostname, endpoint.port, timeout=30) target = s3req.quote(path, safe="/") + ("?" + query if query else "") t0 = time.perf_counter() conn.request(method, target, body=body or None, headers=headers) resp = conn.getresponse() resp.read() dt = (time.perf_counter() - t0) * 1000 conn.close() if resp.status >= 300: sys.exit(f"{method} {path}?{query}: {resp.status}") return dt body = os.urandom(1024) keys = [f"/{bucket}/bench/a/b/c/obj-{n:05d}" for n in range(count)] times = {} for name, fn in [ ("PUT", lambda k: request("PUT", k, body=body)), ("GET", lambda k: request("GET", k)), ("HEAD", lambda k: request("HEAD", k)), ]: times[name] = [fn(k) for k in keys] times["LIST"] = [request("GET", f"/{bucket}", f"list-type=2&max-keys=1000&prefix=bench/") for _ in range(10)] times["DELETE"] = [request("DELETE", k) for k in keys] for name, ts in times.items(): ts = sorted(ts) p90 = ts[min(len(ts) - 1, int(len(ts) * 0.9))] print(f"{name:6} n={len(ts):5} median {statistics.median(ts):7.2f} ms p90 {p90:7.2f} ms") if __name__ == "__main__": main(sys.argv[1:])