-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric.py
146 lines (103 loc) · 3.88 KB
/
metric.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
import time
class MetricCollection(object):
def __init__(self):
self.metrics = []
def append(self, metric):
self.metrics.append(metric)
def clear(self):
self.metrics = []
def __str__(self):
return "\n".join(str(m) for m in self.metrics)
class Metric(object):
def __init__(self, measurement):
self.measurement = measurement
self.values = {}
self.tags = dict()
self.timestamp = None
def set_values(self, data: dict):
self.values = data
def with_timestamp(self, timestamp):
self.timestamp = timestamp
def add_tag(self, name, value):
self.tags[str(name)] = str(value)
def add_value(self, name, value):
self.values[str(name)] = value
def __str__(self):
# Escape measurement manually
escaped_measurement = self.measurement.replace(',', '\\,')
escaped_measurement = escaped_measurement.replace(' ', '\\ ')
protocol = escaped_measurement
# Create tag strings
tags = []
for key, value in self.tags.items():
escaped_name = self.__escape(key)
escaped_value = self.__escape(value)
tags.append("%s=%s" % (escaped_name, escaped_value))
# Concatenate tags to current line protocol
if len(tags) > 0:
protocol = "%s,%s" % (protocol, ",".join(tags))
# Create field strings
values = []
for key, value in self.values.items():
escaped_name = self.__escape(key)
escaped_value = self.__parse_value(value)
values.append("%s=%s" % (escaped_name, escaped_value))
# Concatenate fields to current line protocol
protocol = "%s %s" % (protocol, ",".join(values))
if self.timestamp is not None:
protocol = "%s %d" % (protocol, self.timestamp)
return protocol
def __escape(self, value, escape_quotes=False):
# Escape backslashes first since the other characters are escaped with
# backslashes
new_value = value.replace('\\', '\\\\')
new_value = new_value.replace(' ', '\\ ')
new_value = new_value.replace('=', '\\=')
new_value = new_value.replace(',', '\\,')
if escape_quotes:
new_value = new_value.replace('"', '\\"')
return new_value
def __parse_value(self, value):
if type(value) is int:
return "%di" % value
if type(value) is float:
return "%g" % value
if type(value) is bool:
return value and "t" or "f"
return "\"%s\"" % self.__escape(value, True)
if __name__ == '__main__':
collection = MetricCollection()
N = 200
N2 = 100
num_rows = 10000
avg_time = 0
avg_time_2 = 0
for n in range(0, N):
start = time.perf_counter()
collection.clear()
for i in range(0, num_rows):
metric = Metric("weather")
metric.with_timestamp(1465839830100400200)
metric.add_value('CH_0', '0.0')
metric.add_value('CH_1', '0.1')
metric.add_value('CH_2', '0.2')
metric.add_value('CH_3', '0.3')
metric.add_value('CH_4', '0.4')
metric.add_value('CH_5', '0.5')
metric.add_value('CH_6', '0.6')
metric.add_value('CH_7', '0.7')
#data = {}
#for i in range(0, 8):
# data["CH_" + str(i)] = 0.0 + 0.1 * i
#metric.set_values(data)
collection.append(metric)
avg_time += time.perf_counter() - start
avg_time /= N
for n in range(0, N2):
start = time.perf_counter()
line_str = str(collection)
avg_time_2 += time.perf_counter() - start
avg_time_2 /= N2
print(f"Creating {num_rows} rows took {avg_time} seconds")
print(f"Concatinating {num_rows} rows took {avg_time_2} seconds")
#print(collection)