Skip to content

Commit

Permalink
Refined footprint tests
Browse files Browse the repository at this point in the history
  • Loading branch information
argaen committed May 13, 2017
1 parent 6a11577 commit 0e8f1a6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
23 changes: 14 additions & 9 deletions aiocache/backends/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,24 @@ async def _multi_set(self, pairs, ttl=None):
"""
ttl = ttl or 0

with await self._connect() as redis:
transaction = redis.multi_exec()
flattened = list(itertools.chain.from_iterable(
(key, value) for key, value in pairs))
transaction.mset(*flattened)
if ttl > 0:
for key in flattened[::2]:
transaction.expire(key, timeout=ttl)
flattened = list(itertools.chain.from_iterable(
(key, value) for key, value in pairs))

await transaction.execute()
with await self._connect() as redis:
if ttl:
await self.__multi_set_ttl(redis, flattened, ttl)
else:
await redis.mset(*flattened)

return True

async def __multi_set_ttl(self, conn, flattened, ttl):
redis = conn.multi_exec()
redis.mset(*flattened)
for key in flattened[::2]:
redis.expire(key, timeout=ttl)
await redis.execute()

async def _add(self, key, value, ttl=None):
"""
Stores the value in the given key. Raises an error if the
Expand Down
7 changes: 5 additions & 2 deletions tests/performance/test_footprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async def test_redis_getsetdel(self, aioredis_pool, redis_cache):

print("{:0.2f}/{:0.2f}: {:0.2f}".format(
aiocache_total_time, aioredis_total_time, aiocache_total_time/aioredis_total_time))
assert aiocache_total_time/aioredis_total_time < 1.25
assert aiocache_total_time/aioredis_total_time < 1.30

@pytest.mark.asyncio
async def test_redis_multigetsetdel(self, aioredis_pool, redis_cache):
Expand Down Expand Up @@ -66,7 +66,7 @@ async def test_redis_multigetsetdel(self, aioredis_pool, redis_cache):

print("{:0.2f}/{:0.2f}: {:0.2f}".format(
aiocache_total_time, aioredis_total_time, aiocache_total_time/aioredis_total_time))
assert aiocache_total_time/aioredis_total_time < 1.25
assert aiocache_total_time/aioredis_total_time < 1.30


@pytest.fixture
Expand Down Expand Up @@ -120,6 +120,9 @@ async def test_memcached_multigetsetdel(self, aiomcache_pool, memcached_cache):
# TODO: aiomcache pool behaves really BAD with concurrent requests so multi_set
# is not ideal. With the new MR I've submitted aiomcache/#46 it will improve
# although its not ideal...
# Also, performance if fat worse in local because we don't get benefit from
# concurrency because latency is stable. In build and real environments, the
# number is better.
await memcached_cache.multi_set([(x, x) for x in values], timeout=0)
await memcached_cache.multi_get(values, timeout=0)
for k in values:
Expand Down
4 changes: 1 addition & 3 deletions tests/ut/backends/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ async def test_multi_get(self, redis):
async def test_multi_set(self, redis):
cache, pool = redis
await cache._multi_set([(pytest.KEY, "value"), (pytest.KEY_1, "random")])
assert pool.client.multi_exec.call_count == 1
pool.transaction.mset.assert_called_with(pytest.KEY, "value", pytest.KEY_1, "random")
assert pool.transaction.execute.call_count == 1
pool.client.mset.assert_called_with(pytest.KEY, "value", pytest.KEY_1, "random")

@pytest.mark.asyncio
async def test_multi_set_with_ttl(self, redis):
Expand Down

0 comments on commit 0e8f1a6

Please sign in to comment.