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

Added database cache benchmark #81

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Empty file.
73 changes: 73 additions & 0 deletions benchmarks/cache_benchmarks/database_cache/benchmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import random

from django.core.cache import caches
from django.core.management import call_command

from ...utils import bench_setup


class DatabaseCacheBackend:
Hisham-Pak marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we could add coverage for the some of the other backends too? The benchmarks would be the same, it's just a different setting? 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this in separate pull request?

def setup(self):
bench_setup()
call_command("createcachetable", verbosity=0)
random.seed(0)

self.cache = caches["db"]
self.iterations = 100
self.int_key = "int_key"
self.cache.set(self.int_key, 0)
self.keys = [
"key_{}".format(random.randint(1, 500)) for _ in range(self.iterations)
]
self.values = [
random.randint(1, 1024**1) * bytes([random.randint(0, 255)])
for _ in range(self.iterations)
]

def time_add(self):
for key, value in zip(self.keys, self.values):
self.cache.add(key, value)

def time_get(self):
for key in self.keys:
self.cache.get(key)

def time_set(self):
for key, value in zip(self.keys, self.values):
self.cache.set(key, value)

def time_get_or_set(self):
for key, value in zip(self.keys, self.values):
self.cache.get_or_set(key, value)

def time_touch(self):
for key in self.keys:
self.cache.touch(key)

def time_delete(self):
for key in self.keys:
self.cache.delete(key)

def time_get_many(self):
for _ in range(self.iterations):
self.cache.get_many(self.keys)

def time_set_many(self):
for _ in range(self.iterations):
self.cache.set_many(dict(zip(self.keys, self.values)))

def time_delete_many(self):
for _ in range(self.iterations):
self.cache.delete_many(self.keys)

def time_clear(self):
for _ in range(self.iterations):
self.cache.clear()

def time_incr(self):
for _ in range(self.iterations):
self.cache.incr(self.int_key)

def time_decr(self):
for _ in range(self.iterations):
self.cache.decr(self.int_key)
8 changes: 8 additions & 0 deletions benchmarks/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"},
}

CACHES = {
"db": {
"BACKEND": "django.core.cache.backends.db.DatabaseCache",
"LOCATION": "cache_table",
},
}

INSTALLED_APPS = [
"django.contrib.auth",
"django.contrib.contenttypes",
Expand Down Expand Up @@ -54,6 +61,7 @@
"benchmarks.query_benchmarks.query_select_related",
"benchmarks.req_resp_benchmarks.default_middleware",
"benchmarks.req_resp_benchmarks.http_methods",
"benchmarks.cache_benchmarks.database_cache",
]

SECRET_KEY = "NOT REALLY SECRET"
Expand Down