Skip to content

Commit fcf852e

Browse files
author
Ubuntu
committed
adding pipelines
1 parent 84028e3 commit fcf852e

File tree

2 files changed

+64
-27
lines changed

2 files changed

+64
-27
lines changed

tools/cache_testing.py

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616
load scenarios)
1717
'''
1818

19-
def rand_zipf(n, alpha, numSamples):
19+
def rand_zipf_generator(n, alpha, count, pipeline):
2020
"""
2121
n: The upper bound of the values to generate a zipfian distribution over
2222
(n = 30 would generate a distribution of given alpha from values 1 to 30)
2323
alpha: The alpha parameter to be used while creating the Zipfian distribution
2424
num_samples: The total number of samples to generate over the Zipfian distribution
25-
returns a list of num_samples values of items in the range [1, n] that follow a
26-
Zipfian distribution.
25+
This is a generator that yields up to count values using a generator.
2726
"""
2827

2928
# Calculate Zeta values from 1 to n:
@@ -33,48 +32,85 @@ def rand_zipf(n, alpha, numSamples):
3332
# Store the translation map:
3433
distMap = [x / zeta[-1] for x in zeta]
3534

36-
# Generate an array of uniform 0-1 pseudo-random values:
37-
u = np.random.random(numSamples)
35+
if pipeline == 0:
36+
# Generate an array of uniform 0-1 pseudo-random values:
37+
u = np.random.random(count)
3838

39-
# bisect them with distMap
40-
v = np.searchsorted(distMap, u)
39+
# bisect them with distMap
40+
v = np.searchsorted(distMap, u)
4141

42-
samples = [t-1 for t in v]
43-
return samples
42+
samples = [t-1 for t in v]
4443

45-
def update_stats(r, initial_hits, initial_misses, value_index, total_count):
44+
for sample in samples:
45+
yield sample
46+
else:
47+
current_count = 0
48+
while current_count < count:
49+
# Generate an array of uniform 0-1 pseudo-random values, of the pipeline length:
50+
u = np.random.random(pipeline)
51+
52+
# bisect them with distMap
53+
v = np.searchsorted(distMap, u)
54+
55+
samples = [t-1 for t in v]
56+
yield samples
57+
58+
current_count += len(samples)
59+
60+
def update_stats(r, hits, misses, value_index, total_count):
4661
"""
47-
A void function that uses terminal control sequences to update hit/miss
48-
ratio stats for the user while the testing tool runs.
62+
A void function that uses terminal control sequences
63+
to update hit/miss ratio stats for the user
64+
while the testing tool runs.
4965
"""
5066
percent_complete = (value_index + 1) / total_count
5167

5268
# Use the terminal control sequence to move the cursor to the beginning of the line
5369
print("\r", end="")
5470

55-
current_info = r.info()
56-
current_hits = current_info["keyspace_hits"]
57-
current_misses = current_info["keyspace_misses"]
58-
5971
# Print the loading bar and current hit rate
60-
print("[{}{}] {:.0f}%, current hit rate: {:.6f}%".format("#" * int(percent_complete * 20), " " * int(20 - percent_complete * 20), percent_complete * 100, (current_hits / (current_hits + current_misses)) * 100), end="")
72+
print("[{}{}] {:.0f}%, current hit rate: {:.6f}%".format("#" * int(percent_complete * 20), " " * int(20 - percent_complete * 20), percent_complete * 100, (hits / (hits + misses)) * 100), end="")
6173

6274
if __name__ == '__main__':
6375
parser = argparse.ArgumentParser(description='Cache Benchmark', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
6476
parser.add_argument('-c', '--count', type=int, default=100000, help='total number of incrby operations')
6577
parser.add_argument('-u', '--uri', type=str, default='redis://localhost:6379', help='Redis server URI')
6678
parser.add_argument('-a', '--alpha', type=int, default=1.0, help='alpha value being used for the Zipf distribution')
6779
parser.add_argument('-n', '--number', type=int, default=30, help='the number of values to be used in the distribution')
80+
parser.add_argument('-d', '--length', type=int, default=10, help='the length of the values to be used in the distribution')
81+
parser.add_argument('-p', '--pipeline', type=int, default=0, help='pipeline size')
82+
6883
args = parser.parse_args()
6984
uri = urlparse(args.uri)
7085

71-
r = redis.Redis(host=uri.hostname, port=uri.port)
72-
73-
initial_hits = r.info()['keyspace_hits']
74-
initial_misses = r.info()['keyspace_misses']
75-
76-
distribution_values = rand_zipf(args.number, args.alpha, args.count)
77-
for idx, val in enumerate(distribution_values):
78-
r.incrby(str(val), 1)
79-
if idx % 50 == 0:
80-
update_stats(r, initial_hits, initial_misses, idx, args.count)
86+
r = redis.StrictRedis(host=uri.hostname, port=uri.port)
87+
88+
misses = 0
89+
hits = 0
90+
91+
distribution_values_generator = rand_zipf_generator(args.number, args.alpha, args.count, args.pipeline)
92+
93+
if args.pipeline == 0:
94+
for idx, val in enumerate(distribution_values_generator):
95+
result = r.set(str(val), 'x' * args.length, nx=True)
96+
if result:
97+
misses += 1
98+
else:
99+
hits += 1
100+
if idx % 50 == 0:
101+
update_stats(r, hits, misses, idx, args.count)
102+
else:
103+
total_count = 0
104+
for idx, values in enumerate(distribution_values_generator):
105+
total_count += len(values)
106+
p = r.pipeline()
107+
for val in values:
108+
p.set(str(val), 'x' * args.length, nx=True)
109+
responses = p.execute()
110+
for resp in responses:
111+
if resp:
112+
misses += 1
113+
else:
114+
hits += 1
115+
if idx % 20 == 0:
116+
update_stats(r, hits, misses, total_count, args.count)

tools/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ redis==4.3.4
77
requests==2.28.1
88
aiocsv==1.2.3
99
aiofiles==22.1.0
10+
numpy==1.24.1

0 commit comments

Comments
 (0)