Skip to content

Commit 5f6e9f0

Browse files
authored
Merge branch 'master' into target_nodes
2 parents 0456f66 + 175a05f commit 5f6e9f0

Some content is hidden

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

78 files changed

+9038
-6702
lines changed

benchmarks/base.py

Lines changed: 8 additions & 9 deletions
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(['%s=%s' % (p[0], p[1]) for p in pairs])
38-
sys.stdout.write('Benchmark: %s... ' % 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\n' % t)
43+
sys.stdout.write(f"{t:f}\n")
4544
sys.stdout.flush()

benchmarks/basic_operations.py

Lines changed: 38 additions & 39 deletions
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('{} - {} Requests'.format(func.__name__, count))
53-
print('Duration = {}'.format(duration))
54-
print('Rate = {}'.format(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,10 +66,9 @@ def set_str(conn, num, pipeline_size, data_size):
6266
if pipeline_size > 1:
6367
conn = conn.pipeline()
6468

65-
format_str = '{:0<%d}' % data_size
66-
set_data = format_str.format('a')
69+
set_data = "a".ljust(data_size, "0")
6770
for i in range(num):
68-
conn.set('set_str:%d' % i, set_data)
71+
conn.set(f"set_str:{i}", set_data)
6972
if pipeline_size > 1 and i % pipeline_size == 0:
7073
conn.execute()
7174

@@ -78,10 +81,9 @@ def set_int(conn, num, pipeline_size, data_size):
7881
if pipeline_size > 1:
7982
conn = conn.pipeline()
8083

81-
format_str = '{:0<%d}' % data_size
82-
set_data = int(format_str.format('1'))
84+
set_data = 10 ** (data_size - 1)
8385
for i in range(num):
84-
conn.set('set_int:%d' % i, set_data)
86+
conn.set(f"set_int:{i}", set_data)
8587
if pipeline_size > 1 and i % pipeline_size == 0:
8688
conn.execute()
8789

@@ -95,7 +97,7 @@ def get_str(conn, num, pipeline_size, data_size):
9597
conn = conn.pipeline()
9698

9799
for i in range(num):
98-
conn.get('set_str:%d' % i)
100+
conn.get(f"set_str:{i}")
99101
if pipeline_size > 1 and i % pipeline_size == 0:
100102
conn.execute()
101103

@@ -109,7 +111,7 @@ def get_int(conn, num, pipeline_size, data_size):
109111
conn = conn.pipeline()
110112

111113
for i in range(num):
112-
conn.get('set_int:%d' % i)
114+
conn.get(f"set_int:{i}")
113115
if pipeline_size > 1 and i % pipeline_size == 0:
114116
conn.execute()
115117

@@ -123,7 +125,7 @@ def incr(conn, num, pipeline_size, *args, **kwargs):
123125
conn = conn.pipeline()
124126

125127
for i in range(num):
126-
conn.incr('incr_key')
128+
conn.incr("incr_key")
127129
if pipeline_size > 1 and i % pipeline_size == 0:
128130
conn.execute()
129131

@@ -136,10 +138,9 @@ def lpush(conn, num, pipeline_size, data_size):
136138
if pipeline_size > 1:
137139
conn = conn.pipeline()
138140

139-
format_str = '{:0<%d}' % data_size
140-
set_data = int(format_str.format('1'))
141+
set_data = 10 ** (data_size - 1)
141142
for i in range(num):
142-
conn.lpush('lpush_key', set_data)
143+
conn.lpush("lpush_key", set_data)
143144
if pipeline_size > 1 and i % pipeline_size == 0:
144145
conn.execute()
145146

@@ -153,7 +154,7 @@ def lrange_300(conn, num, pipeline_size, data_size):
153154
conn = conn.pipeline()
154155

155156
for i in range(num):
156-
conn.lrange('lpush_key', i, i+300)
157+
conn.lrange("lpush_key", i, i + 300)
157158
if pipeline_size > 1 and i % pipeline_size == 0:
158159
conn.execute()
159160

@@ -166,7 +167,7 @@ def lpop(conn, num, pipeline_size, data_size):
166167
if pipeline_size > 1:
167168
conn = conn.pipeline()
168169
for i in range(num):
169-
conn.lpop('lpush_key')
170+
conn.lpop("lpush_key")
170171
if pipeline_size > 1 and i % pipeline_size == 0:
171172
conn.execute()
172173
if pipeline_size > 1:
@@ -178,17 +179,15 @@ def hmset(conn, num, pipeline_size, data_size):
178179
if pipeline_size > 1:
179180
conn = conn.pipeline()
180181

181-
set_data = {'str_value': 'string',
182-
'int_value': 123456,
183-
'float_value': 123456.0}
182+
set_data = {"str_value": "string", "int_value": 123456, "float_value": 123456.0}
184183
for i in range(num):
185-
conn.hmset('hmset_key', set_data)
184+
conn.hmset("hmset_key", set_data)
186185
if pipeline_size > 1 and i % pipeline_size == 0:
187186
conn.execute()
188187

189188
if pipeline_size > 1:
190189
conn.execute()
191190

192191

193-
if __name__ == '__main__':
192+
if __name__ == "__main__":
194193
run()
Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import socket
2-
from redis.connection import (Connection, SYM_STAR, SYM_DOLLAR, SYM_EMPTY,
3-
SYM_CRLF)
41
from base import Benchmark
52

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

76
class StringJoiningConnection(Connection):
87
def send_packed_command(self, command, check_health=True):
@@ -11,26 +10,30 @@ def send_packed_command(self, command, check_health=True):
1110
self.connect()
1211
try:
1312
self._sock.sendall(command)
14-
except socket.error as e:
13+
except OSError as e:
1514
self.disconnect()
1615
if len(e.args) == 1:
17-
_errno, errmsg = 'UNKNOWN', e.args[0]
16+
_errno, errmsg = "UNKNOWN", e.args[0]
1817
else:
1918
_errno, errmsg = e.args
20-
raise ConnectionError("Error %s while writing to socket. %s." %
21-
(_errno, errmsg))
19+
raise ConnectionError(f"Error {_errno} while writing to socket. {errmsg}.")
2220
except Exception:
2321
self.disconnect()
2422
raise
2523

2624
def pack_command(self, *args):
2725
"Pack a series of arguments into a value Redis command"
28-
args_output = SYM_EMPTY.join([
29-
SYM_EMPTY.join(
30-
(SYM_DOLLAR, str(len(k)).encode(), SYM_CRLF, k, SYM_CRLF))
31-
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+
)
3234
output = SYM_EMPTY.join(
33-
(SYM_STAR, str(len(args)).encode(), SYM_CRLF, args_output))
35+
(SYM_STAR, str(len(args)).encode(), SYM_CRLF, args_output)
36+
)
3437
return output
3538

3639

@@ -43,33 +46,33 @@ def send_packed_command(self, command, check_health=True):
4346
command = [command]
4447
for item in command:
4548
self._sock.sendall(item)
46-
except socket.error as e:
49+
except OSError as e:
4750
self.disconnect()
4851
if len(e.args) == 1:
49-
_errno, errmsg = 'UNKNOWN', e.args[0]
52+
_errno, errmsg = "UNKNOWN", e.args[0]
5053
else:
5154
_errno, errmsg = e.args
52-
raise ConnectionError("Error %s while writing to socket. %s." %
53-
(_errno, errmsg))
55+
raise ConnectionError(f"Error {_errno} while writing to socket. {errmsg}.")
5456
except Exception:
5557
self.disconnect()
5658
raise
5759

5860
def pack_command(self, *args):
5961
output = []
60-
buff = SYM_EMPTY.join(
61-
(SYM_STAR, str(len(args)).encode(), SYM_CRLF))
62+
buff = SYM_EMPTY.join((SYM_STAR, str(len(args)).encode(), SYM_CRLF))
6263

6364
for k in map(self.encoder.encode, args):
6465
if len(buff) > 6000 or len(k) > 6000:
6566
buff = SYM_EMPTY.join(
66-
(buff, SYM_DOLLAR, str(len(k)).encode(), SYM_CRLF))
67+
(buff, SYM_DOLLAR, str(len(k)).encode(), SYM_CRLF)
68+
)
6769
output.append(buff)
6870
output.append(k)
6971
buff = SYM_CRLF
7072
else:
71-
buff = SYM_EMPTY.join((buff, SYM_DOLLAR, str(len(k)).encode(),
72-
SYM_CRLF, k, SYM_CRLF))
73+
buff = SYM_EMPTY.join(
74+
(buff, SYM_DOLLAR, str(len(k)).encode(), SYM_CRLF, k, SYM_CRLF)
75+
)
7376
output.append(buff)
7477
return output
7578

@@ -78,13 +81,12 @@ class CommandPackerBenchmark(Benchmark):
7881

7982
ARGUMENTS = (
8083
{
81-
'name': 'connection_class',
82-
'values': [StringJoiningConnection, ListJoiningConnection]
84+
"name": "connection_class",
85+
"values": [StringJoiningConnection, ListJoiningConnection],
8386
},
8487
{
85-
'name': 'value_size',
86-
'values': [10, 100, 1000, 10000, 100000, 1000000, 10000000,
87-
100000000]
88+
"name": "value_size",
89+
"values": [10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000],
8890
},
8991
)
9092

@@ -93,9 +95,9 @@ def setup(self, connection_class, value_size):
9395

9496
def run(self, connection_class, value_size):
9597
r = self.get_client()
96-
x = 'a' * value_size
97-
r.set('benchmark', x)
98+
x = "a" * value_size
99+
r.set("benchmark", x)
98100

99101

100-
if __name__ == '__main__':
102+
if __name__ == "__main__":
101103
CommandPackerBenchmark().run_benchmark()

benchmarks/socket_read_size.py

Lines changed: 10 additions & 17 deletions
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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
flake8>=3.9.2
1+
black==21.11b1
2+
flake8==4.0.1
3+
flynt~=0.69.0
4+
isort==5.10.1
25
pytest==6.2.5
36
pytest-timeout==2.0.1
47
tox==3.24.4

0 commit comments

Comments
 (0)