summaryrefslogtreecommitdiff
path: root/code/perflow/parser.py
blob: dae0efe255aea508de1dbed6a42f2a49b235a44a (plain)
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
import csv

flowsAll = []

with open('tcpdata.csv', mode='r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    for row in csv_reader:
        flowsAll.append(float(row["Duration"]) / float(row["Packets"]))

with open('udpdata.csv', mode='r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    for row in csv_reader:
        flowsAll.append(float(row["Duration"]) / float(row["Packets"]))


with open('all_arrivel_time.csv', mode='w', newline='') as allArrival:
    fieldnames = ['time']
    writer = csv.DictWriter(allArrival, fieldnames=fieldnames)
    writer.writeheader()
    for flow in flowsAll:
        writer.writerow({'time': flow})

with open('packetdata.csv', mode='r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    flowsTCP = []
    flowsUDP = []
    for row in csv_reader:
        if row["Protocol"] == 'TCP':
            tcpFlowExists = False
            src = row["Source"]
            dest = row["Destination"]
            timeElapsed = float(row["Time delta from previous captured frame"])
            srcPort = row["Info"].split(' > ')[0].split(' ')[0]
            destPort = row["Info"].split(' > ')[1].split(' ')[1]
            for flow in flowsTCP:
                if src in flow and dest in flow and srcPort in flow and destPort in flow:
                    tcpFlowExists = True
                    flow[4] += timeElapsed
                    flow[5] += 1
                    break
            if not tcpFlowExists:
                flowsTCP.append([src, dest, srcPort, destPort, timeElapsed, 1])
        if row["Protocol"] == 'UDP':
            udpFlowExists = False
            src = row["Source"]
            dest = row["Destination"]
            timeElapsed = float(row["Time delta from previous captured frame"])
            srcPort = row["Info"].split(' > ')[0].split(' ')[0]
            destPort = row["Info"].split(' > ')[1].split(' ')[1]
            for flow in flowsUDP:
                if src in flow and dest in flow and srcPort in flow and destPort in flow:
                    udpFlowExists = True
                    flow[4] += timeElapsed
                    flow[5] += 1
                    break
            if not udpFlowExists:
                flowsUDP.append([src, dest, srcPort, destPort, timeElapsed, 1])

    flowsTCPTimeAverages = []
    flowsUDPTimeAverages = []

    for flow in flowsTCP:
        flowsTCPTimeAverages.append(flow[4] / flow[5])
    for flow in flowsUDP:
        flowsUDPTimeAverages.append(flow[4] / flow[5])


    with open('tcp_arrivel_time.csv', mode='w', newline='') as tcpArrival:
        fieldnames = ['time']
        writer = csv.DictWriter(tcpArrival, fieldnames=fieldnames)
        writer.writeheader()
        for flow in flowsTCPTimeAverages:
            writer.writerow({'time': flow})

    with open('udp_arrivel_time.csv', mode='w', newline='') as udpArrival:
        fieldnames = ['time']
        writer = csv.DictWriter(udpArrival, fieldnames=fieldnames)
        writer.writeheader()
        for flow in flowsUDPTimeAverages:
            writer.writerow({'time': flow})

    print(len(flowsTCPTimeAverages))
    print(len(flowsUDPTimeAverages))