Verified Commit 672713e2 authored by Sebastian Endres's avatar Sebastian Endres
Browse files

More spinner...

parent 0a434ecd
......@@ -15,6 +15,7 @@ from typing import Callable, Literal, Optional, TypeVar, Union
import numpy as np
from matplotlib import pyplot as plt
from yaspin import yaspin
from tracer import ParsingError, Trace, get_quic_payload_size
from utils import existing_file_path
......@@ -245,54 +246,55 @@ class PlotTimeSequenceCli:
# raise errors early
_ = trace.get_facts()
timestamps = [
np.array([float(packet.sniff_timestamp) for packet in trace.packets])
for trace in self.traces
]
packet_numbers = [
np.array([int(packet.quic.packet_number) for packet in trace.packets])
for trace in self.traces
]
min_packet_number: int = map2d(min, packet_numbers)
max_packet_number: int = map2d(max, packet_numbers)
min_timestamp: np.float64 = map2d(min, timestamps)
max_timestamp: np.float64 = map2d(max, timestamps)
ax.set_xlim(left=min(0, min_timestamp), right=max_timestamp)
ax.set_ylim(bottom=min(0, min_packet_number), top=max_packet_number)
with yaspin(text="processing...", color="cyan") as spinner:
timestamps = [
np.array([float(packet.sniff_timestamp) for packet in trace.packets])
for trace in self.traces
]
packet_numbers = [
np.array([int(packet.quic.packet_number) for packet in trace.packets])
for trace in self.traces
]
min_packet_number: int = map2d(min, packet_numbers)
max_packet_number: int = map2d(max, packet_numbers)
min_timestamp: np.float64 = map2d(min, timestamps)
max_timestamp: np.float64 = map2d(max, timestamps)
ax.set_xlim(left=min(0, min_timestamp), right=max_timestamp)
ax.set_ylim(bottom=min(0, min_packet_number), top=max_packet_number)
spinner.ok("✅")
with yaspin(text="plotting...", color="cyan") as spinner:
# plot shadow traces
for trace_timestamps, trace_packet_numbers in zip(
timestamps[1:], packet_numbers[1:]
):
ax.plot(
trace_timestamps,
trace_packet_numbers,
marker="o",
linestyle="",
color="#CCC",
)
# plot shadow traces
# plot main trace
for trace_timestamps, trace_packet_numbers in zip(
timestamps[1:], packet_numbers[1:]
):
ax.plot(
trace_timestamps,
trace_packet_numbers,
timestamps[0],
packet_numbers[0],
marker="o",
linestyle="",
color="#CCC",
color="#3465A4",
)
# plot main trace
ax.plot(
timestamps[0],
packet_numbers[0],
marker="o",
linestyle="",
color="#3465A4",
)
self._annotate_time_plot(ax, max_packet_number / 2)
self._save(fig)
self._annotate_time_plot(ax, max_packet_number / 2)
spinner.ok("✅")
self._save(fig)
def plot_file_size(self):
"""Plot the file size diagram."""
print(
f"Plotting {len(self.traces)} traces into a time vs. transmitted file size plot..."
)
fig: plt.Figure
ax: plt.Axes
fig, ax = plt.subplots(nrows=1, ncols=1)
......@@ -313,52 +315,61 @@ class PlotTimeSequenceCli:
# raise errors early
_ = trace.get_facts()
# drop GET request
timestamps = [
np.array([float(packet.sniff_timestamp) for packet in trace.packets[1:]])
for trace in self.traces
]
file_sizes = [
np.array([get_quic_payload_size(packet) for packet in trace.packets[1:]])
for trace in self.traces
]
accumulated_transmitted_file_size = [
np.cumsum(trace_file_sizes) for trace_file_sizes in file_sizes
]
min_file_size: int = map2d(min, accumulated_transmitted_file_size)
max_file_size: int = map2d(max, accumulated_transmitted_file_size)
min_timestamp = map2d(min, timestamps)
max_timestamp = map2d(max, timestamps)
ax.set_xlim(left=min(0, min_timestamp), right=max_timestamp)
ax.set_ylim(bottom=min(0, min_file_size), top=max_file_size)
ax.set_yticks(np.arange(0, max_file_size * 1.1, 1024 * 1024))
with yaspin(text="processing...", color="cyan") as spinner:
# drop GET request
timestamps = [
np.array(
[float(packet.sniff_timestamp) for packet in trace.packets[1:]]
)
for trace in self.traces
]
file_sizes = [
np.array(
[get_quic_payload_size(packet) for packet in trace.packets[1:]]
)
for trace in self.traces
]
accumulated_transmitted_file_size = [
np.cumsum(trace_file_sizes) for trace_file_sizes in file_sizes
]
min_file_size: int = map2d(min, accumulated_transmitted_file_size)
max_file_size: int = map2d(max, accumulated_transmitted_file_size)
min_timestamp = map2d(min, timestamps)
max_timestamp = map2d(max, timestamps)
ax.set_xlim(left=min(0, min_timestamp), right=max_timestamp)
ax.set_ylim(bottom=min(0, min_file_size), top=max_file_size)
ax.set_yticks(np.arange(0, max_file_size * 1.1, 1024 * 1024))
spinner.ok("✅")
with yaspin(text="plotting...", color="cyan") as spinner:
# plot shadow traces
for trace_timestamps, trace_file_sizes in zip(
timestamps[1:], accumulated_transmitted_file_size[1:]
):
ax.plot(
trace_timestamps,
trace_file_sizes,
marker="o",
linestyle="",
color="#CCC",
)
# plot shadow traces
# plot main trace
for trace_timestamps, trace_file_sizes in zip(
timestamps[1:], accumulated_transmitted_file_size[1:]
):
ax.plot(
trace_timestamps,
trace_file_sizes,
timestamps[0],
accumulated_transmitted_file_size[0],
marker="o",
linestyle="",
color="#CCC",
color="#3465A4",
)
# plot main trace
ax.plot(
timestamps[0],
accumulated_transmitted_file_size[0],
marker="o",
linestyle="",
color="#3465A4",
)
self._annotate_time_plot(ax, max_file_size / 2)
self._save(fig)
self._annotate_time_plot(ax, max_file_size / 2)
spinner.ok("✅")
self._save(fig)
def _save(self, fig: plt.Figure):
"""Save or show the plot."""
......@@ -372,12 +383,23 @@ class PlotTimeSequenceCli:
plt.close(fig)
def run(self):
if self.mode == PlotMode.SEQUENCE_NUMBER:
self.plot_sequence_number()
elif self.mode == PlotMode.FILE_SIZE:
self.plot_file_size()
else:
raise ValueError(f"Unrecognized plot mode {self.mode}")
mapping = {
PlotMode.SEQUENCE_NUMBER: {
"callback": self.plot_sequence_number,
"desc": "time vs. packet number",
},
PlotMode.FILE_SIZE: {
"callback": self.plot_file_size,
"desc": "time vs. transmitted file size",
},
}
cfg = mapping[self.mode]
callback = cfg["callback"]
desc = cfg["desc"]
print(f"Plotting {len(self.traces)} traces into a {desc} plot...")
callback()
def main():
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment