(benchmark: BenchmarkFixture, case: BenchmarkCase)
| 223 | @pytest.mark.parametrize("case", CASES, ids=lambda case: case.id) |
| 224 | @pytest.mark.benchmark(max_time=0.5, max_rounds=10) |
| 225 | def test_gzip(benchmark: BenchmarkFixture, case: BenchmarkCase) -> None: |
| 226 | # Payload construction is intentionally outside the measured region. Cases |
| 227 | # are function-scoped, so only one source payload is resident at a time. |
| 228 | payload = make_payload(case.payload_kind, case.size) |
| 229 | messages: tuple[Message, ...] = ( |
| 230 | { |
| 231 | "type": "http.response.start", |
| 232 | "status": 200, |
| 233 | "headers": [(b"content-type", b"application/json"), (b"content-length", str(len(payload)).encode())], |
| 234 | }, |
| 235 | {"type": "http.response.body", "body": payload}, |
| 236 | ) |
| 237 | app = GZipMiddleware(StaticResponseApp(messages), minimum_size=0, compresslevel=case.level) |
| 238 | scope: Scope = {"type": "http", "headers": [(b"accept-encoding", b"gzip")]} |
| 239 | with closing(ASGIRunner()) as runner: |
| 240 | sent = benchmark.pedantic(runner.run, args=(app, scope), rounds=1) |
| 241 | |
| 242 | assert len(sent) == 2 |
| 243 | assert sent[0]["type"] == "http.response.start" |
| 244 | assert (b"content-encoding", b"gzip") in sent[0]["headers"] |
| 245 | assert sent[1]["type"] == "http.response.body" |
| 246 | compressed = sent[1]["body"] |
| 247 | assert isinstance(compressed, bytes) |
| 248 | |
| 249 | assert gzip.decompress(compressed) == payload |
| 250 | benchmark.extra_info["input_bytes"] = len(payload) |
| 251 | benchmark.extra_info["output_bytes"] = len(compressed) |
| 252 | benchmark.extra_info["compression_ratio"] = len(compressed) / len(payload) |
| 253 | |
| 254 | |
| 255 | @pytest.mark.benchmark(max_time=0.5, max_rounds=1) |
nothing calls this directly
no test coverage detected