From 1ba21da6cbc63c0c549fb92731e25bedc482eb51 Mon Sep 17 00:00:00 2001 From: Filip Wandzio Date: Thu, 4 Sep 2025 22:25:39 +0200 Subject: Unify the directory, add new analysis methods, unify the code style Signed-off-by: Filip Wandzio --- analysis/e1anl/Makefile | 27 ++++++++ .../e1anl/__pycache__/agregate.cpython-313.pyc | Bin 0 -> 3423 bytes analysis/e1anl/__pycache__/analyze.cpython-313.pyc | Bin 0 -> 2835 bytes analysis/e1anl/requirements.txt | 16 +++++ .../e1anl/src/__pycache__/agregate.cpython-313.pyc | Bin 0 -> 3528 bytes .../e1anl/src/__pycache__/analyze.cpython-313.pyc | Bin 0 -> 2962 bytes analysis/e1anl/src/agregate.py | 71 +++++++++++++++++++++ analysis/e1anl/src/analyze.py | 49 ++++++++++++++ analysis/e1anl/src/main.py | 17 +++++ 9 files changed, 180 insertions(+) create mode 100644 analysis/e1anl/Makefile create mode 100644 analysis/e1anl/__pycache__/agregate.cpython-313.pyc create mode 100644 analysis/e1anl/__pycache__/analyze.cpython-313.pyc create mode 100644 analysis/e1anl/requirements.txt create mode 100644 analysis/e1anl/src/__pycache__/agregate.cpython-313.pyc create mode 100644 analysis/e1anl/src/__pycache__/analyze.cpython-313.pyc create mode 100644 analysis/e1anl/src/agregate.py create mode 100644 analysis/e1anl/src/analyze.py create mode 100644 analysis/e1anl/src/main.py (limited to 'analysis/e1anl') diff --git a/analysis/e1anl/Makefile b/analysis/e1anl/Makefile new file mode 100644 index 0000000..7cbd4a3 --- /dev/null +++ b/analysis/e1anl/Makefile @@ -0,0 +1,27 @@ +# Makefile + +VENV_NAME=venv +PYTHON=$(VENV_NAME)/bin/python +SRC=src +OUTPUT=output + +.PHONY: venv install run analyze clean + +venv: + python3 -m venv $(VENV_NAME) + +install: venv + $(PYTHON) -m pip install --upgrade pip + $(PYTHON) -m pip install -r requirements.txt + +run: install + mkdir -p $(OUTPUT) + $(PYTHON) $(SRC)/main.py + +analyze: install + mkdir -p $(OUTPUT) + $(PYTHON) $(SRC)/analyze_metrics.py + +clean: + rm -rf $(VENV_NAME) + rm -rf $(OUTPUT) diff --git a/analysis/e1anl/__pycache__/agregate.cpython-313.pyc b/analysis/e1anl/__pycache__/agregate.cpython-313.pyc new file mode 100644 index 0000000..21c7510 Binary files /dev/null and b/analysis/e1anl/__pycache__/agregate.cpython-313.pyc differ diff --git a/analysis/e1anl/__pycache__/analyze.cpython-313.pyc b/analysis/e1anl/__pycache__/analyze.cpython-313.pyc new file mode 100644 index 0000000..0b6a4ce Binary files /dev/null and b/analysis/e1anl/__pycache__/analyze.cpython-313.pyc differ diff --git a/analysis/e1anl/requirements.txt b/analysis/e1anl/requirements.txt new file mode 100644 index 0000000..3c9cae6 --- /dev/null +++ b/analysis/e1anl/requirements.txt @@ -0,0 +1,16 @@ +contourpy==1.3.3 +cycler==0.12.1 +fonttools==4.59.2 +kiwisolver==1.4.9 +matplotlib==3.10.6 +numpy==2.3.2 +packaging==25.0 +paho-mqtt==2.1.0 +pandas==2.3.2 +pillow==11.3.0 +pyparsing==3.2.3 +python-dateutil==2.9.0.post0 +pytz==2025.2 +seaborn==0.13.2 +six==1.17.0 +tzdata==2025.2 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 Binary files /dev/null and b/analysis/e1anl/src/__pycache__/agregate.cpython-313.pyc 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 Binary files /dev/null and b/analysis/e1anl/src/__pycache__/analyze.cpython-313.pyc 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 @@ +# mqtt_receiver.py + +import paho.mqtt.client as mqtt +import csv +import time +from datetime import datetime +from collections import deque +import os +os.makedirs("output", exist_ok=True) + +BROKER = "192.168.1.101" +SUB_TOPIC = "device/echo/in" +CSV_FILE = "output/rtt_throughput_log.csv" +WINDOW_SEC = 1 + +msg_times = deque() +should_stop = False # flag to stop loop externally + + +def on_connect(client, userdata, flags, rc): + if rc == 0: + print("Connected to broker") + client.subscribe(SUB_TOPIC) + else: + print("Failed to connect, return code:", rc) + + +def on_message(client, userdata, msg): + try: + payload_str = msg.payload.decode() + sent_ms = int(payload_str) + received_ms = int(time.time() * 1000) + rtt = received_ms - sent_ms + timestamp = datetime.now().isoformat() + + now_sec = time.time() + msg_times.append(now_sec) + while msg_times and msg_times[0] < now_sec - WINDOW_SEC: + msg_times.popleft() + throughput = len(msg_times) / WINDOW_SEC + + with open(CSV_FILE, "a", newline="") as f: + writer = csv.writer(f) + writer.writerow([timestamp, sent_ms, received_ms, rtt, throughput]) + + except Exception as e: + print("Error processing message:", e) + + +def collect_data(duration_sec): + global should_stop + should_stop = False + + # Start CSV log + with open(CSV_FILE, "w", newline="") as f: + writer = csv.writer(f) + writer.writerow(["timestamp", "sent_ms", "received_ms", "rtt_ms", "throughput_msg_per_s"]) + + client = mqtt.Client() + client.on_connect = on_connect + client.on_message = on_message + client.connect(BROKER, 1883, 60) + + client.loop_start() + print(f"Zbieranie danych przez {duration_sec / 60:.1f} minut...") + + time.sleep(duration_sec) + + print("Zbieranie danych zakończone.") + client.loop_stop() + 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 @@ +# analyze_metrics.py + +import pandas as pd +import matplotlib.pyplot as plt +import seaborn as sns + +import os +os.makedirs("output", exist_ok=True) + +def analyze(): + CSV_FILE = "output/rtt_throughput_log.csv" + df = pd.read_csv(CSV_FILE) + + df['sent_ms'] = df['sent_ms'].astype(int) + df['received_ms'] = df['received_ms'].astype(int) + df['rtt_ms'] = df['rtt_ms'].astype(int) + df['throughput_msg_per_s'] = df['throughput_msg_per_s'].astype(float) + + mean_rtt = df['rtt_ms'].mean() + p95_rtt = df['rtt_ms'].quantile(0.95) + mean_throughput = df['throughput_msg_per_s'].mean() + + print(f"Średnie RTT: {mean_rtt:.2f} ms") + print(f"RTT 95-percentyl: {p95_rtt:.2f} ms") + print(f"Średni throughput: {mean_throughput:.2f} msg/s") + + plt.figure(figsize=(6,4)) + sns.barplot(x=['Variant A'], y=[mean_rtt], palette="Set2") + plt.title("Średnie RTT - wariant A") + plt.ylabel("RTT [ms]") + plt.tight_layout() + plt.savefig("output/rtt_mean_a.png") + plt.show() + + plt.figure(figsize=(6,4)) + sns.barplot(x=['Variant A'], y=[p95_rtt], palette="Set3") + plt.title("RTT 95-percentyl - wariant A") + plt.ylabel("RTT [ms]") + plt.tight_layout() + plt.savefig("output/rtt_p95_a.png") + plt.show() + + plt.figure(figsize=(6,4)) + sns.barplot(x=['Variant A'], y=[mean_throughput], palette="Set1") + plt.title("Średni throughput - wariant A") + plt.ylabel("Messages per second") + plt.tight_layout() + plt.savefig("output/throughput_a.png") + 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 @@ +# main.py + +from agregate import collect_data +from analyze import analyze + +def main(): + DURATION_HOURS = 2 + DURATION_SECONDS = DURATION_HOURS * 3600 + + print("Collecting data...") + collect_data(DURATION_SECONDS) + + print("Analyzing data...") + analyze() + +if __name__ == "__main__": + main() -- cgit v1.2.3