diff options
Diffstat (limited to 'analysis/e1anl/src')
| -rw-r--r-- | analysis/e1anl/src/__pycache__/agregate.cpython-313.pyc | bin | 0 -> 3528 bytes | |||
| -rw-r--r-- | analysis/e1anl/src/__pycache__/analyze.cpython-313.pyc | bin | 0 -> 2962 bytes | |||
| -rw-r--r-- | analysis/e1anl/src/agregate.py | 71 | ||||
| -rw-r--r-- | analysis/e1anl/src/analyze.py | 49 | ||||
| -rw-r--r-- | analysis/e1anl/src/main.py | 17 |
5 files changed, 137 insertions, 0 deletions
diff --git a/analysis/e1anl/src/__pycache__/agregate.cpython-313.pyc b/analysis/e1anl/src/__pycache__/agregate.cpython-313.pyc new file mode 100644 index 0000000..3a0d6cb --- /dev/null +++ b/analysis/e1anl/src/__pycache__/agregate.cpython-313.pyc | |||
| Binary files differ | |||
diff --git a/analysis/e1anl/src/__pycache__/analyze.cpython-313.pyc b/analysis/e1anl/src/__pycache__/analyze.cpython-313.pyc new file mode 100644 index 0000000..a891763 --- /dev/null +++ b/analysis/e1anl/src/__pycache__/analyze.cpython-313.pyc | |||
| Binary files differ | |||
diff --git a/analysis/e1anl/src/agregate.py b/analysis/e1anl/src/agregate.py new file mode 100644 index 0000000..1054288 --- /dev/null +++ b/analysis/e1anl/src/agregate.py | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | # mqtt_receiver.py | ||
| 2 | |||
| 3 | import paho.mqtt.client as mqtt | ||
| 4 | import csv | ||
| 5 | import time | ||
| 6 | from datetime import datetime | ||
| 7 | from collections import deque | ||
| 8 | import os | ||
| 9 | os.makedirs("output", exist_ok=True) | ||
| 10 | |||
| 11 | BROKER = "192.168.1.101" | ||
| 12 | SUB_TOPIC = "device/echo/in" | ||
| 13 | CSV_FILE = "output/rtt_throughput_log.csv" | ||
| 14 | WINDOW_SEC = 1 | ||
| 15 | |||
| 16 | msg_times = deque() | ||
| 17 | should_stop = False # flag to stop loop externally | ||
| 18 | |||
| 19 | |||
| 20 | def on_connect(client, userdata, flags, rc): | ||
| 21 | if rc == 0: | ||
| 22 | print("Connected to broker") | ||
| 23 | client.subscribe(SUB_TOPIC) | ||
| 24 | else: | ||
| 25 | print("Failed to connect, return code:", rc) | ||
| 26 | |||
| 27 | |||
| 28 | def on_message(client, userdata, msg): | ||
| 29 | try: | ||
| 30 | payload_str = msg.payload.decode() | ||
| 31 | sent_ms = int(payload_str) | ||
| 32 | received_ms = int(time.time() * 1000) | ||
| 33 | rtt = received_ms - sent_ms | ||
| 34 | timestamp = datetime.now().isoformat() | ||
| 35 | |||
| 36 | now_sec = time.time() | ||
| 37 | msg_times.append(now_sec) | ||
| 38 | while msg_times and msg_times[0] < now_sec - WINDOW_SEC: | ||
| 39 | msg_times.popleft() | ||
| 40 | throughput = len(msg_times) / WINDOW_SEC | ||
| 41 | |||
| 42 | with open(CSV_FILE, "a", newline="") as f: | ||
| 43 | writer = csv.writer(f) | ||
| 44 | writer.writerow([timestamp, sent_ms, received_ms, rtt, throughput]) | ||
| 45 | |||
| 46 | except Exception as e: | ||
| 47 | print("Error processing message:", e) | ||
| 48 | |||
| 49 | |||
| 50 | def collect_data(duration_sec): | ||
| 51 | global should_stop | ||
| 52 | should_stop = False | ||
| 53 | |||
| 54 | # Start CSV log | ||
| 55 | with open(CSV_FILE, "w", newline="") as f: | ||
| 56 | writer = csv.writer(f) | ||
| 57 | writer.writerow(["timestamp", "sent_ms", "received_ms", "rtt_ms", "throughput_msg_per_s"]) | ||
| 58 | |||
| 59 | client = mqtt.Client() | ||
| 60 | client.on_connect = on_connect | ||
| 61 | client.on_message = on_message | ||
| 62 | client.connect(BROKER, 1883, 60) | ||
| 63 | |||
| 64 | client.loop_start() | ||
| 65 | print(f"Zbieranie danych przez {duration_sec / 60:.1f} minut...") | ||
| 66 | |||
| 67 | time.sleep(duration_sec) | ||
| 68 | |||
| 69 | print("Zbieranie danych zakończone.") | ||
| 70 | client.loop_stop() | ||
| 71 | client.disconnect() | ||
diff --git a/analysis/e1anl/src/analyze.py b/analysis/e1anl/src/analyze.py new file mode 100644 index 0000000..6e73df3 --- /dev/null +++ b/analysis/e1anl/src/analyze.py | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | # analyze_metrics.py | ||
| 2 | |||
| 3 | import pandas as pd | ||
| 4 | import matplotlib.pyplot as plt | ||
| 5 | import seaborn as sns | ||
| 6 | |||
| 7 | import os | ||
| 8 | os.makedirs("output", exist_ok=True) | ||
| 9 | |||
| 10 | def analyze(): | ||
| 11 | CSV_FILE = "output/rtt_throughput_log.csv" | ||
| 12 | df = pd.read_csv(CSV_FILE) | ||
| 13 | |||
| 14 | df['sent_ms'] = df['sent_ms'].astype(int) | ||
| 15 | df['received_ms'] = df['received_ms'].astype(int) | ||
| 16 | df['rtt_ms'] = df['rtt_ms'].astype(int) | ||
| 17 | df['throughput_msg_per_s'] = df['throughput_msg_per_s'].astype(float) | ||
| 18 | |||
| 19 | mean_rtt = df['rtt_ms'].mean() | ||
| 20 | p95_rtt = df['rtt_ms'].quantile(0.95) | ||
| 21 | mean_throughput = df['throughput_msg_per_s'].mean() | ||
| 22 | |||
| 23 | print(f"Średnie RTT: {mean_rtt:.2f} ms") | ||
| 24 | print(f"RTT 95-percentyl: {p95_rtt:.2f} ms") | ||
| 25 | print(f"Średni throughput: {mean_throughput:.2f} msg/s") | ||
| 26 | |||
| 27 | plt.figure(figsize=(6,4)) | ||
| 28 | sns.barplot(x=['Variant A'], y=[mean_rtt], palette="Set2") | ||
| 29 | plt.title("Średnie RTT - wariant A") | ||
| 30 | plt.ylabel("RTT [ms]") | ||
| 31 | plt.tight_layout() | ||
| 32 | plt.savefig("output/rtt_mean_a.png") | ||
| 33 | plt.show() | ||
| 34 | |||
| 35 | plt.figure(figsize=(6,4)) | ||
| 36 | sns.barplot(x=['Variant A'], y=[p95_rtt], palette="Set3") | ||
| 37 | plt.title("RTT 95-percentyl - wariant A") | ||
| 38 | plt.ylabel("RTT [ms]") | ||
| 39 | plt.tight_layout() | ||
| 40 | plt.savefig("output/rtt_p95_a.png") | ||
| 41 | plt.show() | ||
| 42 | |||
| 43 | plt.figure(figsize=(6,4)) | ||
| 44 | sns.barplot(x=['Variant A'], y=[mean_throughput], palette="Set1") | ||
| 45 | plt.title("Średni throughput - wariant A") | ||
| 46 | plt.ylabel("Messages per second") | ||
| 47 | plt.tight_layout() | ||
| 48 | plt.savefig("output/throughput_a.png") | ||
| 49 | plt.show() | ||
diff --git a/analysis/e1anl/src/main.py b/analysis/e1anl/src/main.py new file mode 100644 index 0000000..b35eead --- /dev/null +++ b/analysis/e1anl/src/main.py | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | # main.py | ||
| 2 | |||
| 3 | from agregate import collect_data | ||
| 4 | from analyze import analyze | ||
| 5 | |||
| 6 | def main(): | ||
| 7 | DURATION_HOURS = 2 | ||
| 8 | DURATION_SECONDS = DURATION_HOURS * 3600 | ||
| 9 | |||
| 10 | print("Collecting data...") | ||
| 11 | collect_data(DURATION_SECONDS) | ||
| 12 | |||
| 13 | print("Analyzing data...") | ||
| 14 | analyze() | ||
| 15 | |||
| 16 | if __name__ == "__main__": | ||
| 17 | main() | ||
