Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make brotli configurable by environment variable #117

Merged
merged 3 commits into from
Jan 4, 2023

Conversation

ddelange
Copy link
Contributor

@ddelange ddelange commented Dec 9, 2022

This PR allows configuring compression level and compression mode via environment variables.

  • Django's default gzip compression level is 6 out of 9.
  • Brotli's default compression level is 11 out of 11.
  • This PR sets django-brotli's default to 4 out of 11.

For a response with nearly 2MB uncompressed json payload containing many lists and dicts of english words (broti's strength), integers, and floats, a brotly quality setting of 5 or lower is needed avoid being slower than the gzip middleware (and still achieve 20% better compression ratio).

Setting the default quality to 4 makes brotli twice as fast as gzip@6, and still yields 20% better compression.

Benchmark:

>>> payload = open('./payload.json', 'rb').read()
>>> len(payload)
1759385
>>> import gzip, brotli
>>> len(gzip.compress(payload, 6))
516307
>>> len(brotli.compress(payload, quality=11))
328070
>>> len(brotli.compress(payload, quality=10))
336622
>>> len(brotli.compress(payload, quality=9))
361724
>>> len(brotli.compress(payload, quality=8))
366235
>>> len(brotli.compress(payload, quality=7))
371714
>>> len(brotli.compress(payload, quality=6))
381026
>>> len(brotli.compress(payload, quality=5))
394309
>>> len(brotli.compress(payload, quality=4))
419936
>>> len(brotli.compress(payload, quality=3))
483480
>>> len(brotli.compress(payload, quality=2))
499352
>>> %timeit gzip.compress(payload, 6)
77 ms ± 1.07 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
>>> %timeit brotli.compress(payload, quality=11)
4.16 s ± 137 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
>>> %timeit brotli.compress(payload, quality=10)
1.74 s ± 74.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
>>> %timeit brotli.compress(payload, quality=9)
178 ms ± 15.5 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
>>> %timeit brotli.compress(payload, quality=8)
134 ms ± 9.4 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
>>> %timeit brotli.compress(payload, quality=7)
111 ms ± 7.22 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
>>> %timeit brotli.compress(payload, quality=6)
80.9 ms ± 2.43 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
>>> %timeit brotli.compress(payload, quality=5)
74.9 ms ± 1.23 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
>>> %timeit brotli.compress(payload, quality=4)
38.2 ms ± 431 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
>>> %timeit brotli.compress(payload, quality=3)
24.6 ms ± 309 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
>>> %timeit brotli.compress(payload, quality=2)
21.3 ms ± 773 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

@ddelange
Copy link
Contributor Author

ddelange commented Jan 1, 2023

Hi @illagrenan 👋 could you have a look if you have a minute?

@illagrenan
Copy link
Owner

Thanks for your pull request, it looks good :-)

@illagrenan illagrenan merged commit 51a9d8c into illagrenan:master Jan 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants