diff options
| author | farhan <farhanalirazaazeemi@gmail.com> | 2025-12-13 23:33:33 +0500 |
|---|---|---|
| committer | Jacob Walls <jacobtylerwalls@gmail.com> | 2026-03-09 08:41:00 -0400 |
| commit | 12bb16da8fbadac34e2de318cc79d7d765f35a96 (patch) | |
| tree | e1ad54809d460e2139f1436492432072a8c45427 /tests/utils_tests | |
| parent | 787166fe27b0e7c7f97505da5766cfa72e76ae25 (diff) | |
Fixed #36293 -- Avoided buffering streaming responses in GZipMiddleware.
This avoids latency and/or blocking.
The example of streaming a CSV file was rewritten to employ batching for
greater efficiency in all layers (db, HTTP, etc.). The improved
performance from batching should outweigh the drag introduced by an
additional byte for each flush.
Co-authored-by: huoyinghui <huoyinghui@users.noreply.github.com>
Diffstat (limited to 'tests/utils_tests')
| -rw-r--r-- | tests/utils_tests/test_text.py | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/tests/utils_tests/test_text.py b/tests/utils_tests/test_text.py index 50e205a254..101943957c 100644 --- a/tests/utils_tests/test_text.py +++ b/tests/utils_tests/test_text.py @@ -1,3 +1,4 @@ +import gzip import json import sys @@ -404,13 +405,18 @@ class TestUtilsText(SimpleTestCase): text.get_valid_filename("$.$.$") def test_compress_sequence(self): - data = [{"key": i} for i in range(10)] - seq = list(json.JSONEncoder().iterencode(data)) - seq = [s.encode() for s in seq] - actual_length = len(b"".join(seq)) - out = text.compress_sequence(seq) - compressed_length = len(b"".join(out)) - self.assertLess(compressed_length, actual_length) + data = [{"key": i} for i in range(100)] + seq = [s.encode() for s in json.JSONEncoder().iterencode(data)] + original = b"".join(seq) + batch_size = 256 + batched_seq = ( + original[i : i + batch_size] for i in range(0, len(original), batch_size) + ) + compressed_chunks = list(text.compress_sequence(batched_seq)) + out = b"".join(compressed_chunks) + self.assertEqual(gzip.decompress(out), original) + self.assertLess(len(out), len(original)) + self.assertGreater(len(compressed_chunks), 2) def test_format_lazy(self): self.assertEqual("django/test", format_lazy("{}/{}", "django", lazystr("test"))) |
