-
Notifications
You must be signed in to change notification settings - Fork 9
/
loader
executable file
·387 lines (346 loc) · 12.7 KB
/
loader
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/env pypy3
#
# ViaQ logging load generator
#
# Copyright 2021 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import sys
import os
import time
import uuid
import random
import string
import logging
import logging.handlers
import argparse
import math
DEFAULT_STDDEV = 32
REPORT_INTERVAL = 10
def msgsizgen_fixed(stats, mean, stddev=None):
'''Generate a series of "fixed" sizes, always yielding the given
"mean" as the value.
The stddev value is always ignored.
'''
while True:
yield stats.nextseq(mean)
def msgsizgen_gaussian(stats, mean, stddev=DEFAULT_STDDEV):
'''Generate a series of random numbers with a Guassian distribution,
where the mean and standard deviation are given arguments.
The mean is a required parameter, while the stddev will default to
32.
The generator will yield numbers continuously.
'''
randomizer = random.Random()
while True:
yield stats.nextseq(int(round(randomizer.gauss(mean, stddev))))
dist_methods = {
'fixed': msgsizgen_fixed,
'gaussian': msgsizgen_gaussian,
'normal': msgsizgen_gaussian
}
class StatsCtx(object):
def __init__(self, msgpersec, rptint=1):
self.start = self.past = time.time()
self.seq = 0
self.msgs = 0
self.total_size = 0
self.skipped_periods = 0
self.skipped_report_periods = 0
self.msgpersec = msgpersec
if msgpersec > 0:
self.msgperiod = (1/msgpersec, self.start + 1/msgpersec)
else:
self.msgperiod = None
self.rptperiod = (rptint, self.start + rptint)
def nextseq(self, size):
now = time.time()
self.seq += 1
self.msgs += 1
self.total_size += size
if self.msgperiod is not None:
while now < self.msgperiod[1]:
sleep_window = self.msgperiod[1] - now
time.sleep(sleep_window)
now = time.time()
# We have exited our rate-limit window, calculate next
# window and continue
assert now >= self.msgperiod[1], (
f"Logic bomb! {now} < {self.msgperiod[1]}"
)
# Determine if we are in the immmediate next period,
# or how many periods we have skipped.
prev_period_end = self.msgperiod[1]
period_dist = now - prev_period_end
period_cnt = int(math.floor(period_dist / self.msgperiod[0]))
self.skipped_periods += period_cnt
self.msgperiod = (
self.msgperiod[0], prev_period_end + (
(period_cnt + 1) * self.msgperiod[0]
)
)
assert (self.msgperiod[1] - now) <= self.msgperiod[0], (
f"Period calc logic bomb: {prev_period_end}, {now},"
f" {period_dist!r}, {period_cnt!r}, {self.msgperiod[0]}"
)
if now >= self.rptperiod[1]:
prev_period_end = self.rptperiod[1]
period_dist = now - prev_period_end
period_cnt = int(math.floor(period_dist / self.rptperiod[0]))
self.skipped_periods += period_cnt
self.rptperiod = (
self.rptperiod[0], prev_period_end + (
(period_cnt + 1) * self.rptperiod[0]
)
)
assert (self.rptperiod[1] - now) <= self.rptperiod[0], (
f"Report period calc logic bomb: {prev_period_end}, {now},"
f" {period_dist!r}, {period_cnt!r}, {self.rptperiod[0]}"
)
report = True
else:
report = False
return size, self.seq, report
def statstr(self, seq_width):
now = time.time()
msg_rate = self.msgs / (now - self.past)
total_msg_rate = self.seq / (now - self.start)
if self.msgperiod is not None:
if msg_rate < (self.msgpersec - 0.50):
notice = " (too slow, %d)" % self.msgpersec
elif msg_rate > (self.msgpersec + 0.50):
notice = " (too fast, %d)" % self.msgpersec
else:
notice = ""
else:
notice = ""
self.past = now
self.msgs = 0
return "%.3fs %0*d %10.3f%s %10.3f %d %d" % (
now, seq_width, self.seq, msg_rate, notice, total_msg_rate,
self.skipped_periods, self.skipped_report_periods
)
chars = string.ascii_lowercase + string.digits
def payload_random(size):
return ''.join(random.choice(chars) for x in range(size))
def payload_fixed(size):
return 'x' * size
gen_methods = {'fixed': payload_fixed, 'random': payload_random}
def load(
invocid, size, output_method, report_method, dist='gaussian',
stddev=DEFAULT_STDDEV, report_interval=10, seq_width=10, msgpersec=0,
total_size=0, payload_gen='random'
):
'''Emit a formatted payload of random bytes, using a Gaussian
distribution using a given size as the mean. The payload
is emitted using the output_method function, while statistics
about the output rate are reported through the report_method.
The reports are generated once every 10 secs, no more than
once a second by default. Both parameters can be changed by
the caller.
The generated sequence number defaults to a width of 10.
The invocation ID, invocid, is required from the caller to
facilitate finding the output emitted by this instance.
'''
msgsizgen = dist_methods[dist]
payloadgen = gen_methods[payload_gen]
sep = ' - '
sep_len = len(sep)
prefix = "loader seq - %s - " % invocid
prefix_len = len(prefix)
sub_len = (prefix_len + seq_width + sep_len)
stats = StatsCtx(msgpersec, report_interval)
for asize, seq, report in msgsizgen(stats, size, stddev):
asize -= sub_len
if (
report_method is None and (
report or (
total_size > 0 and (
stats.total_size > total_size * (1024 * 1024)
)
)
)
):
# Reporting is done inline as part of a sequence message payload.
stats_msg = "(stats: %s) " % stats.statstr(seq_width)
stats_msg_len = len(stats_msg)
else:
stats_msg = ""
stats_msg_len = 0
asize -= stats_msg_len
asize = 1 if asize <= 0 else asize
msg = stats_msg + payloadgen(asize)
output_method("%s%0*d%s%s" % (prefix, seq_width, seq, sep, msg))
if report_method is not None and report:
# Reporting is done out-of-band.
report_method("loader stat: %s" % stats.statstr(seq_width))
if total_size > 0 and (stats.total_size > total_size * (1024 * 1024)):
# We're done
if report_method is not None and not report:
# Reporting is done out-of-band.
report_method("loader stat: %s" % stats.statstr(seq_width))
break
if __name__ == '__main__':
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(module)s.%(funcName)s: %(message)s')
try:
handler = logging.handlers.SysLogHandler(address='/dev/log')
handler.setFormatter(formatter)
except Exception as e:
print(
"*** Warning *** - could not open /dev/log, falling back to"
f" console handler for logger ... (exception: {e})",
file=sys.stderr
)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(formatter)
log.addHandler(consoleHandler)
else:
log.addHandler(handler)
parser = argparse.ArgumentParser(description='Message payload generator.')
parser.add_argument(
'payload_size', metavar='SIZE', type=int, nargs='?',
default=1024,
help=(
'an integer specifying the mean size of the payload using a'
' Gaussian distribution'
)
)
parser.add_argument(
'--distribution', metavar='DIST', dest='payload_dist', action='store',
default='gaussian', choices=dist_methods,
help=(
'the size distribution to use, e.g. "gaussian" (default),'
' "normal" (alias for gaussian), or "fixed"'
)
)
parser.add_argument(
'--payload-gen', metavar='GEN', dest='payload_gen', action='store',
default='random', choices=gen_methods,
help=(
'the payload generator to use, e.g. "random" (default), or "fixed"'
)
)
parser.add_argument(
'--invocid', metavar='INVOCID', dest='invocid', action='store',
default=uuid.uuid4().hex,
help=(
'the unique invocation ID string to use (defaults to 32 char'
' generated one)'
)
)
parser.add_argument(
'--stddev', metavar='STDDEV', dest='stddev', type=float,
default=DEFAULT_STDDEV,
help=(
'the standard deviation to use with a random distribution'
f' (defaults to {DEFAULT_STDDEV})'
)
)
parser.add_argument(
'--output', metavar='METHOD', dest='output', action='store',
default='stdout',
help=(
'where to emit the output, one of: "stdout", "stderr", "syslog",'
' "<file name>" (defaults to "stdout")'
)
)
parser.add_argument(
'--report', metavar='METHOD', dest='report', action='store',
default='inline',
help=(
'where to emit the report output, one of: "inline", "stdout",'
' "stderr", "syslog", "<file name>" (defaults to "inline")'
)
)
parser.add_argument(
'--msgpersec', metavar='MSGPERSEC', dest='msgpersec', type=float,
default=0,
help=(
'the # of logs per second (floating point) to emit (defaults to'
' 0, unlimited)'
)
)
parser.add_argument(
'--report-interval', metavar='INTERVAL', dest='reportint', type=int,
default=REPORT_INTERVAL,
help=(
'the # of seconds between reports'
f' (defaults to {REPORT_INTERVAL} secs)'
)
)
parser.add_argument(
'--total-size', metavar='TOTSIZE', dest='totalsize', type=int,
default=0,
help=(
'the total # of megabytes to generate before ending'
' (defaults to 0, unlimited)'
)
)
args = parser.parse_args()
# Determine the output method to emit logs
if args.output == 'syslog':
output_method = log.debug
ofp = None
else:
if args.output in ('stderr', 'stdout'):
ofp = getattr(sys, args.output)
ofp = os.fdopen(ofp.fileno(), mode="w", buffering=65536)
else:
# Assume the parameter is a file name
ofp = open(args.output, "w", buffering=65536)
def output_closure(payload):
ret = ofp.write(payload + '\n')
ofp.flush()
assert ret == len(payload) + 1, (
f"ofp.write(payload + '\n') return {ret!r},"
f" expected {len(payload) + 1}"
)
output_method = output_closure
# Determine the report method to use
if args.report == 'inline':
report_method = None
rfp = None
elif args.report == 'syslog':
report_method = log.info
rfp = None
else:
if args.report in ('stderr', 'stdout'):
rfp = getattr(sys, args.report)
else:
# Assume the parameter is a file name
rfp = open(args.report, "w")
def report_closure(payload):
ret = rfp.write(payload + '\n')
rfp.flush()
assert ret == len(payload) + 1, (
f"rfp.write(payload + '\n') return {ret!r},"
f" expected {len(payload) + 1}"
)
report_method = report_closure
try:
load(
args.invocid, args.payload_size, output_method, report_method,
dist=args.payload_dist, stddev=args.stddev,
msgpersec=args.msgpersec, report_interval=args.reportint,
total_size=args.totalsize, payload_gen=args.payload_gen
)
except KeyboardInterrupt:
if ofp:
ofp.flush()
ofp.close()
if rfp:
rfp.flush()
rfp.close()