-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathceilorunner.py
executable file
·191 lines (154 loc) · 5.95 KB
/
ceilorunner.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
#!/usr/bin/env python
from ceilometerclient.shell import CeilometerShell
from ceilometerclient import client as ceiloclient
from ceilometerclient.v2 import options
import threading
import argparse
import sys
import os
import time
import pdb
"""
Replace functions will call the non-printing version of functions
Add to prevent seeing ton of out put on cm shell functions
Name them as same as the shell functions
"""
def do_meter_list(client, args):
return client.meters.list(q=options.cli_to_array(args.query))
def do_resource_list(client, args):
return client.resources.list(q=options.cli_to_array(args.query))
def do_resource_show(client, args):
return client.resources.get(args.resource_id)
def do_alarm_list(client, args):
return client.alarms.list(q=options.cli_to_array(args.query))
def do_statistics(client, args):
'''List the statistics for a meter.'''
aggregates = []
for a in args.aggregate:
aggregates.append(dict(zip(('func', 'param'), a.split("<-"))))
api_args = {'meter_name': args.meter,
'q': options.cli_to_array(args.query),
'period': args.period,
'groupby': args.groupby,
'aggregates': aggregates}
return client.statistics.list(**api_args)
def do_sample_list(client, args):
fields = {'meter_name': args.meter,
'q': options.cli_to_array(args.query),
'limit': args.limit}
return client.samples.list(**fields)
"""
Uses the env if no authentication args are given
"""
def get_ceilometer_api_client(args):
s = CeilometerShell()
ret_value = s.parse_args(args)
if isinstance(ret_value, tuple):
api_version, parsed_args = s.parse_args(args)
return parsed_args, ceiloclient.get_client(api_version, **(parsed_args.__dict__))
return None, None
def parse_args(args):
arg_parser = argparse.ArgumentParser(
prog='ceilometer',
)
arg_parser.add_argument('--num-threads', type=int, default=1)
arg_parser.add_argument('--num-iterations', type=int, default=1)
arg_parser.add_argument('--print-stats', action='store_true', help="Prints per thread stats")
#arg_parser.add_argument('--input-file', default="~/.inputceilorunner")
return arg_parser.parse_known_args()
def enable_output(self):
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
def disable_output(self):
sys.stdout = os.devnull
sys.stderr = os.devnull
class CeiloCommandThread(threading.Thread):
def __init__(self, num_iterations, client, func, args):
threading.Thread.__init__(self, name=func.__name__)
self.num_iterations = num_iterations
self.client = client
self.func = self.get_function_to_call(func)
self.args = args
self.run_times = []
self.error_flag = False
self.error_str = ''
@property
def avg(self):
if self.run_times:
return sum(self.run_times) / len(self.run_times)
else:
return 0.0
@property
def sum(self):
if self.run_times:
return sum(self.run_times)
else:
return 0.0
@property
def min(self):
if self.run_times:
return min(self.run_times)
else:
return 0.0
@property
def max(self):
if self.run_times:
return max(self.run_times)
else:
return 0.0
@property
def error(self):
return self.error_str if self.error_flag else ''
def print_stats(self):
print "Function %s took %f sec (avg=%f, min=%f, max=%f) for %d iterations (%s)" \
% (self.name, self.sum, self.avg, self.min, self.max, len(self.run_times), self.error)
def print_curr_stats(self, curr_time, bytesize):
print "id, fname, curr_time, bytesize, curr_avg, s: %d, %s, %f, %d, %f, %f" % (self.ident, self.name, curr_time, bytesize, self.avg, time.time())
def get_function_to_call(self, shell_func):
replace_function = globals().get(shell_func.__name__, None)
return replace_function if replace_function else shell_func
def run(self):
try:
self.run_times = []
for _ in range(self.num_iterations):
t0 = time.time()
res = None
try:
res = self.func(self.client, self.args)
finally:
t1 = time.time()
self.run_times.append(t1 - t0)
self.print_curr_stats(t1 - t0, sys.getsizeof(res) if res else 0)
except Exception as e:
self.error_flag = True
self.error_str = "error occured in thread %s" % str(e)
def main(args=None):
try:
if args is None:
args = sys.argv[1:]
local_args, ceilo_args = parse_args(args)
ceilo_client_args, client = get_ceilometer_api_client(ceilo_args)
if ceilo_client_args:
threads = []
for _ in range(local_args.num_threads):
t = CeiloCommandThread(local_args.num_iterations,
client, ceilo_client_args.func, ceilo_client_args)
threads.append(t)
#t.run()
t.start()
total_runtimes = []
for i, t in enumerate(threads):
t.join()
if local_args.print_stats or t.error_flag:
t.print_stats()
total_runtimes = total_runtimes + t.run_times
gt_60 = sum(1 for t in threads if t.max > 60.0)
print "Threads that have an iteration that ran greater than 60 secs", gt_60
print "num iter / thread, numthreads, ave, min, max = %d\t%d\t%f\t%f\t%f" % \
(local_args.num_iterations, len(threads),
sum(total_runtimes)/len(total_runtimes), min(total_runtimes), max(total_runtimes))
except Exception as e:
print "CeiloRunner: Unknown error ", str(e)
raise
if __name__ == "__main__":
main()