speed optimizations
paulfantom committed
Jul 13, 2016 at 14:43 UTC
cb4c6077ff24dacbe88472cc6c03d120fab61885
2 files changed
+65
-32
python.d/python_modules/base.py
+63
-28
@@ -93,27 +93,34 @@ class BaseService(threading.Thread):
93
t_start = time.time()
94
# check if it is time to execute job update() function
95
if self.timetable['next'] > t_start:
96
- msg.debug(self.chart_name + " will be run in " +
97
- str(int((self.timetable['next'] - t_start) * 1000)) + " ms")
96
+ #msg.debug(self.chart_name + " will be run in " +
97
+ # str(int((self.timetable['next'] - t_start) * 1000)) + " ms")
98
+ msg.debug(self.chart_name,"will be run in", str(int((self.timetable['next'] - t_start) * 1000)), "ms")
99
return True
100
101
since_last = int((t_start - self.timetable['last']) * 1000000)
101
- msg.debug(self.chart_name +
102
- " ready to run, after " + str(int((t_start - self.timetable['last']) * 1000)) +
103
- " ms (update_every: " + str(self.timetable['freq'] * 1000) +
104
- " ms, latency: " + str(int((t_start - self.timetable['next']) * 1000)) + " ms)")
102
+ #msg.debug(self.chart_name +
103
+ # " ready to run, after " + str(int((t_start - self.timetable['last']) * 1000)) +
104
+ # " ms (update_every: " + str(self.timetable['freq'] * 1000) +
105
+ # " ms, latency: " + str(int((t_start - self.timetable['next']) * 1000)) + " ms)")
106
+ msg.debug(self.chart_name,
107
+ "ready to run, after", str(int((t_start - self.timetable['last']) * 1000)),
108
+ "ms (update_every:", str(self.timetable['freq'] * 1000),
109
+ "ms, latency:", str(int((t_start - self.timetable['next']) * 1000)), "ms")
110
if not self.update(since_last):
111
return False
112
t_end = time.time()
113
self.timetable['next'] = t_end - (t_end % self.timetable['freq']) + self.timetable['freq']
109
-
114
# draw performance graph
115
run_time = str(int((t_end - t_start) * 1000))
112
- run_time_chart = "BEGIN netdata.plugin_pythond_" + self.chart_name + " " + str(since_last) + '\n'
113
- run_time_chart += "SET run_time = " + run_time + '\n'
114
- run_time_chart += "END\n"
116
+ #run_time_chart = "BEGIN netdata.plugin_pythond_" + self.chart_name + " " + str(since_last) + '\n'
117
+ #run_time_chart += "SET run_time = " + run_time + '\n'
118
+ #run_time_chart += "END\n"
119
+ run_time_chart = "BEGIN netdata.plugin_pythond_%s %s\nSET run_time = %s\nEND\n" % \
120
+ (self.chart_name, str(since_last), run_time)
121
sys.stdout.write(run_time_chart)
116
- msg.debug(self.chart_name + " updated in " + str(run_time) + " ms")
122
+ #msg.debug(self.chart_name + " updated in " + str(run_time) + " ms")
123
+ msg.debug(self.chart_name, "updated in", str(run_time), "ms")
124
self.timetable['last'] = t_start
125
return True
126
@@ -141,23 +148,42 @@ class BaseService(threading.Thread):
148
else:
149
time.sleep(self.timetable['freq'])
150
151
+ def _format(self, *input):
152
+ params = []
153
+ append = params.append
154
+ for p in input:
155
+ if p is None:
156
+ append(p)
157
+ continue
158
+ if type(p) is not str:
159
+ p = str(p)
160
+ if ' ' in p:
161
+ p = "'" + p + "'"
162
+ append(p)
163
+ return params
164
+
165
def _line(self, instruction, *params):
166
"""
167
Converts *params to string and joins them with one space between every one.
168
:param params: str/int/float
169
"""
149
- self._data_stream += instruction
150
- for p in params:
151
- if p is None:
152
- p = ""
153
- else:
154
- p = str(p)
155
- if len(p) == 0:
156
- p = "''"
157
- if ' ' in p:
158
- p = "'" + p + "'"
159
- self._data_stream += " " + p
160
- self._data_stream += "\n"
170
+ #self._data_stream += instruction
171
+ tmp = list(map((lambda x: "''" if x is None or len(x) == 0 else x), params))
172
+
173
+ self._data_stream += "%s %s\n" % (instruction, str(" ".join(tmp)))
174
+
175
+ # self.error(str(" ".join(tmp)))
176
+ # for p in params:
177
+ # if p is None:
178
+ # p = ""
179
+ # else:
180
+ # p = str(p)
181
+ # if len(p) == 0:
182
+ # p = "''"
183
+ # if ' ' in p:
184
+ # p = "'" + p + "'"
185
+ # self._data_stream += " " + p
186
+ #self._data_stream += "\n"
187
188
def chart(self, type_id, name="", title="", units="", family="",
189
category="", charttype="line", priority="", update_every=""):
@@ -174,7 +200,12 @@ class BaseService(threading.Thread):
200
:param update_every: int/str
201
"""
202
self._charts.append(type_id)
177
- self._line("CHART", type_id, name, title, units, family, category, charttype, priority, update_every)
203
+ #self._line("CHART", type_id, name, title, units, family, category, charttype, priority, update_every)
204
+
205
+ p = self._format(type_id, name, title, units, family, category, charttype, priority, update_every)
206
+ self._line("CHART", *p)
207
+
208
+
209
210
def dimension(self, id, name=None, algorithm="absolute", multiplier=1, divisor=1, hidden=False):
211
"""
@@ -204,9 +235,13 @@ class BaseService(threading.Thread):
235
236
self._dimensions.append(id)
237
if hidden:
207
- self._line("DIMENSION", id, name, algorithm, multiplier, divisor, "hidden")
238
+ p = self._format(id, name, algorithm, multiplier, divisor, "hidden")
239
+ #self._line("DIMENSION", id, name, algorithm, str(multiplier), str(divisor), "hidden")
240
else:
209
- self._line("DIMENSION", id, name, algorithm, multiplier, divisor)
241
+ p = self._format(id, name, algorithm, multiplier, divisor)
242
+ #self._line("DIMENSION", id, name, algorithm, str(multiplier), str(divisor))
243
+
244
+ self._line("DIMENSION", *p)
245
246
def begin(self, type_id, microseconds=0):
247
"""
@@ -224,7 +259,7 @@ class BaseService(threading.Thread):
259
self.error("malformed begin statement: microseconds are not a number:", microseconds)
260
microseconds = ""
261
227
- self._line("BEGIN", type_id, microseconds)
262
+ self._line("BEGIN", type_id, str(microseconds))
263
return True
264
265
def set(self, id, value):
@@ -242,7 +277,7 @@ class BaseService(threading.Thread):
277
except TypeError:
278
self.error("cannot set non-numeric value:", value)
279
return False
245
- self._line("SET", id, "=", value)
280
+ self._line("SET", id, "=", str(value))
281
return True
282
283
def end(self):
python.d/python_modules/msg.py
+2
-4
@@ -12,10 +12,8 @@ def log_msg(msg_type, *args):
12
Print message on stderr.
13
:param msg_type: str
14
"""
15
- msg = PROGRAM + " " + str(msg_type) + ":"
16
- for i in args:
17
- msg += " "
18
- msg += str(i)
15
+ msg = "%s %s: %s" % (PROGRAM, str(msg_type), " ".join(args))
16
+
17
sys.stderr.write(msg + "\n")
18
sys.stderr.flush()
19