| 1 |
import time |
| 2 |
from argparse import ArgumentParser |
| 3 |
from functools import wraps |
| 4 |
|
| 5 |
import valkey |
| 6 |
|
| 7 |
|
| 8 |
def parse_args(): |
| 9 |
parser = ArgumentParser() |
| 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 |
) |
| 25 |
|
| 26 |
args = parser.parse_args() |
| 27 |
return args |
| 28 |
|
| 29 |
|
| 30 |
def run(): |
| 31 |
args = parse_args() |
| 32 |
r = valkey.Valkey() |
| 33 |
r.flushall() |
| 34 |
set_str(conn=r, num=args.n, pipeline_size=args.P, data_size=args.s) |
| 35 |
set_int(conn=r, num=args.n, pipeline_size=args.P, data_size=args.s) |
| 36 |
get_str(conn=r, num=args.n, pipeline_size=args.P, data_size=args.s) |
| 37 |
get_int(conn=r, num=args.n, pipeline_size=args.P, data_size=args.s) |
| 38 |
incr(conn=r, num=args.n, pipeline_size=args.P, data_size=args.s) |
| 39 |
lpush(conn=r, num=args.n, pipeline_size=args.P, data_size=args.s) |
| 40 |
lrange_300(conn=r, num=args.n, pipeline_size=args.P, data_size=args.s) |
| 41 |
lpop(conn=r, num=args.n, pipeline_size=args.P, data_size=args.s) |
| 42 |
hmset(conn=r, num=args.n, pipeline_size=args.P, data_size=args.s) |
| 43 |
|
| 44 |
|
| 45 |
def timer(func): |
| 46 |
@wraps(func) |
| 47 |
def wrapper(*args, **kwargs): |
| 48 |
start = time.monotonic() |
| 49 |
ret = func(*args, **kwargs) |
| 50 |
duration = time.monotonic() - start |
| 51 |
if "num" in kwargs: |
| 52 |
count = kwargs["num"] |
| 53 |
else: |
| 54 |
count = args[1] |
| 55 |
print(f"{func.__name__} - {count} Requests") |
| 56 |
print(f"Duration = {duration}") |
| 57 |
print(f"Rate = {count/duration}") |
| 58 |
print() |
| 59 |
return ret |
| 60 |
|
| 61 |
return wrapper |
| 62 |
|
| 63 |
|
| 64 |
@timer |
| 65 |
def set_str(conn, num, pipeline_size, data_size): |
| 66 |
if pipeline_size > 1: |
| 67 |
conn = conn.pipeline() |
| 68 |
|
| 69 |
set_data = "a".ljust(data_size, "0") |
| 70 |
for i in range(num): |
| 71 |
conn.set(f"set_str:{i}", set_data) |
| 72 |
if pipeline_size > 1 and i % pipeline_size == 0: |
| 73 |
conn.execute() |
| 74 |
|
| 75 |
if pipeline_size > 1: |
| 76 |
conn.execute() |
| 77 |
|
| 78 |
|
| 79 |
@timer |
| 80 |
def set_int(conn, num, pipeline_size, data_size): |
| 81 |
if pipeline_size > 1: |
| 82 |
conn = conn.pipeline() |
| 83 |
|
| 84 |
set_data = 10 ** (data_size - 1) |
| 85 |
for i in range(num): |
| 86 |
conn.set(f"set_int:{i}", set_data) |
| 87 |
if pipeline_size > 1 and i % pipeline_size == 0: |
| 88 |
conn.execute() |
| 89 |
|
| 90 |
if pipeline_size > 1: |
| 91 |
conn.execute() |
| 92 |
|
| 93 |
|
| 94 |
@timer |
| 95 |
def get_str(conn, num, pipeline_size, data_size): |
| 96 |
if pipeline_size > 1: |
| 97 |
conn = conn.pipeline() |
| 98 |
|
| 99 |
for i in range(num): |
| 100 |
conn.get(f"set_str:{i}") |
| 101 |
if pipeline_size > 1 and i % pipeline_size == 0: |
| 102 |
conn.execute() |
| 103 |
|
| 104 |
if pipeline_size > 1: |
| 105 |
conn.execute() |
| 106 |
|
| 107 |
|
| 108 |
@timer |
| 109 |
def get_int(conn, num, pipeline_size, data_size): |
| 110 |
if pipeline_size > 1: |
| 111 |
conn = conn.pipeline() |
| 112 |
|
| 113 |
for i in range(num): |
| 114 |
conn.get(f"set_int:{i}") |
| 115 |
if pipeline_size > 1 and i % pipeline_size == 0: |
| 116 |
conn.execute() |
| 117 |
|
| 118 |
if pipeline_size > 1: |
| 119 |
conn.execute() |
| 120 |
|
| 121 |
|
| 122 |
@timer |
| 123 |
def incr(conn, num, pipeline_size, *args, **kwargs): |
| 124 |
if pipeline_size > 1: |
| 125 |
conn = conn.pipeline() |
| 126 |
|
| 127 |
for i in range(num): |
| 128 |
conn.incr("incr_key") |
| 129 |
if pipeline_size > 1 and i % pipeline_size == 0: |
| 130 |
conn.execute() |
| 131 |
|
| 132 |
if pipeline_size > 1: |
| 133 |
conn.execute() |
| 134 |
|
| 135 |
|
| 136 |
@timer |
| 137 |
def lpush(conn, num, pipeline_size, data_size): |
| 138 |
if pipeline_size > 1: |
| 139 |
conn = conn.pipeline() |
| 140 |
|
| 141 |
set_data = 10 ** (data_size - 1) |
| 142 |
for i in range(num): |
| 143 |
conn.lpush("lpush_key", set_data) |
| 144 |
if pipeline_size > 1 and i % pipeline_size == 0: |
| 145 |
conn.execute() |
| 146 |
|
| 147 |
if pipeline_size > 1: |
| 148 |
conn.execute() |
| 149 |
|
| 150 |
|
| 151 |
@timer |
| 152 |
def lrange_300(conn, num, pipeline_size, data_size): |
| 153 |
if pipeline_size > 1: |
| 154 |
conn = conn.pipeline() |
| 155 |
|
| 156 |
for i in range(num): |
| 157 |
conn.lrange("lpush_key", i, i + 300) |
| 158 |
if pipeline_size > 1 and i % pipeline_size == 0: |
| 159 |
conn.execute() |
| 160 |
|
| 161 |
if pipeline_size > 1: |
| 162 |
conn.execute() |
| 163 |
|
| 164 |
|
| 165 |
@timer |
| 166 |
def lpop(conn, num, pipeline_size, data_size): |
| 167 |
if pipeline_size > 1: |
| 168 |
conn = conn.pipeline() |
| 169 |
for i in range(num): |
| 170 |
conn.lpop("lpush_key") |
| 171 |
if pipeline_size > 1 and i % pipeline_size == 0: |
| 172 |
conn.execute() |
| 173 |
if pipeline_size > 1: |
| 174 |
conn.execute() |
| 175 |
|
| 176 |
|
| 177 |
@timer |
| 178 |
def hmset(conn, num, pipeline_size, data_size): |
| 179 |
if pipeline_size > 1: |
| 180 |
conn = conn.pipeline() |
| 181 |
|
| 182 |
set_data = {"str_value": "string", "int_value": 123456, "float_value": 123456.0} |
| 183 |
for i in range(num): |
| 184 |
conn.hmset("hmset_key", set_data) |
| 185 |
if pipeline_size > 1 and i % pipeline_size == 0: |
| 186 |
conn.execute() |
| 187 |
|
| 188 |
if pipeline_size > 1: |
| 189 |
conn.execute() |
| 190 |
|
| 191 |
|
| 192 |
if __name__ == "__main__": |
| 193 |
run() |