This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
forked from bslatkin/ringbuffer
-
Notifications
You must be signed in to change notification settings - Fork 3
/
perf_test_ringbuffer.py
executable file
·280 lines (205 loc) · 7.51 KB
/
perf_test_ringbuffer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python3
"""Performance test of the RingBuffer class."""
import argparse
import collections
import cProfile
import ctypes
import functools
import logging
import multiprocessing
import os
import pstats
import random
import struct
import time
import zlib
import ringbuffer
FLAGS = argparse.ArgumentParser()
FLAGS.add_argument('--debug', action='store_true')
FLAGS.add_argument(
'--duration-seconds', action='store', type=int, required=True)
FLAGS.add_argument('--slots', action='store', type=int, required=True)
FLAGS.add_argument('--slot-bytes', action='store', type=int, required=True)
FLAGS.add_argument('--readers', action='store', type=int, required=True)
FLAGS.add_argument(
'--reader-burn-cpu-milliseconds', action='store', type=int, default=0)
FLAGS.add_argument(
'--writes-per-second', action='store', type=int, required=True)
FLAGS.add_argument(
'--verify_writes', action='store_true', dest='verify_writes')
FLAGS.add_argument(
'--no-verify_writes', action='store_false', dest='verify_writes')
FLAGS.set_defaults(verify_writes=True)
def profile(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
profiler = cProfile.Profile()
try:
return profiler.runcall(func, *args, **kwargs)
finally:
stats = pstats.Stats(profiler)
stats.strip_dirs()
stats.sort_stats('tottime')
stats.print_stats()
return wrapper
def sleep_generator(duration_seconds, writes_per_second):
start = time.time()
end = start + duration_seconds
target_duration = 1 / writes_per_second
seen_durations = collections.deque(maxlen=10)
while True:
before = time.time()
if before >= end:
return
yield
after = time.time()
last_duration = after - before
# Keep an average duration to better approximate the processing
# time and keep the sleep time stable. Otherwise the variability will
# cause this method to oversleep.
seen_durations.append(last_duration)
avg_duration = sum(seen_durations) / len(seen_durations)
next_delay = target_duration - avg_duration
if next_delay > 0:
time.sleep(next_delay)
# Using a memoryview prevents copying when random_data slices this value.
_CACHED_RANDOM_DATA = memoryview(100 * os.urandom(10 * 10**6))
def get_random_data(num_bytes):
"""Generate random input data from cached randomness.
We do this because os.urandom can be very slow and that's not what this
code is trying to load test.
"""
index = random.randint(0, len(_CACHED_RANDOM_DATA) - num_bytes)
t = ctypes.c_byte * num_bytes
return t.from_buffer_copy(_CACHED_RANDOM_DATA[index:index + num_bytes])
def get_random_data_bytes(num_bytes):
"""Generate random input data from cached randomness.
We do this because os.urandom can be very slow and that's not what this
code is trying to load test.
"""
index = random.randint(0, len(_CACHED_RANDOM_DATA) - num_bytes)
return _CACHED_RANDOM_DATA[index:index + num_bytes]
def get_crc32(data):
return zlib.crc32(data) & 0xffffffff
def generate_verifiable_data(num_bytes):
random_size = num_bytes - 4
random_data = get_random_data_bytes(random_size)
crc = get_crc32(random_data)
result = bytearray(num_bytes)
result[:random_size] = random_data
struct.pack_into('>I', result, random_size, crc)
t = ctypes.c_byte * num_bytes
return t.from_buffer_copy(result)
def verify_data(data):
random_size = len(data) - 4
random_data = data[:random_size]
found_crc = get_crc32(random_data)
(expected_crc, ) = struct.unpack_from('>I', data, random_size)
assert expected_crc == found_crc, 'Expected crc %r, found crc %r' % (
expected_crc, found_crc)
class Timing:
def __init__(self, now=time.time):
self.now = now
self.start = None
self.end = None
self.duration = None
def __enter__(self):
self.start = self.now()
return self
def __exit__(self, *args, **kwargs):
self.end = self.now()
self.duration = self.end - self.start
return False
_PRINT_LOCK = multiprocessing.Lock()
def print_process_stats(process, flags, slots, elapsed):
slots_per_second = slots / elapsed.duration
mb_per_second = flags.slot_bytes * slots_per_second / 1e6
delta = slots_per_second - flags.writes_per_second
percent_wrong = 100 * delta / flags.writes_per_second
message = """%(action)s
%(slots)d slots in %(duration)f seconds
%(bandwidth)f MBytes per second
%(rate)f slots/second, %(delta).1f%% relative to target
""" % {
'action': process,
'slots': slots,
'duration': elapsed.duration,
'rate': slots_per_second,
'bandwidth': mb_per_second,
'delta': percent_wrong,
}
with _PRINT_LOCK:
print(message)
# @profile
def writer(flags, out_ring):
print_every = flags.writes_per_second
with Timing() as elapsed:
it = sleep_generator(flags.duration_seconds, flags.writes_per_second)
writes = 0
for _ in it:
if flags.verify_writes:
data = generate_verifiable_data(flags.slot_bytes)
else:
data = get_random_data(flags.slot_bytes)
try:
out_ring.try_write(data)
except ringbuffer.WaitingForReaderError:
logging.error('Write %d is waiting for readers', writes)
continue
writes += 1
if writes and writes % print_every == 0:
logging.debug('Wrote %d slots so far', writes)
out_ring.writer_done()
logging.debug('Exiting writer')
print_process_stats('Writer', flags, writes, elapsed)
def burn_cpu(milliseconds):
start = now = time.time()
end = start + milliseconds / 1000
while True:
now = time.time()
if now >= end:
break
for i in range(100):
random.random()**1 / 2
# @profile
def reader(flags, in_ring, reader):
print_every = flags.writes_per_second
reads = 0
with Timing() as elapsed:
while True:
try:
data = in_ring.blocking_read(reader)
except ringbuffer.WriterFinishedError:
break
if flags.verify_writes:
byte_data = bytearray(data[0])
verify_data(byte_data)
if flags.reader_burn_cpu_milliseconds:
burn_cpu(flags.reader_burn_cpu_milliseconds)
reads += 1
if reads % print_every == 0:
logging.info('%r read %d slots so far', reader, reads)
logging.debug('Exiting reader %r', reader)
print_process_stats('Reader %d' % id(reader), flags, reads, elapsed)
def get_buffer(flags):
t = ctypes.c_byte * flags.slot_bytes
return ringbuffer.RingBuffer(c_type=t, slot_count=flags.slots)
def main():
flags = FLAGS.parse_args()
print('Starting performance test with flags: %r' % flags)
if flags.debug:
logging.getLogger().setLevel(logging.DEBUG)
ring = get_buffer(flags)
ring.new_writer()
processes = [multiprocessing.Process(target=writer, args=(flags, ring))]
for i in range(flags.readers):
processes.append(
multiprocessing.Process(
target=reader, args=(flags, ring, ring.new_reader())))
for process in processes:
process.start()
for process in processes:
process.join()
assert process.exitcode == 0
if __name__ == '__main__':
main()