From d0fb756e448ec697353b0213a41348e89d387ccb Mon Sep 17 00:00:00 2001 From: nico Date: Thu, 24 Oct 2019 17:41:51 +0200 Subject: [PATCH] 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 --- influx.py | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/influx.py b/influx.py index 327154f..fcf2f41 100644 --- a/influx.py +++ b/influx.py @@ -9,9 +9,10 @@ from influxdb import InfluxDBClient from ejabberdrpc import EjabberdMetrics -class Influx(): - def __init__(self, metrics): +class Influx: + def __init__(self, metrics, client): self._metrics = metrics + self.client = client @staticmethod def _timestamp(): @@ -71,27 +72,25 @@ class Influx(): 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})) - # output - client = InfluxDBClient(host='localhost', port=8086) - - # 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)) - + # write output to database + self.client.write_points(data, database='custom', time_precision='ms', batch_size=10000, protocol='line') if __name__ == "__main__": with open("config.json", "r", encoding="utf-8") as f: login = json.load(f) - metrics = EjabberdMetrics("http://[::1]:4560", login) - influx = Influx(metrics) + # init global handler + 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: + # TODO this will fail when the connection drops try except maybe? metrics.update() influx.writeMetrics()