-
Notifications
You must be signed in to change notification settings - Fork 9
/
packet_size_cdf.py
executable file
·74 lines (61 loc) · 2.13 KB
/
packet_size_cdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright 2014-2015 Matthieu Baerts & Quentin De Coninck
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import argparse
import matplotlib
# Do not use any X11 backend
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import os
import pcapy
import statsmodels.api as sm
parser = argparse.ArgumentParser(
description="Summarize stat files generated by analyze")
parser.add_argument("pcap", help="path to the pcap to compute CDF")
args = parser.parse_args()
reader = pcapy.open_offline(args.pcap)
sizes = []
while True:
try:
(header, payload) = reader.next()
sizes.append(header.getlen())
except pcapy.PcapError:
break
plt.figure()
plt.clf()
fig, ax = plt.subplots()
graph_fname = os.getcwd() + "/cdf_size_packets_" + os.path.basename(os.path.splitext(args.pcap)[0]) + ".pdf"
try:
sample = np.array(sorted(sizes))
ecdf = sm.distributions.ECDF(sample)
x = np.linspace(min(sample), max(sample))
y = ecdf(x)
ax.step(x, y, color='b')
except ZeroDivisionError as e:
print(str(e))
# Shrink current axis's height by 10% on the top
box = ax.get_position()
ax.set_position([box.x0, box.y0,
box.width, box.height * 0.9])
# Put a legend above current axis
# ax.legend(loc='lower center', bbox_to_anchor=(0.5, 1.05), fancybox=True, shadow=True, ncol=len(aggl_res[cond]))
plt.xlabel('Size of packets in bytes', fontsize=18)
plt.savefig(graph_fname)
plt.close('all')