Skip to content

Commit

Permalink
Add some benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
drgarcia1986 committed Jul 31, 2020
1 parent 409a32e commit 2952054
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
Empty file added benchmarks/__init__.py
Empty file.
60 changes: 60 additions & 0 deletions benchmarks/asynchronous.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import asyncio
from string import ascii_lowercase
from timeit import timeit

from aiocache.backends.redis import RedisCache
from aiocache.backends.memory import SimpleMemoryCache
from shared_memory_dict.caches.aiocache import SharedMemoryCache

loop = asyncio.get_event_loop()

cache_smc_aiocache = SharedMemoryCache(size=64)
cache_aiocache_redis = RedisCache(loop=loop)
cache_aiocache_memory = SimpleMemoryCache()


async def agressive(cache):
for c in ascii_lowercase:
for i in range(5):
await cache.set(f'{c}{i}', '1')
for i in range(10, 0, -1):
await cache.get(f'{c}{i}')


async def fun(cache):
await cache.set('fun', 'uhull')
for _ in range(3):
await cache.get('fun')


def collect(cache_var_name):
time_of_agressive = timeit(
stmt=f'loop.run_until_complete(agressive({cache_var_name}))',
number=100,
globals=globals()
)
time_of_fun = timeit(
stmt=f'loop.run_until_complete(fun({cache_var_name}))',
number=100,
globals=globals()
)

return {
'agressive': time_of_agressive,
'fun': time_of_fun
}


if __name__ == '__main__':
caches = {
k: collect(k)
for k in globals().keys()
if k.startswith('cache_')
}

print('Bench Against Async Solutions')
for k, v in caches.items():
print(f'Cache Type : {k}:')
print(f'agressive : {v["agressive"]}')
print(f'fun : {v["fun"]}')
print('')
1 change: 1 addition & 0 deletions benchmarks/fake_django_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SECRET_KEY = 'bla'
55 changes: 55 additions & 0 deletions benchmarks/sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from string import ascii_lowercase
from timeit import timeit

from django.core.cache.backends.locmem import LocMemCache
from django_redis.cache import RedisCache
from shared_memory_dict.caches.django import SharedMemoryCache

cache_smc_django = SharedMemoryCache(
'django', params={'OPTIONS': {'MEMORY_BLOCK_SIZE': 64}}
)
cache_django_redis = RedisCache(server='redis://127.0.0.1:6379/1', params={})
cache_django_locmem = LocMemCache('locmem', params={})


def agressive(cache):
for c in ascii_lowercase:
for i in range(5):
cache.set(f'{c}{i}', '1')
for i in range(10, 0, -1):
cache.get(f'{c}{i}')


def fun(cache):
cache.set('fun', 'uhull')
for _ in range(3):
cache.get('fun')


def collect(cache_var_name):
time_of_agressive = timeit(
stmt=f'agressive({cache_var_name})', number=100, globals=globals()
)
time_of_fun = timeit(
stmt=f'fun({cache_var_name})', number=100, globals=globals()
)

return {
'agressive': time_of_agressive,
'fun': time_of_fun
}


if __name__ == '__main__':
caches = {
k: collect(k)
for k in globals().keys()
if k.startswith('cache_')
}

print('Bench Against Sync Solutions')
for k, v in caches.items():
print(f'Cache Type : {k}:')
print(f'agressive : {v["agressive"]}')
print(f'fun : {v["fun"]}')
print('')

0 comments on commit 2952054

Please sign in to comment.