pyshark--throughput-analysis.py 1.77 KB
Newer Older
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
# The script analyzes directly a PCAP trace
# Remember to include the filter in display_filter
# CF - 2019

import pyshark
from datetime import datetime
import time
import math
import numpy as np
from matplotlib import pyplot as plt

###########################################
# 
# DATA Traffic (code 1)
#
###########################################

cap = pyshark.FileCapture('./compressed-trace.pcap', 
      display_filter="ip.src==10.16.0.2 && tcp.srcport==60204 && ip.dst==10.0.0.11 && tcp.dstport==5001")	

totPkt=0
totData=0

ref=float(cap[0].sniff_timestamp)
#print (ref)

# dictionary to store in form of key-value the tuple (time/amount of data in the given time)
# and compute throughput
thr_dict = {}

for pkt in cap:
	totData= totData+ int(pkt.tcp.len)
	totPkt= totPkt + 1
	#print (pkt.sniff_timestamp," ", pkt.tcp.len)

	# * * * *  to check the time
	#inDate=pkt.sniff_time
	#d=datetime.strptime(str(inDate), "%Y-%m-%d %H:%M:%S.%f")
	#print (time.mktime(d.timetuple()), " ", pkt.tcp.len)
	
	# doing the timediff from the first element to check time progress	
	timediff=float(pkt.sniff_timestamp)-ref
	i, d = divmod(timediff, 1)
	#print (i,pkt.tcp.len)


	# insertion in the dictionary: if the value does not exitst,
	# then we create the new tuple, otherwise we just sum the data field
	if i not in thr_dict:
		thr_dict[i] = int(pkt.tcp.len)
	else:
		thr_dict[i] += int(pkt.tcp.len)


# printing the dictionary
for key, value in thr_dict.items() :
    print (key, value)

# plot
keys = np.fromiter(thr_dict.keys(), dtype=float)
vals = np.fromiter(thr_dict.values(), dtype=float)
data=(((vals*8)/1024)/1024)
timeinterval=keys/1
plt.plot(timeinterval, data)

plt.xlabel("Time (s)", fontsize = 20)
plt.ylabel("Throughput (Mbit/s)", fontsize = 20)

plt.show()

#print("Tot pkt - tot data",totPkt,totData)