Skip to content

Instantly share code, notes, and snippets.

@davidfischer
Created February 14, 2021 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidfischer/a6739beb9ff00e74e132c65ad502a25e to your computer and use it in GitHub Desktop.
Save davidfischer/a6739beb9ff00e74e132c65ad502a25e to your computer and use it in GitHub Desktop.
This shows that storage instance startup times for S3 are much higher than other backends. This is due to some connection resource setup that the S3boto backend does.
import random
import string
import timeit
from django.conf import settings
from django.core.files.storage import get_storage_class
iterations = 100
path = "pdfs/docs/latest/docs.pdf"
storage = get_storage_class(settings.RTD_BUILD_MEDIA_STORAGE)()
def test_const_only():
return get_storage_class(settings.RTD_BUILD_MEDIA_STORAGE)()
def test_url_only():
new_path = ''.join([random.choice(string.ascii_lowercase) for _ in range(30)])
return storage.url(new_path)
def test_const_plus_url():
storage = get_storage_class(settings.RTD_BUILD_MEDIA_STORAGE)()
return storage.url(path)
# ~sub 0.1ms per iteration
iter_time = timeit.timeit(test_const_only, number=iterations) / iterations
print(f"Constructor Only: {iter_time:.6f} seconds")
# ~1.5ms per iteration -- actually the first iteration is expensive and others are much faster
iter_time = timeit.timeit(test_url_only, number=iterations) / iterations
print(f"URL Calculation Only: {iter_time:.6f} seconds")
# ~50ms per iteration
iter_time = timeit.timeit(test_const_plus_url, number=iterations) / iterations
print(f"Constructor + URL: {iter_time:.6f} seconds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment