optimization attempt
* init client handler only once and not every time * don't try to create database on every run just on the first one
This commit is contained in:
parent
38dba3845f
commit
d0fb756e44
31
influx.py
31
influx.py
|
@ -9,9 +9,10 @@ from influxdb import InfluxDBClient
|
||||||
from ejabberdrpc import EjabberdMetrics
|
from ejabberdrpc import EjabberdMetrics
|
||||||
|
|
||||||
|
|
||||||
class Influx():
|
class Influx:
|
||||||
def __init__(self, metrics):
|
def __init__(self, metrics, client):
|
||||||
self._metrics = metrics
|
self._metrics = metrics
|
||||||
|
self.client = client
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _timestamp():
|
def _timestamp():
|
||||||
|
@ -71,27 +72,25 @@ class Influx():
|
||||||
for k, v in ipv.items():
|
for k, v in ipv.items():
|
||||||
data.append(self._parse("ejabberd_online_client_ipversion", k, v, cur_ts, {"vhost": vhost, "node": node, "ipversion": k, "client": cl}))
|
data.append(self._parse("ejabberd_online_client_ipversion", k, v, cur_ts, {"vhost": vhost, "node": node, "ipversion": k, "client": cl}))
|
||||||
|
|
||||||
# output
|
# write output to database
|
||||||
client = InfluxDBClient(host='localhost', port=8086)
|
self.client.write_points(data, database='custom', time_precision='ms', batch_size=10000, protocol='line')
|
||||||
|
|
||||||
# test code
|
|
||||||
client.create_database('writetest')
|
|
||||||
client_write_start_time = time.perf_counter()
|
|
||||||
client.write_points(data, database='writetest', time_precision='ms', batch_size=10000, protocol='line')
|
|
||||||
|
|
||||||
# test code
|
|
||||||
client_write_end_time = time.perf_counter()
|
|
||||||
print("Client Library Write: {time}s".format(time=client_write_end_time - client_write_start_time))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
with open("config.json", "r", encoding="utf-8") as f:
|
with open("config.json", "r", encoding="utf-8") as f:
|
||||||
login = json.load(f)
|
login = json.load(f)
|
||||||
|
|
||||||
metrics = EjabberdMetrics("http://[::1]:4560", login)
|
# init global handler
|
||||||
influx = Influx(metrics)
|
metrics = EjabberdMetrics("http://localhost:4560", login)
|
||||||
|
client = InfluxDBClient(host='localhost', port=8086, database='custom')
|
||||||
|
|
||||||
|
# create database only once
|
||||||
|
client.create_database('custom')
|
||||||
|
|
||||||
|
# init influx class
|
||||||
|
influx = Influx(metrics, client)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
# TODO this will fail when the connection drops try except maybe?
|
||||||
metrics.update()
|
metrics.update()
|
||||||
influx.writeMetrics()
|
influx.writeMetrics()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue