summaryrefslogtreecommitdiff
path: root/benchmarks
diff options
context:
space:
mode:
authorAndy McCurdy <andy@andymccurdy.com>2014-04-28 22:21:24 -0700
committerAndy McCurdy <andy@andymccurdy.com>2014-04-28 22:22:32 -0700
commit1851035084f49c483d74b41ffc79a50d019e97c5 (patch)
treeaf1dc6eb228d1aebb86647403f8085dd5027e45e /benchmarks
parent24e9f1147b168ac139ece8d20b5be82583e87731 (diff)
downloadredis-py-1851035084f49c483d74b41ffc79a50d019e97c5.tar.gz
tiny benchmark framework and a benchmark to determine the optimal number of bytes to read from a socket
Diffstat (limited to 'benchmarks')
-rw-r--r--benchmarks/__init__.py0
-rw-r--r--benchmarks/base.py46
-rw-r--r--benchmarks/socket_read_size.py34
3 files changed, 80 insertions, 0 deletions
diff --git a/benchmarks/__init__.py b/benchmarks/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/benchmarks/__init__.py
diff --git a/benchmarks/base.py b/benchmarks/base.py
new file mode 100644
index 0000000..a97001f
--- /dev/null
+++ b/benchmarks/base.py
@@ -0,0 +1,46 @@
+import functools
+import itertools
+import redis
+import sys
+import timeit
+from redis._compat import izip
+
+
+class Benchmark(object):
+ ARGUMENTS = ()
+
+ def __init__(self):
+ self._client = None
+
+ def get_client(self, **kwargs):
+ # eventually make this more robust and take optional args from
+ # argparse
+ if self._client is None or kwargs:
+ defaults = {
+ 'db': 9
+ }
+ defaults.update(kwargs)
+ pool = redis.ConnectionPool(**kwargs)
+ self._client = redis.StrictRedis(connection_pool=pool)
+ return self._client
+
+ def setup(self, **kwargs):
+ pass
+
+ def run(self, **kwargs):
+ pass
+
+ def run_benchmark(self):
+ group_names = [group['name'] for group in self.ARGUMENTS]
+ group_values = [group['values'] for group in self.ARGUMENTS]
+ for value_set in itertools.product(*group_values):
+ pairs = list(izip(group_names, value_set))
+ arg_string = ', '.join(['%s=%s' % (p[0], p[1]) for p in pairs])
+ sys.stdout.write('Benchmark: %s... ' % arg_string)
+ sys.stdout.flush()
+ kwargs = dict(pairs)
+ setup = functools.partial(self.setup, **kwargs)
+ run = functools.partial(self.run, **kwargs)
+ t = timeit.timeit(stmt=run, setup=setup, number=1000)
+ sys.stdout.write('%f\n' % t)
+ sys.stdout.flush()
diff --git a/benchmarks/socket_read_size.py b/benchmarks/socket_read_size.py
new file mode 100644
index 0000000..72a1b0a
--- /dev/null
+++ b/benchmarks/socket_read_size.py
@@ -0,0 +1,34 @@
+from redis.connection import PythonParser, HiredisParser
+from base import Benchmark
+
+
+class SocketReadBenchmark(Benchmark):
+
+ ARGUMENTS = (
+ {
+ 'name': 'parser',
+ 'values': [PythonParser, HiredisParser]
+ },
+ {
+ 'name': 'value_size',
+ 'values': [10, 100, 1000, 10000, 100000, 1000000, 10000000,
+ 100000000]
+ },
+ {
+ 'name': 'read_size',
+ 'values': [4096, 8192, 16384, 32768, 65536, 131072]
+ }
+ )
+
+ def setup(self, value_size, read_size, parser):
+ r = self.get_client(parser_class=parser,
+ socket_read_size=read_size)
+ r.set('benchmark', 'a' * value_size)
+
+ def run(self, value_size, read_size, parser):
+ r = self.get_client()
+ r.get('benchmark')
+
+
+if __name__ == '__main__':
+ SocketReadBenchmark().run_benchmark()