forked from severb/graypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perftest.py
executable file
·101 lines (89 loc) · 3.18 KB
/
perftest.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
#! /usr/bin/env python
import argparse
import logging
import logging.config
import sys
import time
def main(argv=sys.argv):
parser = argparse.ArgumentParser(prog="perftest.py")
parser.add_argument('--graylog-host',
help='Graylog2 host. Do not test GELFHandler if not specified.')
parser.add_argument('--graylog-port', type=int, default=12201,
help='Graylog2 GELF UDP port. Default: 12201')
parser.add_argument('--rabbit-url',
help='RabbitMQ url (ex: amqp://guest:guest@localhost/). '
'Do not test GELFRabbitHandler if not specified.')
parser.add_argument('--rabbit-exchange', default='logging.gelf',
help='RabbitMQ exchange. Default: logging.gelf')
parser.add_argument('--console-logger', action='store_true', default=None)
parser.add_argument('--stress', action='store_true',
help='Enable performance/stress test. WARNING this logs MANY warnings.')
args = parser.parse_args(argv[1:])
if all(v is None for v in
[args.graylog_host, args.rabbit_url, args.console_logger]):
parser.print_help()
config = {
'version': 1,
'formatters': {
'brief': {'format': "%(levelname)-7s %(name)s - %(message)s"},
'message': {'format': "%(message)s"},
},
'handlers': {},
'root': {'handlers': [], 'level': 'DEBUG'},
'disable_existing_loggers': False,
}
if args.graylog_host is not None:
config['handlers']['graylog_udp'] = {
'class': 'graypy.GELFHandler',
'host': args.graylog_host,
'port': args.graylog_port,
'debugging_fields': 0,
'formatter': 'message',
}
config['root']['handlers'].append('graylog_udp')
if args.rabbit_url is not None:
config['handlers']['graylog_rabbit'] = {
'class': 'graypy.GELFRabbitHandler',
'url': args.rabbit_url,
'exchange': args.rabbit_exchange,
'debugging_fields': 0,
'formatter': 'message',
}
config['root']['handlers'].append('graylog_rabbit')
if args.console_logger:
config['handlers']['console'] = {
'class': 'logging.StreamHandler',
'formatter': 'brief',
}
config['root']['handlers'].append('console')
logging.config.dictConfig(config)
log = logging.getLogger()
t_start = time.time()
total = 0
log.debug('debug')
log.info('info')
log.warn('warning')
log.error('error')
log.critical('critical')
total += 5
if args.stress:
t_end = time.time() + 10
tx = t_end - 9
cx = 0
while True:
log.warn('warning')
cx += 1
total += 1
if time.time() > tx:
elapsed = time.time() - (tx - 1)
tx += 1
print('%s messages in %.3f seconds (%.3f msg/s)'
% (cx, elapsed, cx / elapsed))
cx = 0
if tx > t_end:
break
elapsed = time.time() - t_start
print('%s messages in %.3f seconds (%.3f msg/s)'
% (total, elapsed, total / elapsed))
if __name__ == '__main__':
main()