benchmarks/benchmark.py
marcsello c1f214c9d2
All checks were successful
continuous-integration/drone/push Build is passing
did good
2020-10-20 00:45:08 +02:00

111 lines
2.8 KiB
Python

#!/usr/bin/env python3
import logging
import os
import sys
import time
import json
from datetime 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 = "birbnetes"
MQTT_PASSWORD = "de4d2182"
MQTT_HOSTNAME = "mqtt.kmlabz.com"
MQTT_PORT = 1883
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):
global STARTTIME
client.subscribe(f"command/10")
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': '10'}), "application/json")
}
requests.post(URL, files=files)
STARTTIME = time.time()
def mqtt_on_command(client, userdata, message):
global ENDTIME
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':
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="10")
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()