From e2a6c82dae403d60d457d06e8cb8c646595171fe Mon Sep 17 00:00:00 2001 From: karlicoss Date: Sat, 9 Sep 2023 02:03:27 +0100 Subject: [PATCH] misc: some initial Redis evaluation relevant to - https://github.com/karlicoss/cachew/issues/9 - https://github.com/karlicoss/cachew/issues/14 (thanks to redis expiry might be very easy to implement) --- misc/test_redis/docker-compose.yml | 21 +++++++++ misc/test_redis/test.py | 72 ++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 misc/test_redis/docker-compose.yml create mode 100755 misc/test_redis/test.py diff --git a/misc/test_redis/docker-compose.yml b/misc/test_redis/docker-compose.yml new file mode 100644 index 0000000..dc23f48 --- /dev/null +++ b/misc/test_redis/docker-compose.yml @@ -0,0 +1,21 @@ +services: + redis: + image: "redis:alpine" + # restart: always + command: + - "sh" + - "-euc" + - | + exec redis-server + # - | + # echo "requirepass '$$REDIS_PASSWORD'" > /etc/redis.conf + # exec redis-server /etc/redis.conf + # environment: + # REDIS_PASSWORD: "password" + ports: + - 6379:6379 + volumes: + - "redis-cachew:/data:rw" + +volumes: + redis-cachew: diff --git a/misc/test_redis/test.py b/misc/test_redis/test.py new file mode 100755 index 0000000..7f04f23 --- /dev/null +++ b/misc/test_redis/test.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 +from time import time + +from loguru import logger +from more_itertools import ilen +import redis + +r = redis.Redis(host='localhost', port=6379, db=0) + + +N = 1_000_000 + +def items(): + yield from map(str, range(N)) + + +TAG = 'keys' + + +def reset(): + r.delete(TAG) + + +def write(): + for i, obj in enumerate(items()): + key = f'obj:{i}' + r.hset(key, 'data', obj) + r.lpush(TAG, key) + +def read(): + keys = r.lrange(TAG, 0, -1) + result = ( + r.hget(key, 'data') for key in keys + ) + print('total', ilen(result)) + + +# TODO could use lmove for atomic operations? +def write2(): + for obj in items(): + r.lpush(TAG, obj) + + +def read2(): + result = r.lrange(TAG, 0, -1) + print('total', ilen(result)) + + +reset() + +a = time() +write2() +b = time() +logger.info(f'writing took {b - a:.1f}s') + +a = time() +read2() +b = time() +logger.info(f'reading took {b - a:.1f}s') + + +# with read()/write() +# 100000 strings: +# 2023-09-09 01:50:23.498 | INFO | __main__::37 - writing took 13.1s +# 2023-09-09 01:50:30.052 | INFO | __main__::42 - reading took 6.6s +# hmm kinda slow.. + + +# with read2/write2, writing about 7secs, and reading is instantaneous?? +# for 1M objects, writing took 60 secs, and reading 0.2s? +# lol could be promising... +# I guess it's not iterative, but could retrieve items in batches?