From 336a2c0a1237b1be4820b799578fb181fcb7272c Mon Sep 17 00:00:00 2001 From: Torma Date: Mon, 19 Oct 2020 19:11:18 +0200 Subject: [PATCH] benchmark script done --- .drone.yml | 20 +++++++++ .gitignore | 2 +- benchmark.py | 110 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 ++ 4 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 .drone.yml create mode 100644 benchmark.py create mode 100644 requirements.txt diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..616bebe --- /dev/null +++ b/.drone.yml @@ -0,0 +1,20 @@ +kind: pipeline +type: docker +name: default + +steps: + - name: code-analysis + image: aosapps/drone-sonar-plugin + settings: + sonar_host: + from_secret: SONAR_HOST + sonar_token: + from_secret: SONAR_CODE + + - name: ms-teams + image: kuperiu/drone-teams + settings: + webhook: + from_secret: TEAMS_WEBHOOK + when: + status: [ failure ] \ No newline at end of file diff --git a/.gitignore b/.gitignore index 13d1490..388004c 100644 --- a/.gitignore +++ b/.gitignore @@ -128,4 +128,4 @@ dmypy.json # Pyre type checker .pyre/ - +.idea/ \ No newline at end of file diff --git a/benchmark.py b/benchmark.py new file mode 100644 index 0000000..89bed1f --- /dev/null +++ b/benchmark.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python3 + +import logging +import os +import sys +import time +import json +import datetime +import sentry_sdk +from sentry_sdk.integrations.logging import LoggingIntegration +import paho.mqtt.client +import requests + +""" +Main Entrypoint +""" + +__author__ = "@tormakris" +__copyright__ = "Copyright 2020, Birbnetes Team" +__module_name__ = "app" +__version__text__ = "1" + +SENTRY_DSN = os.environ.get("SENTRY_DSN") +RELEASE_ID = os.environ.get("RELEASE_ID", "test") +RELEASEMODE = os.environ.get("RELEASEMODE", "dev") + +DEVICE_ID = os.environ.get("DEVICE_ID", "devraspi") + +MQTT_USERNAME = "" +MQTT_PASSWORD = "" +MQTT_HOSTNAME = "" +MQTT_PORT = "" + +STARTTIME = time.time() +ENDTIME = time.time() + +URL = "https://birb.k8s.kmlabz.com/sample" +FILE = 'CommonStarling_100962.wav' + +sentry_logging = LoggingIntegration( + level=logging.INFO, + event_level=logging.ERROR +) + +if SENTRY_DSN: + sentry_sdk.init( + dsn=SENTRY_DSN, + integrations=[sentry_logging], + traces_sample_rate=1.0, + send_default_pii=True, + release=RELEASE_ID, + environment=RELEASEMODE, + _experiments={"auto_enabling_integrations": True} + ) + + +def mqtt_on_connect(client, userdata, flags, rc): + client.subscribe(f"command/benchmark-script") + logging.info("Sending Message") + files = { + "file": ( + os.path.basename(FILE), open(FILE, 'rb').read(), 'audio/wave', {'Content-length': os.path.getsize(FILE)}), + "description": (None, json.dumps({'date': datetime.now().isoformat(), 'device_id': '1'}), "application/json") + } + requests.post(URL, files=files) + nonlocal STARTTIME + STARTTIME = time.time() + + +def mqtt_on_command(client, userdata, message): + try: + msg = json.loads(message.payload.decode()) + except (UnicodeError, json.JSONDecodeError) as e: + logging.error(f"MQTT Invalid message recieved: {e}") + return + + if msg.get("command") == 'doAlert': + nonlocal ENDTIME + ENDTIME = time.time() + elapsed = ENDTIME - STARTTIME + logging.info(f"Elapsed time: {elapsed}") + + +def main() -> None: + """ + Main function + :return: + """ + logging.basicConfig(stream=sys.stdout, format="%(asctime)s - %(name)s [%(levelname)s]: %(message)s", + level=logging.DEBUG if '--debug' in sys.argv else logging.INFO) + + client = paho.mqtt.client.Client(client_id="benchmark-script") + client.on_connect = mqtt_on_connect + client.on_message = mqtt_on_command + + if MQTT_USERNAME: + client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD) + + client.connect(MQTT_HOSTNAME, MQTT_PORT, 60) + + try: + client.loop_forever() + except KeyboardInterrupt: + logging.info("SIGINT recieved! Stopping...") + + client.disconnect() + + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c598ad7 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +paho-mqtt +requests +sentry_sdk \ No newline at end of file