MCPcopy Create free account
hub / github.com/encode/starlette / test_gzip

Function test_gzip

benchmarks/gzip_benchmark.py:225–252  ·  view source on GitHub ↗
(benchmark: BenchmarkFixture, case: BenchmarkCase)

Source from the content-addressed store, hash-verified

223@pytest.mark.parametrize("case", CASES, ids=lambda case: case.id)
224@pytest.mark.benchmark(max_time=0.5, max_rounds=10)
225def 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)

Callers

nothing calls this directly

Calls 4

GZipMiddlewareClass · 0.90
ASGIRunnerClass · 0.90
make_payloadFunction · 0.85
StaticResponseAppClass · 0.85

Tested by

no test coverage detected