Skip to content

Commit b94e230

Browse files
authored
Added black and isort (#1734)
1 parent 368a25f commit b94e230

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+7341
-6732
lines changed

benchmarks/base.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import functools
22
import itertools
3-
import redis
43
import sys
54
import timeit
65

6+
import redis
7+
78

89
class Benchmark:
910
ARGUMENTS = ()
@@ -15,9 +16,7 @@ def get_client(self, **kwargs):
1516
# eventually make this more robust and take optional args from
1617
# argparse
1718
if self._client is None or kwargs:
18-
defaults = {
19-
'db': 9
20-
}
19+
defaults = {"db": 9}
2120
defaults.update(kwargs)
2221
pool = redis.ConnectionPool(**kwargs)
2322
self._client = redis.Redis(connection_pool=pool)
@@ -30,16 +29,16 @@ def run(self, **kwargs):
3029
pass
3130

3231
def run_benchmark(self):
33-
group_names = [group['name'] for group in self.ARGUMENTS]
34-
group_values = [group['values'] for group in self.ARGUMENTS]
32+
group_names = [group["name"] for group in self.ARGUMENTS]
33+
group_values = [group["values"] for group in self.ARGUMENTS]
3534
for value_set in itertools.product(*group_values):
3635
pairs = list(zip(group_names, value_set))
37-
arg_string = ', '.join(f'{p[0]}={p[1]}' for p in pairs)
38-
sys.stdout.write(f'Benchmark: {arg_string}... ')
36+
arg_string = ", ".join(f"{p[0]}={p[1]}" for p in pairs)
37+
sys.stdout.write(f"Benchmark: {arg_string}... ")
3938
sys.stdout.flush()
4039
kwargs = dict(pairs)
4140
setup = functools.partial(self.setup, **kwargs)
4241
run = functools.partial(self.run, **kwargs)
4342
t = timeit.timeit(stmt=run, setup=setup, number=1000)
44-
sys.stdout.write(f'{t:f}\n')
43+
sys.stdout.write(f"{t:f}\n")
4544
sys.stdout.flush()

benchmarks/basic_operations.py

+36-34
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1-
import redis
21
import time
3-
from functools import wraps
42
from argparse import ArgumentParser
3+
from functools import wraps
4+
5+
import redis
56

67

78
def parse_args():
89
parser = ArgumentParser()
9-
parser.add_argument('-n',
10-
type=int,
11-
help='Total number of requests (default 100000)',
12-
default=100000)
13-
parser.add_argument('-P',
14-
type=int,
15-
help=('Pipeline <numreq> requests.'
16-
' Default 1 (no pipeline).'),
17-
default=1)
18-
parser.add_argument('-s',
19-
type=int,
20-
help='Data size of SET/GET value in bytes (default 2)',
21-
default=2)
10+
parser.add_argument(
11+
"-n", type=int, help="Total number of requests (default 100000)", default=100000
12+
)
13+
parser.add_argument(
14+
"-P",
15+
type=int,
16+
help=("Pipeline <numreq> requests." " Default 1 (no pipeline)."),
17+
default=1,
18+
)
19+
parser.add_argument(
20+
"-s",
21+
type=int,
22+
help="Data size of SET/GET value in bytes (default 2)",
23+
default=2,
24+
)
2225

2326
args = parser.parse_args()
2427
return args
@@ -45,15 +48,16 @@ def wrapper(*args, **kwargs):
4548
start = time.monotonic()
4649
ret = func(*args, **kwargs)
4750
duration = time.monotonic() - start
48-
if 'num' in kwargs:
49-
count = kwargs['num']
51+
if "num" in kwargs:
52+
count = kwargs["num"]
5053
else:
5154
count = args[1]
52-
print(f'{func.__name__} - {count} Requests')
53-
print(f'Duration = {duration}')
54-
print(f'Rate = {count/duration}')
55+
print(f"{func.__name__} - {count} Requests")
56+
print(f"Duration = {duration}")
57+
print(f"Rate = {count/duration}")
5558
print()
5659
return ret
60+
5761
return wrapper
5862

5963

@@ -62,9 +66,9 @@ def set_str(conn, num, pipeline_size, data_size):
6266
if pipeline_size > 1:
6367
conn = conn.pipeline()
6468

65-
set_data = 'a'.ljust(data_size, '0')
69+
set_data = "a".ljust(data_size, "0")
6670
for i in range(num):
67-
conn.set(f'set_str:{i}', set_data)
71+
conn.set(f"set_str:{i}", set_data)
6872
if pipeline_size > 1 and i % pipeline_size == 0:
6973
conn.execute()
7074

@@ -79,7 +83,7 @@ def set_int(conn, num, pipeline_size, data_size):
7983

8084
set_data = 10 ** (data_size - 1)
8185
for i in range(num):
82-
conn.set(f'set_int:{i}', set_data)
86+
conn.set(f"set_int:{i}", set_data)
8387
if pipeline_size > 1 and i % pipeline_size == 0:
8488
conn.execute()
8589

@@ -93,7 +97,7 @@ def get_str(conn, num, pipeline_size, data_size):
9397
conn = conn.pipeline()
9498

9599
for i in range(num):
96-
conn.get(f'set_str:{i}')
100+
conn.get(f"set_str:{i}")
97101
if pipeline_size > 1 and i % pipeline_size == 0:
98102
conn.execute()
99103

@@ -107,7 +111,7 @@ def get_int(conn, num, pipeline_size, data_size):
107111
conn = conn.pipeline()
108112

109113
for i in range(num):
110-
conn.get(f'set_int:{i}')
114+
conn.get(f"set_int:{i}")
111115
if pipeline_size > 1 and i % pipeline_size == 0:
112116
conn.execute()
113117

@@ -121,7 +125,7 @@ def incr(conn, num, pipeline_size, *args, **kwargs):
121125
conn = conn.pipeline()
122126

123127
for i in range(num):
124-
conn.incr('incr_key')
128+
conn.incr("incr_key")
125129
if pipeline_size > 1 and i % pipeline_size == 0:
126130
conn.execute()
127131

@@ -136,7 +140,7 @@ def lpush(conn, num, pipeline_size, data_size):
136140

137141
set_data = 10 ** (data_size - 1)
138142
for i in range(num):
139-
conn.lpush('lpush_key', set_data)
143+
conn.lpush("lpush_key", set_data)
140144
if pipeline_size > 1 and i % pipeline_size == 0:
141145
conn.execute()
142146

@@ -150,7 +154,7 @@ def lrange_300(conn, num, pipeline_size, data_size):
150154
conn = conn.pipeline()
151155

152156
for i in range(num):
153-
conn.lrange('lpush_key', i, i+300)
157+
conn.lrange("lpush_key", i, i + 300)
154158
if pipeline_size > 1 and i % pipeline_size == 0:
155159
conn.execute()
156160

@@ -163,7 +167,7 @@ def lpop(conn, num, pipeline_size, data_size):
163167
if pipeline_size > 1:
164168
conn = conn.pipeline()
165169
for i in range(num):
166-
conn.lpop('lpush_key')
170+
conn.lpop("lpush_key")
167171
if pipeline_size > 1 and i % pipeline_size == 0:
168172
conn.execute()
169173
if pipeline_size > 1:
@@ -175,17 +179,15 @@ def hmset(conn, num, pipeline_size, data_size):
175179
if pipeline_size > 1:
176180
conn = conn.pipeline()
177181

178-
set_data = {'str_value': 'string',
179-
'int_value': 123456,
180-
'float_value': 123456.0}
182+
set_data = {"str_value": "string", "int_value": 123456, "float_value": 123456.0}
181183
for i in range(num):
182-
conn.hmset('hmset_key', set_data)
184+
conn.hmset("hmset_key", set_data)
183185
if pipeline_size > 1 and i % pipeline_size == 0:
184186
conn.execute()
185187

186188
if pipeline_size > 1:
187189
conn.execute()
188190

189191

190-
if __name__ == '__main__':
192+
if __name__ == "__main__":
191193
run()
+27-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from redis.connection import (Connection, SYM_STAR, SYM_DOLLAR, SYM_EMPTY,
2-
SYM_CRLF)
31
from base import Benchmark
42

3+
from redis.connection import SYM_CRLF, SYM_DOLLAR, SYM_EMPTY, SYM_STAR, Connection
4+
55

66
class StringJoiningConnection(Connection):
77
def send_packed_command(self, command, check_health=True):
@@ -13,7 +13,7 @@ def send_packed_command(self, command, check_health=True):
1313
except OSError as e:
1414
self.disconnect()
1515
if len(e.args) == 1:
16-
_errno, errmsg = 'UNKNOWN', e.args[0]
16+
_errno, errmsg = "UNKNOWN", e.args[0]
1717
else:
1818
_errno, errmsg = e.args
1919
raise ConnectionError(f"Error {_errno} while writing to socket. {errmsg}.")
@@ -23,12 +23,17 @@ def send_packed_command(self, command, check_health=True):
2323

2424
def pack_command(self, *args):
2525
"Pack a series of arguments into a value Redis command"
26-
args_output = SYM_EMPTY.join([
27-
SYM_EMPTY.join(
28-
(SYM_DOLLAR, str(len(k)).encode(), SYM_CRLF, k, SYM_CRLF))
29-
for k in map(self.encoder.encode, args)])
26+
args_output = SYM_EMPTY.join(
27+
[
28+
SYM_EMPTY.join(
29+
(SYM_DOLLAR, str(len(k)).encode(), SYM_CRLF, k, SYM_CRLF)
30+
)
31+
for k in map(self.encoder.encode, args)
32+
]
33+
)
3034
output = SYM_EMPTY.join(
31-
(SYM_STAR, str(len(args)).encode(), SYM_CRLF, args_output))
35+
(SYM_STAR, str(len(args)).encode(), SYM_CRLF, args_output)
36+
)
3237
return output
3338

3439

@@ -44,7 +49,7 @@ def send_packed_command(self, command, check_health=True):
4449
except OSError as e:
4550
self.disconnect()
4651
if len(e.args) == 1:
47-
_errno, errmsg = 'UNKNOWN', e.args[0]
52+
_errno, errmsg = "UNKNOWN", e.args[0]
4853
else:
4954
_errno, errmsg = e.args
5055
raise ConnectionError(f"Error {_errno} while writing to socket. {errmsg}.")
@@ -54,19 +59,20 @@ def send_packed_command(self, command, check_health=True):
5459

5560
def pack_command(self, *args):
5661
output = []
57-
buff = SYM_EMPTY.join(
58-
(SYM_STAR, str(len(args)).encode(), SYM_CRLF))
62+
buff = SYM_EMPTY.join((SYM_STAR, str(len(args)).encode(), SYM_CRLF))
5963

6064
for k in map(self.encoder.encode, args):
6165
if len(buff) > 6000 or len(k) > 6000:
6266
buff = SYM_EMPTY.join(
63-
(buff, SYM_DOLLAR, str(len(k)).encode(), SYM_CRLF))
67+
(buff, SYM_DOLLAR, str(len(k)).encode(), SYM_CRLF)
68+
)
6469
output.append(buff)
6570
output.append(k)
6671
buff = SYM_CRLF
6772
else:
68-
buff = SYM_EMPTY.join((buff, SYM_DOLLAR, str(len(k)).encode(),
69-
SYM_CRLF, k, SYM_CRLF))
73+
buff = SYM_EMPTY.join(
74+
(buff, SYM_DOLLAR, str(len(k)).encode(), SYM_CRLF, k, SYM_CRLF)
75+
)
7076
output.append(buff)
7177
return output
7278

@@ -75,13 +81,12 @@ class CommandPackerBenchmark(Benchmark):
7581

7682
ARGUMENTS = (
7783
{
78-
'name': 'connection_class',
79-
'values': [StringJoiningConnection, ListJoiningConnection]
84+
"name": "connection_class",
85+
"values": [StringJoiningConnection, ListJoiningConnection],
8086
},
8187
{
82-
'name': 'value_size',
83-
'values': [10, 100, 1000, 10000, 100000, 1000000, 10000000,
84-
100000000]
88+
"name": "value_size",
89+
"values": [10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000],
8590
},
8691
)
8792

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

9196
def run(self, connection_class, value_size):
9297
r = self.get_client()
93-
x = 'a' * value_size
94-
r.set('benchmark', x)
98+
x = "a" * value_size
99+
r.set("benchmark", x)
95100

96101

97-
if __name__ == '__main__':
102+
if __name__ == "__main__":
98103
CommandPackerBenchmark().run_benchmark()

benchmarks/socket_read_size.py

+10-17
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
1-
from redis.connection import PythonParser, HiredisParser
21
from base import Benchmark
32

3+
from redis.connection import HiredisParser, PythonParser
4+
45

56
class SocketReadBenchmark(Benchmark):
67

78
ARGUMENTS = (
9+
{"name": "parser", "values": [PythonParser, HiredisParser]},
810
{
9-
'name': 'parser',
10-
'values': [PythonParser, HiredisParser]
11-
},
12-
{
13-
'name': 'value_size',
14-
'values': [10, 100, 1000, 10000, 100000, 1000000, 10000000,
15-
100000000]
11+
"name": "value_size",
12+
"values": [10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000],
1613
},
17-
{
18-
'name': 'read_size',
19-
'values': [4096, 8192, 16384, 32768, 65536, 131072]
20-
}
14+
{"name": "read_size", "values": [4096, 8192, 16384, 32768, 65536, 131072]},
2115
)
2216

2317
def setup(self, value_size, read_size, parser):
24-
r = self.get_client(parser_class=parser,
25-
socket_read_size=read_size)
26-
r.set('benchmark', 'a' * value_size)
18+
r = self.get_client(parser_class=parser, socket_read_size=read_size)
19+
r.set("benchmark", "a" * value_size)
2720

2821
def run(self, value_size, read_size, parser):
2922
r = self.get_client()
30-
r.get('benchmark')
23+
r.get("benchmark")
3124

3225

33-
if __name__ == '__main__':
26+
if __name__ == "__main__":
3427
SocketReadBenchmark().run_benchmark()

dev_requirements.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
flake8>=3.9.2
1+
black==21.11b1
2+
flake8==4.0.1
23
flynt~=0.69.0
4+
isort==5.10.1
35
pytest==6.2.5
46
pytest-timeout==2.0.1
57
tox==3.24.4

0 commit comments

Comments
 (0)