diff --git a/.gitignore b/.gitignore index 37891a0..42e1797 100644 --- a/.gitignore +++ b/.gitignore @@ -134,3 +134,5 @@ data/database.db secrets.yaml test.py test.png +data/database.db-shm +data/database.db-wal diff --git a/config/config.yaml b/config/config.yaml index 6fa5c43..243d21c 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -6,4 +6,13 @@ ros_ip: 192.168.88.1 ros_queues: - WAN ros_du_invert: False -ros_fasttrack_comment: "defconf: fasttrack" \ No newline at end of file +ros_fasttrack_comment: "defconf: fasttrack" +# Minimum speed which not to cross, if speed is lower than this it will set this speed +ros_minimum_speed: 300000 +# Speed at which to leave fasttrack on. Use if your ROS device's CPU can't handle max speed you get, for example LHG LTE +# starts to show high CPU usage at close to 20mbit. +# Not implemented yet +ros_maximum_speed: 15000000 # 15 mbit + + +# Conversion rate 1 mbit : 1000000 bits \ No newline at end of file diff --git a/speedtester.py b/speedtester.py index b5b5b40..54f36a7 100644 --- a/speedtester.py +++ b/speedtester.py @@ -111,6 +111,36 @@ def ros_dynamic_speed(upload, download): connection.disconnect() +def speedtest(): + import speedtest + + servers = [] + threads = None + + for i in range(0, 3): + try: + log.debug("Initializing speedtest...") + s = speedtest.Speedtest() + + log.debug(f"Running test...") + s.get_servers(servers) + s.get_best_server() + s.download(threads=threads) + s.upload(threads=threads, pre_allocate=False) + + results_dict = s.results.dict() + + if results_dict['download'] >= config['ros_minimum_speed']: + download = round(results_dict['download'] / 1000000, 2) + upload = round(results_dict['upload'] / 1000000, 2) + break + time.sleep(10) + except: + log.error(f"Test failed, try {i + 1}/3", exc_info=True) + + log.debug(f"{download}mbps, {upload}mbps") + return download, upload, results_dict + if __name__ == "__main__": ''' This script will run a few speed tests, calculate average upload and download speeds and record them into database. @@ -127,41 +157,21 @@ if __name__ == "__main__": ros_fastrack_enable(True) time.sleep(5) - import speedtest - - servers = [] - threads = None - - for i in range(0, 3): - try: - log.debug("Initializing speedtest...") - s = speedtest.Speedtest() - - log.debug(f"Running test...") - s.get_servers(servers) - s.get_best_server() - s.download(threads=threads) - s.upload(threads=threads, pre_allocate=False) - - results_dict = s.results.dict() - download = round(results_dict['download'] / 1000000, 2) - upload = round(results_dict['upload'] / 1000000, 2) - # download = uniform(0,2) - # upload = uniform(0,2) - break - except: - log.error(f"Test failed, try {i + 1}/3", exc_info=True) - - log.debug(f"{download}mbps, {upload}mbps") + download, upload, results_dict = speedtest() entry = Entry() entry.upload = upload entry.download = download entry.save() + ros_upload = results_dict['upload'] + ros_download = results_dict['download'] + + if ros_download < config['ros_minimum_speed']: + ros_download = config['ros_minimum_speed'] + if config["ros_dynamic_speed"]: ros_dynamic_speed(results_dict['upload'], results_dict['download']) - ros_fastrack_enable(False) dates, downloads, uploads = gather_data()