Skip to content

Commit

Permalink
Added black and isort (#1734)
Browse files Browse the repository at this point in the history
  • Loading branch information
WisdomPill authored Nov 30, 2021
1 parent 368a25f commit b94e230
Show file tree
Hide file tree
Showing 60 changed files with 7,341 additions and 6,732 deletions.
17 changes: 8 additions & 9 deletions benchmarks/base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import functools
import itertools
import redis
import sys
import timeit

import redis


class Benchmark:
ARGUMENTS = ()
Expand All @@ -15,9 +16,7 @@ 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 = {"db": 9}
defaults.update(kwargs)
pool = redis.ConnectionPool(**kwargs)
self._client = redis.Redis(connection_pool=pool)
Expand All @@ -30,16 +29,16 @@ 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]
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()
70 changes: 36 additions & 34 deletions benchmarks/basic_operations.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -45,15 +48,16 @@ def wrapper(*args, **kwargs):
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


Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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:
Expand All @@ -175,17 +179,15 @@ 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()

if pipeline_size > 1:
conn.execute()


if __name__ == '__main__':
if __name__ == "__main__":
run()
49 changes: 27 additions & 22 deletions benchmarks/command_packer_benchmark.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -13,7 +13,7 @@ def send_packed_command(self, command, check_health=True):
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}.")
Expand All @@ -23,12 +23,17 @@ def send_packed_command(self, command, check_health=True):

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


Expand All @@ -44,7 +49,7 @@ def send_packed_command(self, command, check_health=True):
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}.")
Expand All @@ -54,19 +59,20 @@ def send_packed_command(self, command, check_health=True):

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

Expand All @@ -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],
},
)

Expand All @@ -90,9 +95,9 @@ def setup(self, connection_class, value_size):

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()
27 changes: 10 additions & 17 deletions benchmarks/socket_read_size.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 3 additions & 1 deletion dev_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
flake8>=3.9.2
black==21.11b1
flake8==4.0.1
flynt~=0.69.0
isort==5.10.1
pytest==6.2.5
pytest-timeout==2.0.1
tox==3.24.4
Expand Down
Loading

0 comments on commit b94e230

Please sign in to comment.