-
Notifications
You must be signed in to change notification settings - Fork 658
/
Copy pathget_metrics
executable file
·282 lines (230 loc) · 10.5 KB
/
get_metrics
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
#! /usr/bin/env python3
"""
Convenience script for gathering metrics data
"""
import argparse
import logging
import os
import shutil
from abc import ABC, abstractmethod
from collections import namedtuple
from datetime import datetime, timedelta
from indy_common.config_util import getConfig
from plenum.common.constants import KeyValueStorageType
from plenum.common.metrics_collector import MetricsName
from plenum.common.metrics_stats import load_metrics_from_kv_store
from storage.helper import initKeyValueStorage
logging.root.handlers = []
logger = logging.getLogger()
logger.propagate = False
logger.disabled = True
def read_args():
parser = argparse.ArgumentParser(description="Process metrics")
parser.add_argument('--data_dir', required=False, help="Path to metrics data, replaces a")
parser.add_argument('--node_name', required=False, help="Node's name")
parser.add_argument('--network', required=False, help="Network name to read metrics from")
parser.add_argument('--output', required=False, default=None, help="Output CSV file name with metrics")
parser.add_argument('--ts_fmt', required=False, default="%Y-%m-%d %H:%M:%S", help="Output timestamp format")
parser.add_argument(
'--min_ts',
required=False,
default=None,
help="Process all metrics starting from given timestamp (beginning by default)")
parser.add_argument(
'--max_ts',
required=False,
default=None,
help="Process all metrics till given timestamp (end by default)")
parser.add_argument(
'--step',
type=int,
required=False,
default=60,
help="Build timelog with given timestep, seconds (60 by default)"
)
return parser.parse_args()
def get_data_dir(node_name=None, network=None):
config = getConfig()
_network = network if network else config.NETWORK_NAME
data_dir = os.path.join(config.LEDGER_DIR, _network, 'data')
if not os.path.exists(data_dir):
print("No such file or directory: {}".format(data_dir))
print("Please check, that network: '{}' was used ".format(_network))
exit()
if not node_name:
dirs = os.listdir(data_dir)
if len(dirs) == 0:
print("Node's 'data' folder not found: {}".format(data_dir))
exit()
node_name = dirs[0]
return os.path.join(data_dir, node_name)
def make_copy_of_data(data_dir):
read_copy_data_dir = data_dir + '-read-copy'
if os.path.exists(read_copy_data_dir):
shutil.rmtree(read_copy_data_dir)
shutil.copytree(data_dir, read_copy_data_dir)
return read_copy_data_dir
class Column(ABC):
Context = namedtuple('Context', 'args stats')
Data = namedtuple('Data', 'ts frame')
def __init__(self, title):
self.title = title
@abstractmethod
def value(self, context: Context, data: Data):
pass
class Timestamp(Column):
def __init__(self):
super().__init__("timestamp")
def value(self, context, data):
return data.ts.replace(microsecond=0).strftime(context.args.ts_fmt)
class MetricsValue(Column):
def __init__(self, name: MetricsName, param: str = 'avg'):
super().__init__(self._gen_title(name, param))
self._name = name
self._param = param
def value(self, context, data):
acc = data.frame.get(self._name)
result = getattr(acc, self._param)
if not result:
return 0
if self._param in ['sum', 'count']:
result /= context.stats.timestep.total_seconds()
return result
@staticmethod
def _gen_title(name: MetricsName, param: str):
title = str(name).split('.')[-1].lower()
if param == 'sum':
return "{}_per_sec".format(title)
if param == 'count':
return "{}_count_per_sec".format(title)
return "{}_{}".format(param, title)
def process_storage(storage, args):
fmt = "%Y-%m-%d %H:%M:%S"
min_ts = datetime.strptime(args.min_ts, fmt) if args.min_ts else None
max_ts = datetime.strptime(args.max_ts, fmt) if args.max_ts else None
stats = load_metrics_from_kv_store(storage, min_ts, max_ts, timedelta(seconds=args.step))
print("Start time: {}".format(stats.min_ts))
print("End time: {}".format(stats.max_ts))
print("Duration: {}".format(stats.max_ts - stats.min_ts))
print("")
total = stats.total
print("Number of messages processed in one looper run:")
print(" Node: {}".format(total.get(MetricsName.NODE_STACK_MESSAGES_PROCESSED).to_str()))
print(" Client: {}".format(total.get(MetricsName.CLIENT_STACK_MESSAGES_PROCESSED).to_str()))
print("")
print("Seconds passed between looper runs:")
print(" {}".format(total.get(MetricsName.LOOPER_RUN_TIME_SPENT).to_str()))
print("")
print("Number of messages in one transport batch:")
print(" {}".format(total.get(MetricsName.TRANSPORT_BATCH_SIZE).to_str()))
print("")
print("Node message size, bytes:")
print(" Outgoing: {}".format(total.get(MetricsName.OUTGOING_NODE_MESSAGE_SIZE).to_str()))
print(" Incoming: {}".format(total.get(MetricsName.INCOMING_NODE_MESSAGE_SIZE).to_str()))
print("")
print("Client message size, bytes:")
print(" Outgoing: {}".format(total.get(MetricsName.OUTGOING_CLIENT_MESSAGE_SIZE).to_str()))
print(" Incoming: {}".format(total.get(MetricsName.INCOMING_CLIENT_MESSAGE_SIZE).to_str()))
print("")
print("Number of requests in one 3PC batch:")
print(" Created: {}".format(total.get(MetricsName.THREE_PC_BATCH_SIZE).to_str()))
print(" Ordered: {}".format(total.get(MetricsName.ORDERED_BATCH_SIZE).to_str()))
print("")
print("Time spent on write request processing, ms:")
print(" {}".format(total.get(MetricsName.REQUEST_PROCESSING_TIME).
to_str(show_sums=False, stats_mul=1000.0, stats_unit="ms")))
print("")
print("Monitor throughput, TPS")
print(" Master: {}".format(total.get(MetricsName.MONITOR_AVG_THROUGHPUT).to_str(show_sums=False)))
print(" Backup: {}".format(total.get(MetricsName.BACKUP_MONITOR_AVG_THROUGHPUT).to_str(show_sums=False)))
print("")
print("Monitor latency, seconds")
print(" Master: {}".format(total.get(MetricsName.MONITOR_AVG_LATENCY).to_str(show_sums=False)))
print(" Backup: {}".format(total.get(MetricsName.BACKUP_MONITOR_AVG_LATENCY).to_str(show_sums=False)))
print("")
print("RAM info:")
print(" Available, Mb: {}".format(
total.get(MetricsName.AVAILABLE_RAM_SIZE).to_str(show_sums=False, stats_mul=1.0 / 1024 / 1024)))
print(" Node RSS, Mb: {}".format(
total.get(MetricsName.NODE_RSS_SIZE).to_str(show_sums=False, stats_mul=1.0 / 1024 / 1024)))
print(" Node VMS, Mb: {}".format(
total.get(MetricsName.NODE_VMS_SIZE).to_str(show_sums=False, stats_mul=1.0 / 1024 / 1024)))
print("")
print("Additional statistics:")
three_pc = total.get(MetricsName.ORDERED_BATCH_SIZE)
node_in = total.get(MetricsName.INCOMING_NODE_MESSAGE_SIZE)
node_out = total.get(MetricsName.OUTGOING_NODE_MESSAGE_SIZE)
client_in = total.get(MetricsName.INCOMING_CLIENT_MESSAGE_SIZE)
client_out = total.get(MetricsName.OUTGOING_CLIENT_MESSAGE_SIZE)
node_traffic = node_in.sum + node_out.sum
client_traffic = (client_in.sum + client_out.sum)
print(" Client incoming/outgoing: {:.2f} messages, {:.2f} traffic"
.format(client_in.count / client_out.count, client_in.sum / client_out.sum if client_out.sum > 0 else 0))
print(" Node incoming/outgoing traffic: {:.2f}".format(node_in.sum / node_out.sum if node_out.sum > 0 else 0))
print(" Node/client traffic: {:.2f}".format(node_traffic / client_traffic))
if three_pc.count > 0:
print(" Node traffic per batch: {:.2f}".format(node_traffic / three_pc.count))
print(" Node traffic per request: {:.2f}".format(node_traffic / three_pc.sum))
print("")
print("Profiling info:")
for m in MetricsName:
if m > 30000:
break
if m < MetricsName.NODE_PROD_TIME and \
m not in {MetricsName.REQUEST_PROCESSING_TIME,
MetricsName.BACKUP_REQUEST_PROCESSING_TIME,
MetricsName.GC_GEN0_TIME,
MetricsName.GC_GEN1_TIME,
MetricsName.GC_GEN2_TIME}:
continue
acc = total.get(m)
print(" {} : {}".format(str(m).split('.')[-1],
acc.to_str(sums_unit='seconds', stats_unit='ms', stats_mul=1000)))
if args.output is not None:
columns = [Timestamp()]
for m in MetricsName:
if m < 30000:
columns.append(MetricsValue(m, 'sum'))
columns.append(MetricsValue(m, 'count'))
columns.append(MetricsValue(m, 'min'))
columns.append(MetricsValue(m, 'lo'))
columns.append(MetricsValue(m, 'avg'))
columns.append(MetricsValue(m, 'hi'))
columns.append(MetricsValue(m, 'max'))
with open(args.output, 'w') as f:
f.write(",".join(c.title for c in columns))
f.write("\n")
context = Column.Context(args=args, stats=stats)
for ts, frame in sorted(stats.frames(), key=lambda v: v[0]):
data = Column.Data(ts=ts, frame=frame)
f.write(",".join(str(c.value(context, data)) for c in columns))
f.write("\n")
def detect_storage_type(path):
if os.path.isfile('{}.bin'.format(path)):
return KeyValueStorageType.BinaryFile
if any(f.endswith('.ldb') for f in os.listdir(path)):
return KeyValueStorageType.Leveldb
return KeyValueStorageType.Rocksdb
if __name__ == '__main__':
args = read_args()
if args.data_dir is not None:
storage_type = detect_storage_type(args.data_dir)
location, name = os.path.split(args.data_dir)
storage = initKeyValueStorage(storage_type, location, name)
process_storage(storage, args)
exit()
data_dir = get_data_dir(args.node_name, args.network)
data_is_copied = False
try:
config = getConfig()
if config.METRICS_KV_STORAGE != KeyValueStorageType.Rocksdb:
data_dir = make_copy_of_data(data_dir)
data_is_copied = True
storage = initKeyValueStorage(
config.METRICS_KV_STORAGE,
data_dir,
config.METRICS_KV_DB_NAME)
process_storage(storage, args)
finally:
if data_is_copied:
shutil.rmtree(data_dir)