summaryrefslogtreecommitdiff
path: root/benchmarks
diff options
context:
space:
mode:
authorAnas <anas.el.amraoui@live.com>2021-11-30 18:05:51 +0200
committerGitHub <noreply@github.com>2021-11-30 18:05:51 +0200
commitb94e230b17d08e6c89d134e933c706256b79bc4a (patch)
tree993bd7565169229326b810b66939587431ab9dc6 /benchmarks
parent368a25f9d163d784a8896f1c087582405e98e006 (diff)
downloadredis-py-b94e230b17d08e6c89d134e933c706256b79bc4a.tar.gz
Added black and isort (#1734)
Diffstat (limited to 'benchmarks')
-rw-r--r--benchmarks/base.py17
-rw-r--r--benchmarks/basic_operations.py70
-rw-r--r--benchmarks/command_packer_benchmark.py49
-rw-r--r--benchmarks/socket_read_size.py27
4 files changed, 81 insertions, 82 deletions
diff --git a/benchmarks/base.py b/benchmarks/base.py
index 519c9cc..f52657f 100644
--- a/benchmarks/base.py
+++ b/benchmarks/base.py
@@ -1,9 +1,10 @@
import functools
import itertools
-import redis
import sys
import timeit
+import redis
+
class Benchmark:
ARGUMENTS = ()
@@ -15,9 +16,7 @@ class Benchmark:
# eventually make this more robust and take optional args from
# argparse
if self._client is None or kwargs:
- defaults = {
- 'db': 9
- }
+ defaults = {"db": 9}
defaults.update(kwargs)
pool = redis.ConnectionPool(**kwargs)
self._client = redis.Redis(connection_pool=pool)
@@ -30,16 +29,16 @@ class Benchmark:
pass
def run_benchmark(self):
- group_names = [group['name'] for group in self.ARGUMENTS]
- group_values = [group['values'] for group in self.ARGUMENTS]
+ 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(zip(group_names, value_set))
- arg_string = ', '.join(f'{p[0]}={p[1]}' for p in pairs)
- sys.stdout.write(f'Benchmark: {arg_string}... ')
+ arg_string = ", ".join(f"{p[0]}={p[1]}" for p in pairs)
+ sys.stdout.write(f"Benchmark: {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'{t:f}\n')
+ sys.stdout.write(f"{t:f}\n")
sys.stdout.flush()
diff --git a/benchmarks/basic_operations.py b/benchmarks/basic_operations.py
index cb009de..1dc4a87 100644
--- a/benchmarks/basic_operations.py
+++ b/benchmarks/basic_operations.py
@@ -1,24 +1,27 @@
-import redis
import time
-from functools import wraps
from argparse import ArgumentParser
+from functools import wraps
+
+import redis
def parse_args():
parser = ArgumentParser()
- parser.add_argument('-n',
- type=int,
- help='Total number of requests (default 100000)',
- default=100000)
- parser.add_argument('-P',
- type=int,
- help=('Pipeline <numreq> requests.'
- ' Default 1 (no pipeline).'),
- default=1)
- parser.add_argument('-s',
- type=int,
- help='Data size of SET/GET value in bytes (default 2)',
- default=2)
+ parser.add_argument(
+ "-n", type=int, help="Total number of requests (default 100000)", default=100000
+ )
+ parser.add_argument(
+ "-P",
+ type=int,
+ help=("Pipeline <numreq> requests." " Default 1 (no pipeline)."),
+ default=1,
+ )
+ parser.add_argument(
+ "-s",
+ type=int,
+ help="Data size of SET/GET value in bytes (default 2)",
+ default=2,
+ )
args = parser.parse_args()
return args
@@ -45,15 +48,16 @@ def timer(func):
start = time.monotonic()
ret = func(*args, **kwargs)
duration = time.monotonic() - start
- if 'num' in kwargs:
- count = kwargs['num']
+ if "num" in kwargs:
+ count = kwargs["num"]
else:
count = args[1]
- print(f'{func.__name__} - {count} Requests')
- print(f'Duration = {duration}')
- print(f'Rate = {count/duration}')
+ print(f"{func.__name__} - {count} Requests")
+ print(f"Duration = {duration}")
+ print(f"Rate = {count/duration}")
print()
return ret
+
return wrapper
@@ -62,9 +66,9 @@ def set_str(conn, num, pipeline_size, data_size):
if pipeline_size > 1:
conn = conn.pipeline()
- set_data = 'a'.ljust(data_size, '0')
+ set_data = "a".ljust(data_size, "0")
for i in range(num):
- conn.set(f'set_str:{i}', set_data)
+ conn.set(f"set_str:{i}", set_data)
if pipeline_size > 1 and i % pipeline_size == 0:
conn.execute()
@@ -79,7 +83,7 @@ def set_int(conn, num, pipeline_size, data_size):
set_data = 10 ** (data_size - 1)
for i in range(num):
- conn.set(f'set_int:{i}', set_data)
+ conn.set(f"set_int:{i}", set_data)
if pipeline_size > 1 and i % pipeline_size == 0:
conn.execute()
@@ -93,7 +97,7 @@ def get_str(conn, num, pipeline_size, data_size):
conn = conn.pipeline()
for i in range(num):
- conn.get(f'set_str:{i}')
+ conn.get(f"set_str:{i}")
if pipeline_size > 1 and i % pipeline_size == 0:
conn.execute()
@@ -107,7 +111,7 @@ def get_int(conn, num, pipeline_size, data_size):
conn = conn.pipeline()
for i in range(num):
- conn.get(f'set_int:{i}')
+ conn.get(f"set_int:{i}")
if pipeline_size > 1 and i % pipeline_size == 0:
conn.execute()
@@ -121,7 +125,7 @@ def incr(conn, num, pipeline_size, *args, **kwargs):
conn = conn.pipeline()
for i in range(num):
- conn.incr('incr_key')
+ conn.incr("incr_key")
if pipeline_size > 1 and i % pipeline_size == 0:
conn.execute()
@@ -136,7 +140,7 @@ def lpush(conn, num, pipeline_size, data_size):
set_data = 10 ** (data_size - 1)
for i in range(num):
- conn.lpush('lpush_key', set_data)
+ conn.lpush("lpush_key", set_data)
if pipeline_size > 1 and i % pipeline_size == 0:
conn.execute()
@@ -150,7 +154,7 @@ def lrange_300(conn, num, pipeline_size, data_size):
conn = conn.pipeline()
for i in range(num):
- conn.lrange('lpush_key', i, i+300)
+ conn.lrange("lpush_key", i, i + 300)
if pipeline_size > 1 and i % pipeline_size == 0:
conn.execute()
@@ -163,7 +167,7 @@ def lpop(conn, num, pipeline_size, data_size):
if pipeline_size > 1:
conn = conn.pipeline()
for i in range(num):
- conn.lpop('lpush_key')
+ conn.lpop("lpush_key")
if pipeline_size > 1 and i % pipeline_size == 0:
conn.execute()
if pipeline_size > 1:
@@ -175,11 +179,9 @@ def hmset(conn, num, pipeline_size, data_size):
if pipeline_size > 1:
conn = conn.pipeline()
- set_data = {'str_value': 'string',
- 'int_value': 123456,
- 'float_value': 123456.0}
+ set_data = {"str_value": "string", "int_value": 123456, "float_value": 123456.0}
for i in range(num):
- conn.hmset('hmset_key', set_data)
+ conn.hmset("hmset_key", set_data)
if pipeline_size > 1 and i % pipeline_size == 0:
conn.execute()
@@ -187,5 +189,5 @@ def hmset(conn, num, pipeline_size, data_size):
conn.execute()
-if __name__ == '__main__':
+if __name__ == "__main__":
run()
diff --git a/benchmarks/command_packer_benchmark.py b/benchmarks/command_packer_benchmark.py
index 3176c06..e66dbbc 100644
--- a/benchmarks/command_packer_benchmark.py
+++ b/benchmarks/command_packer_benchmark.py
@@ -1,7 +1,7 @@
-from redis.connection import (Connection, SYM_STAR, SYM_DOLLAR, SYM_EMPTY,
- SYM_CRLF)
from base import Benchmark
+from redis.connection import SYM_CRLF, SYM_DOLLAR, SYM_EMPTY, SYM_STAR, Connection
+
class StringJoiningConnection(Connection):
def send_packed_command(self, command, check_health=True):
@@ -13,7 +13,7 @@ class StringJoiningConnection(Connection):
except OSError as e:
self.disconnect()
if len(e.args) == 1:
- _errno, errmsg = 'UNKNOWN', e.args[0]
+ _errno, errmsg = "UNKNOWN", e.args[0]
else:
_errno, errmsg = e.args
raise ConnectionError(f"Error {_errno} while writing to socket. {errmsg}.")
@@ -23,12 +23,17 @@ class StringJoiningConnection(Connection):
def pack_command(self, *args):
"Pack a series of arguments into a value Redis command"
- args_output = SYM_EMPTY.join([
- SYM_EMPTY.join(
- (SYM_DOLLAR, str(len(k)).encode(), SYM_CRLF, k, SYM_CRLF))
- for k in map(self.encoder.encode, args)])
+ args_output = SYM_EMPTY.join(
+ [
+ SYM_EMPTY.join(
+ (SYM_DOLLAR, str(len(k)).encode(), SYM_CRLF, k, SYM_CRLF)
+ )
+ for k in map(self.encoder.encode, args)
+ ]
+ )
output = SYM_EMPTY.join(
- (SYM_STAR, str(len(args)).encode(), SYM_CRLF, args_output))
+ (SYM_STAR, str(len(args)).encode(), SYM_CRLF, args_output)
+ )
return output
@@ -44,7 +49,7 @@ class ListJoiningConnection(Connection):
except OSError as e:
self.disconnect()
if len(e.args) == 1:
- _errno, errmsg = 'UNKNOWN', e.args[0]
+ _errno, errmsg = "UNKNOWN", e.args[0]
else:
_errno, errmsg = e.args
raise ConnectionError(f"Error {_errno} while writing to socket. {errmsg}.")
@@ -54,19 +59,20 @@ class ListJoiningConnection(Connection):
def pack_command(self, *args):
output = []
- buff = SYM_EMPTY.join(
- (SYM_STAR, str(len(args)).encode(), SYM_CRLF))
+ buff = SYM_EMPTY.join((SYM_STAR, str(len(args)).encode(), SYM_CRLF))
for k in map(self.encoder.encode, args):
if len(buff) > 6000 or len(k) > 6000:
buff = SYM_EMPTY.join(
- (buff, SYM_DOLLAR, str(len(k)).encode(), SYM_CRLF))
+ (buff, SYM_DOLLAR, str(len(k)).encode(), SYM_CRLF)
+ )
output.append(buff)
output.append(k)
buff = SYM_CRLF
else:
- buff = SYM_EMPTY.join((buff, SYM_DOLLAR, str(len(k)).encode(),
- SYM_CRLF, k, SYM_CRLF))
+ buff = SYM_EMPTY.join(
+ (buff, SYM_DOLLAR, str(len(k)).encode(), SYM_CRLF, k, SYM_CRLF)
+ )
output.append(buff)
return output
@@ -75,13 +81,12 @@ class CommandPackerBenchmark(Benchmark):
ARGUMENTS = (
{
- 'name': 'connection_class',
- 'values': [StringJoiningConnection, ListJoiningConnection]
+ "name": "connection_class",
+ "values": [StringJoiningConnection, ListJoiningConnection],
},
{
- 'name': 'value_size',
- 'values': [10, 100, 1000, 10000, 100000, 1000000, 10000000,
- 100000000]
+ "name": "value_size",
+ "values": [10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000],
},
)
@@ -90,9 +95,9 @@ class CommandPackerBenchmark(Benchmark):
def run(self, connection_class, value_size):
r = self.get_client()
- x = 'a' * value_size
- r.set('benchmark', x)
+ x = "a" * value_size
+ r.set("benchmark", x)
-if __name__ == '__main__':
+if __name__ == "__main__":
CommandPackerBenchmark().run_benchmark()
diff --git a/benchmarks/socket_read_size.py b/benchmarks/socket_read_size.py
index 72a1b0a..3427956 100644
--- a/benchmarks/socket_read_size.py
+++ b/benchmarks/socket_read_size.py
@@ -1,34 +1,27 @@
-from redis.connection import PythonParser, HiredisParser
from base import Benchmark
+from redis.connection import HiredisParser, PythonParser
+
class SocketReadBenchmark(Benchmark):
ARGUMENTS = (
+ {"name": "parser", "values": [PythonParser, HiredisParser]},
{
- 'name': 'parser',
- 'values': [PythonParser, HiredisParser]
- },
- {
- 'name': 'value_size',
- 'values': [10, 100, 1000, 10000, 100000, 1000000, 10000000,
- 100000000]
+ "name": "value_size",
+ "values": [10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000],
},
- {
- 'name': 'read_size',
- 'values': [4096, 8192, 16384, 32768, 65536, 131072]
- }
+ {"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)
+ 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')
+ r.get("benchmark")
-if __name__ == '__main__':
+if __name__ == "__main__":
SocketReadBenchmark().run_benchmark()