Skip to content

Instantly share code, notes, and snippets.

@TylerCode
Created December 22, 2023 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TylerCode/097f38664a831e8a906fd9a38ef78b30 to your computer and use it in GitHub Desktop.
Save TylerCode/097f38664a831e8a906fd9a38ef78b30 to your computer and use it in GitHub Desktop.
Speedtesting on the reg
import csv
import subprocess
from datetime import datetime
import time
def run_speedtest():
# Run speedtest-cli and capture output
process = subprocess.Popen(['speedtest-cli', '--csv'], stdout=subprocess.PIPE)
output, error = process.communicate()
if process.returncode != 0:
print("Error running speedtest-cli")
return None
return output.decode('utf-8')
def main():
results = []
for i in range(3):
print(f"Running test {i+1}/3...")
result = run_speedtest()
if result:
results.append(result)
print(f"Test {i+1} completed.")
time.sleep(60) # Wait for a minute before the next test
# Use the correct headers from `speedtest-cli --csv-header` output
headers = results[0].split(',')
ping_index = 6
download_index = 7
upload_index = 8
# Calculate averages
averages = [
sum(float(row.split(',')[ping_index]) for row in results) / len(results),
sum(float(row.split(',')[download_index]) for row in results) / len(results) / 1e6,
sum(float(row.split(',')[upload_index]) for row in results) / len(results) / 1e6
]
# Add current date and time
averages.insert(0, datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
# Write to CSV
with open('~/speedtest_results.csv', 'a', newline='') as file:
writer = csv.writer(file)
writer.writerow(averages)
print("Results written to CSV.")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment