diff --git a/scripts/test.sh b/scripts/test.sh new file mode 100644 index 0000000..60da057 --- /dev/null +++ b/scripts/test.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +sudo tshark -a duration:60 -f "port 25565" -w ./trace/java-60s.pcap +sudo tshark -a duration:60 -f "port 19132" -w ./trace/bedrock-60s.pcap diff --git a/servers/java/run.sh b/servers/java/run.sh old mode 100644 new mode 100755 diff --git a/src/graphs.py b/src/graphs.py new file mode 100644 index 0000000..0f0a15c --- /dev/null +++ b/src/graphs.py @@ -0,0 +1,137 @@ +# Copyright (c) 2024 Guilherme Werner +# SPDX-License-Identifier: MIT + +import pyshark +import pandas as pd +import matplotlib.pyplot as plt + +max_packets = 10000 +trace_files = ["bedrock-60s", "java-60s"] + +metrics_dict = { + "test_name": [], + "total_packets": [], + "average_packet_size_kb": [], + "total_acks": [], + "total_syns": [], + "average_latency_ms": [] +} + +for trace_file in trace_files: + capture = pyshark.FileCapture(f"./trace/{trace_file}.pcap", keep_packets=False) + + data = [] + packet_count = 0 + + for packet in capture: + if "IP" in packet: + try: + timestamp = float(packet.sniff_timestamp) + # Tamanho do pacote em bytes + packet_size = int(packet.length) + + if "TCP" in packet: + ack_flag = int(packet.tcp.flags_ack) if hasattr(packet.tcp, "flags_ack") else 0 + syn_flag = int(packet.tcp.flags_syn) if hasattr(packet.tcp, "flags_syn") else 0 + else: + ack_flag = 0 + syn_flag = 0 + + data.append({ + "timestamp": timestamp, + "packet_size": packet_size, + "ack_flag": ack_flag, + "syn_flag": syn_flag + }) + + packet_count += 1 + + if packet_count >= max_packets: + break + except AttributeError: + continue + + df = pd.DataFrame(data) + + # Calcular a latência em segundos e converter para milissegundos + df["latency"] = df["timestamp"].diff() * 1000 + + total_packets = len(df) + average_packet_size = df["packet_size"].mean() + total_acks = df["ack_flag"].sum() + total_syns = df["syn_flag"].sum() + average_latency = df["latency"].mean() + + metrics_dict["test_name"].append(trace_file) + metrics_dict["total_packets"].append(total_packets) + metrics_dict["average_packet_size_kb"].append(average_packet_size) + metrics_dict["total_acks"].append(total_acks) + metrics_dict["total_syns"].append(total_syns) + metrics_dict["average_latency_ms"].append(round(average_latency, 2)) + + metrics_df = pd.DataFrame([{ + "total_packets": total_packets, + "average_packet_size_kb": average_packet_size, + "total_acks": total_acks, + "total_syns": total_syns, + "average_latency_ms": round(average_latency, 2) + }]) + + metrics_df.to_csv(f"./trace/{trace_file}.csv", index=False) + +metrics_df_all = pd.DataFrame(metrics_dict) + +test_labels = {"java-60s": "Java (60s)", "bedrock-60s": "Bedrock (60s)"} + +# Gráfico do total de pacotes +plt.figure(figsize=(12, 6)) +metrics_df_all.plot(kind="bar", x="test_name", y="total_packets", legend=False) +plt.title("Total de Pacotes por Teste") +plt.xlabel("Teste") +plt.ylabel("Total de Pacotes") +plt.xticks(ticks=range(len(test_labels)), labels=[test_labels[file] for file in trace_files], rotation=45) +plt.tight_layout() +plt.savefig("./trace/total_packets_comparison.png") +plt.close() + +# Gráfico do tamanho médio dos pacotes +plt.figure(figsize=(12, 6)) +metrics_df_all.plot(kind="bar", x="test_name", y="average_packet_size_kb", legend=False) +plt.title("Tamanho Médio dos Pacotes por Teste") +plt.xlabel("Teste") +plt.ylabel("Tamanho Médio dos Pacotes (kB)") +plt.xticks(ticks=range(len(test_labels)), labels=[test_labels[file] for file in trace_files], rotation=45) +plt.tight_layout() +plt.savefig("./trace/average_packet_size_comparison.png") +plt.close() + +# Gráfico da latência média +plt.figure(figsize=(12, 6)) +metrics_df_all.plot(kind="bar", x="test_name", y="average_latency_ms", legend=False) +plt.title("Latência Média por Teste") +plt.xlabel("Teste") +plt.ylabel("Latência Média (ms)") +plt.xticks(ticks=range(len(test_labels)), labels=[test_labels[file] for file in trace_files], rotation=45) +plt.tight_layout() +plt.savefig("./trace/average_latency_comparison.png") +plt.close() + +# Gráfico de Pizza para TCP - Dados vs Controle +# Filtrar apenas pacotes TCP +tcp_df = df[df["ack_flag"] > 0] + +# Calcular os pacotes de dados (não são ACKs) +data_packets = len(df) - tcp_df.shape[0] +control_packets = tcp_df.shape[0] # Inclui ACKs, SYNs, etc. + +# Proporções +sizes = [data_packets, control_packets] +labels = ['Dados', 'Controle'] + +plt.figure(figsize=(8, 8)) +plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140, colors=['#ff9999','#66b3ff']) +plt.title('Distribuição dos Pacotes TCP: Dados vs Controle') +plt.savefig("./trace/tcp_packet_distribution.png") +plt.close() + +print("Métricas e gráficos comparativos salvos.") diff --git a/trace/average_latency_comparison.png b/trace/average_latency_comparison.png new file mode 100644 index 0000000..2631534 Binary files /dev/null and b/trace/average_latency_comparison.png differ diff --git a/trace/average_packet_size_comparison.png b/trace/average_packet_size_comparison.png new file mode 100644 index 0000000..4db4b69 Binary files /dev/null and b/trace/average_packet_size_comparison.png differ diff --git a/trace/bedrock-60s.csv b/trace/bedrock-60s.csv new file mode 100644 index 0000000..b36da6d --- /dev/null +++ b/trace/bedrock-60s.csv @@ -0,0 +1,2 @@ +total_packets,average_packet_size_kb,total_acks,total_syns,average_latency_ms +10000,1101.6974,0,0,0.99 diff --git a/trace/java-60s.csv b/trace/java-60s.csv new file mode 100644 index 0000000..c14573c --- /dev/null +++ b/trace/java-60s.csv @@ -0,0 +1,2 @@ +total_packets,average_packet_size_kb,total_acks,total_syns,average_latency_ms +10000,707.9825,9800,400,0.1 diff --git a/trace/tcp_packet_distribution.png b/trace/tcp_packet_distribution.png new file mode 100644 index 0000000..972d34d Binary files /dev/null and b/trace/tcp_packet_distribution.png differ diff --git a/trace/total_packets_comparison.png b/trace/total_packets_comparison.png new file mode 100644 index 0000000..b0454ee Binary files /dev/null and b/trace/total_packets_comparison.png differ