Skip to content

Instantly share code, notes, and snippets.

@playpauseandstop
Created November 1, 2012 14:22
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 playpauseandstop/3993906 to your computer and use it in GitHub Desktop.
Save playpauseandstop/3993906 to your computer and use it in GitHub Desktop.
Bench for getting data from Memcached via Django cache
#!/usr/bin/env python
import copy
import sys
import time
from argparse import ArgumentParser
from random import choice
from string import digits, letters
from django.core.cache import cache
DEFAULT_COUNTER = 1000
DEFAULT_LENGTH = 1024
def bench(length, counter):
to_cache = {}
values = [random_string(length) for _ in xrange(counter)]
for i, value in enumerate(values):
to_cache['key-random-{}'.format(i)] = value
cache.set_many(to_cache)
start = time.time()
from_cache = \
[cache.get('key-random-{}'.format(i)) for i in xrange(counter)]
end = time.time() - start
from_cache = copy.copy(from_cache)
cache.delete_many(to_cache.keys())
assert from_cache == values
print('{} value(s) read from cache by {:.4f} s.'.format(counter, end))
def main():
parser = ArgumentParser()
parser.add_argument(
'-c', '--counter', default=DEFAULT_COUNTER, dest='counter',
help='How many get iterations will make. By default: {}'.
format(DEFAULT_COUNTER), type=int
)
parser.add_argument(
'-l', '--length', default=DEFAULT_LENGTH, dest='length',
help='Length of cache value. By default: {}'.format(DEFAULT_LENGTH),
type=int
)
args = parser.parse_args(sys.argv[1:])
bench(args.length, args.counter)
def random_string(length=None):
length = length or DEFAULT_LENGTH
return u''.join([choice(digits + letters) for _ in xrange(length)])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment