-
Notifications
You must be signed in to change notification settings - Fork 1
/
throughput_analysis.py
129 lines (116 loc) · 3.6 KB
/
throughput_analysis.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import dpkt
import math
import numpy as np
import os
import sys
from tqdm import tqdm
bucket_size=0.1 # in seconds
for alg in ['pcc']:
pcap = dpkt.pcap.Reader(open(alg+'-trace', 'rb'))
bucket_start = -1
buckets, bucket, jain = {}, {}, {}
prev_bucket = []
flows = {}
for ts, buf in tqdm(pcap):
if bucket_start + bucket_size <= ts or bucket_start == -1:
for x in bucket:
bucket[x] /= ts - bucket_start
if bucket != []:
buckets[ts] = bucket
tpts = [bucket[x] for x in bucket]
if len(prev_bucket) != len(bucket):
# Ignore these border cases for jain index as they may be inaccurate
pass
else:
if tpts != []:
jain[ts] = sum(tpts) ** 2 / (len(tpts) * sum([x ** 2 for x in tpts]))
else: jain[ts] = 0
prev_bucket = bucket
bucket_start = ts
bucket = {}
try:
eth = dpkt.ethernet.Ethernet(buf)
except:
continue
if type(eth.data) == str or type(eth.data.data) == str:
continue
if type(eth.data.data) != dpkt.tcp.TCP and type(eth.data.data) != dpkt.udp.UDP:
continue
if eth.data.data.dport in [5001, 8888, 9000]:
if eth.data.data.sport not in flows:
flows[eth.data.data.sport] = 1
if eth.data.data.sport not in bucket:
bucket[eth.data.data.sport] = 0
bucket[eth.data.data.sport] += len(buf)
tptfilename = '' + alg + '-trace' + "-tpt.dat"
tptpolyfilename = '' + alg + '-trace' + "-tptpoly.dat"
jainfilename = '' + alg + '-trace' + "-jain.dat"
tptfile = open(tptfilename, 'w')
tptpolyfile = open(tptpolyfilename, 'w')
jainfile = open(jainfilename, 'w')
timestamps = [x for x in buckets]
timestamps.sort()
flows = [x for x in flows]
flows.sort()
print(len(flows))
print(flows)
start_time = timestamps[0]
for ts in timestamps:
out = str(ts - start_time) + " "
for x in flows:
if x in buckets[ts]:
out += str(buckets[ts][x] * 8e-6) + " "
else:
out += "0 "
tptfile.write(out + "\n")
if ts in jain:
jainfile.write(str(ts - start_time) + " " + str(jain[ts]) + "\n")
print("here")
for ts in timestamps:
tpts = [buckets[ts][x] for x in buckets[ts]]
pltpt = 8e-6 * (np.mean(tpts) + np.std(tpts))
if math.isnan(pltpt): continue
if pltpt < 0: pltpt = 0
tptpolyfile.write("%f %f\n" % (ts - start_time, pltpt))
for ts in timestamps[::-1]:
tpts = [buckets[ts][x] for x in buckets[ts]]
pltpt = 8e-6 * (np.mean(tpts) - np.std(tpts))
if math.isnan(pltpt): continue
if pltpt < 0: pltpt = 0
tptpolyfile.write("%f %f\n" % (ts - start_time, pltpt))
#exit()
tptgnufilename = '' + alg + '-trace' + "-tpt.gnuplot"
tptgnufile = open(tptgnufilename, 'w')
tptgnufile.write("""
set terminal svg;
set output '%s';
set title "Dynamic behavior";
set ylabel 'Throughput (Mbit/s)';
set xlabel 'Time (s)';
set xrange [0:105];
set yrange [1:100];
set logscale y;
set key off;
""" % ('' + alg + '-trace' + "-tpt.svg"))
tptgnucmd = "plot "
for i in range(len(flows)):
tptgnucmd += "'%s' using 1:%d with lines, " % (tptfilename, i+2)
tptgnufile.write(tptgnucmd)
tptgnufile.close
jaingnufilename = '' + alg + '-trace' + "-jain.gnuplot"
jaingnufile = open(jaingnufilename, 'w')
jaingnufile.write("""
set terminal svg;
set output '%s';
set title "Dynamic behavior";
set ylabel 'Jain index';
set xlabel 'Time (s)';
set xrange [0:105];
set yrange [0:1];
set key off;
plot '%s' using 1:2 with lines
""" % ('' + alg + '-trace' + "-jain.svg", jainfilename))
print("gnuplot -p %s" % tptgnufilename)
print("inkscape -A %s %s" % ('' + alg + '-trace' + "-tpt.pdf", '' + alg + '-trace' + "-tpt.svg"))
print("gnuplot -p %s" % jaingnufilename)
print("inkscape -A %s %s" % ('' + alg + '-trace' + "-jain.pdf", '' + alg + '-trace' + "-jain.svg"))