A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
flowmon-parse-results.py
Go to the documentation of this file.
1
import
os
2
import
sys
3
4
try
:
5
from
xml.etree
import
cElementTree
as
ElementTree
6
except
ImportError:
7
from
xml.etree
import
ElementTree
8
9
10
def
parse_time_ns
(tm):
11
if
tm.endswith(
"ns"
):
12
return
float(tm[:-2])
13
raise
ValueError(tm)
14
15
16
## FiveTuple
17
class
FiveTuple
(
object
):
18
## class variables
19
## @var sourceAddress
20
# source address
21
## @var destinationAddress
22
# destination address
23
## @var protocol
24
# network protocol
25
## @var sourcePort
26
# source port
27
## @var destinationPort
28
# destination port
29
## @var __slots_
30
# class variable list
31
__slots_ = [
"sourceAddress"
,
"destinationAddress"
,
"protocol"
,
"sourcePort"
,
"destinationPort"
]
32
33
def
__init__
(self, el):
34
"""! The initializer.
35
@param self The object pointer.
36
@param el The element.
37
"""
38
self.
sourceAddress
= el.get(
"sourceAddress"
)
39
self.
destinationAddress
= el.get(
"destinationAddress"
)
40
self.
sourcePort
= int(el.get(
"sourcePort"
))
41
self.
destinationPort
= int(el.get(
"destinationPort"
))
42
self.
protocol
= int(el.get(
"protocol"
))
43
44
45
## Histogram
46
class
Histogram
(
object
):
47
## class variables
48
## @var bins
49
# histogram bins
50
## @var __slots_
51
# class variable list
52
__slots_ =
"bins"
,
"nbins"
,
"number_of_flows"
53
54
def
__init__
(self, el=None):
55
"""! The initializer.
56
@param self The object pointer.
57
@param el The element.
58
"""
59
self.
bins
= []
60
if
el
is
not
None
:
61
# self.nbins = int(el.get('nBins'))
62
for
bin
in
el.findall(
"bin"
):
63
self.
bins
.append(
64
(float(bin.get(
"start"
)), float(bin.get(
"width"
)), int(bin.get(
"count"
)))
65
)
66
67
68
## Flow
69
class
Flow
(
object
):
70
## class variables
71
## @var flowId
72
# delay ID
73
## @var delayMean
74
# mean delay
75
## @var packetLossRatio
76
# packet loss ratio
77
## @var rxBitrate
78
# receive bit rate
79
## @var txBitrate
80
# transmit bit rate
81
## @var packetSizeMean
82
# packet size mean
83
## @var probe_stats_unsorted
84
# unsirted probe stats
85
## @var hopCount
86
# hop count
87
## @var flowInterruptionsHistogram
88
# flow histogram
89
## @var rx_duration
90
# receive duration
91
## @var __slots_
92
# class variable list
93
__slots_ = [
94
"flowId"
,
95
"delayMean"
,
96
"packetLossRatio"
,
97
"rxBitrate"
,
98
"txBitrate"
,
99
"fiveTuple"
,
100
"packetSizeMean"
,
101
"probe_stats_unsorted"
,
102
"hopCount"
,
103
"flowInterruptionsHistogram"
,
104
"rx_duration"
,
105
]
106
107
def
__init__
(self, flow_el):
108
"""! The initializer.
109
@param self The object pointer.
110
@param flow_el The element.
111
"""
112
self.
flowId
= int(flow_el.get(
"flowId"
))
113
rxPackets = float(flow_el.get(
"rxPackets"
))
114
txPackets = float(flow_el.get(
"txPackets"
))
115
116
tx_duration = (
117
parse_time_ns
(flow_el.get(
"timeLastTxPacket"
))
118
-
parse_time_ns
(flow_el.get(
"timeFirstTxPacket"
))
119
) * 1e-9
120
rx_duration = (
121
parse_time_ns
(flow_el.get(
"timeLastRxPacket"
))
122
-
parse_time_ns
(flow_el.get(
"timeFirstRxPacket"
))
123
) * 1e-9
124
self.
rx_duration
= rx_duration
125
self.
probe_stats_unsorted
= []
126
if
rxPackets:
127
self.
hopCount
= float(flow_el.get(
"timesForwarded"
)) / rxPackets + 1
128
else
:
129
self.
hopCount
= -1000
130
if
rxPackets:
131
self.
delayMean
= float(flow_el.get(
"delaySum"
)[:-2]) / rxPackets * 1e-9
132
self.
packetSizeMean
= float(flow_el.get(
"rxBytes"
)) / rxPackets
133
else
:
134
self.
delayMean
=
None
135
self.
packetSizeMean
=
None
136
if
rx_duration > 0:
137
self.
rxBitrate
= float(flow_el.get(
"rxBytes"
)) * 8 / rx_duration
138
else
:
139
self.
rxBitrate
=
None
140
if
tx_duration > 0:
141
self.
txBitrate
= float(flow_el.get(
"txBytes"
)) * 8 / tx_duration
142
else
:
143
self.
txBitrate
=
None
144
lost = float(flow_el.get(
"lostPackets"
))
145
# print "rxBytes: %s; txPackets: %s; rxPackets: %s; lostPackets: %s" % (flow_el.get('rxBytes'), txPackets, rxPackets, lost)
146
if
rxPackets == 0:
147
self.
packetLossRatio
=
None
148
else
:
149
self.
packetLossRatio
= lost / (rxPackets + lost)
150
151
interrupt_hist_elem = flow_el.find(
"flowInterruptionsHistogram"
)
152
if
interrupt_hist_elem
is
None
:
153
self.
flowInterruptionsHistogram
=
None
154
else
:
155
self.
flowInterruptionsHistogram
=
Histogram
(interrupt_hist_elem)
156
157
158
## ProbeFlowStats
159
class
ProbeFlowStats
(
object
):
160
## class variables
161
## @var packets
162
# network packets
163
## @var bytes
164
# bytes
165
## @var __slots_
166
# class variable list
167
__slots_ = [
"probeId"
,
"packets"
,
"bytes"
,
"delayFromFirstProbe"
]
168
169
170
## Simulation
171
class
Simulation
(
object
):
172
## class variables
173
## @var flows
174
# list of flows
175
def
__init__
(self, simulation_el):
176
"""! The initializer.
177
@param self The object pointer.
178
@param simulation_el The element.
179
"""
180
self.
flows
= []
181
(FlowClassifier_el,) = simulation_el.findall(
"Ipv4FlowClassifier"
)
182
flow_map = {}
183
for
flow_el
in
simulation_el.findall(
"FlowStats/Flow"
):
184
flow =
Flow
(flow_el)
185
flow_map[flow.flowId] = flow
186
self.
flows
.append(flow)
187
for
flow_cls
in
FlowClassifier_el.findall(
"Flow"
):
188
flowId = int(flow_cls.get(
"flowId"
))
189
flow_map[flowId].fiveTuple =
FiveTuple
(flow_cls)
190
191
for
probe_elem
in
simulation_el.findall(
"FlowProbes/FlowProbe"
):
192
probeId = int(probe_elem.get(
"index"
))
193
for
stats
in
probe_elem.findall(
"FlowStats"
):
194
flowId = int(stats.get(
"flowId"
))
195
s =
ProbeFlowStats
()
196
s.packets = int(stats.get(
"packets"
))
197
s.bytes = float(stats.get(
"bytes"
))
198
s.probeId = probeId
199
if
s.packets > 0:
200
s.delayFromFirstProbe =
parse_time_ns
(
201
stats.get(
"delayFromFirstProbeSum"
)
202
) / float(s.packets)
203
else
:
204
s.delayFromFirstProbe = 0
205
flow_map[flowId].probe_stats_unsorted.append(s)
206
207
208
def
main(argv):
209
with
open(argv[1], encoding=
"utf-8"
)
as
file_obj:
210
print(
"Reading XML file "
, end=
" "
)
211
212
sys.stdout.flush()
213
level = 0
214
sim_list = []
215
for
event, elem
in
ElementTree.iterparse(file_obj, events=(
"start"
,
"end"
)):
216
if
event ==
"start"
:
217
level += 1
218
if
event ==
"end"
:
219
level -= 1
220
if
level == 0
and
elem.tag ==
"FlowMonitor"
:
221
sim =
Simulation
(elem)
222
sim_list.append(sim)
223
elem.clear()
# won't need this any more
224
sys.stdout.write(
"."
)
225
sys.stdout.flush()
226
print(
" done."
)
227
228
for
sim
in
sim_list:
229
for
flow
in
sim.flows:
230
t = flow.fiveTuple
231
proto = {6:
"TCP"
, 17:
"UDP"
}[t.protocol]
232
print(
233
"FlowID: %i (%s %s/%s --> %s/%i)"
234
% (
235
flow.flowId,
236
proto,
237
t.sourceAddress,
238
t.sourcePort,
239
t.destinationAddress,
240
t.destinationPort,
241
)
242
)
243
if
flow.txBitrate
is
None
:
244
print(
"\tTX bitrate: None"
)
245
else
:
246
print(
"\tTX bitrate: %.2f kbit/s"
% (flow.txBitrate * 1e-3,))
247
if
flow.rxBitrate
is
None
:
248
print(
"\tRX bitrate: None"
)
249
else
:
250
print(
"\tRX bitrate: %.2f kbit/s"
% (flow.rxBitrate * 1e-3,))
251
if
flow.delayMean
is
None
:
252
print(
"\tMean Delay: None"
)
253
else
:
254
print(
"\tMean Delay: %.2f ms"
% (flow.delayMean * 1e3,))
255
if
flow.packetLossRatio
is
None
:
256
print(
"\tPacket Loss Ratio: None"
)
257
else
:
258
print(
"\tPacket Loss Ratio: %.2f %%"
% (flow.packetLossRatio * 100))
259
260
261
if
__name__ ==
"__main__"
:
262
main(sys.argv)
flowmon-parse-results.FiveTuple
FiveTuple.
Definition
flowmon-parse-results.py:17
flowmon-parse-results.FiveTuple.sourcePort
sourcePort
source port
Definition
flowmon-parse-results.py:40
flowmon-parse-results.FiveTuple.protocol
protocol
network protocol
Definition
flowmon-parse-results.py:42
flowmon-parse-results.FiveTuple.sourceAddress
sourceAddress
class variablessource address
Definition
flowmon-parse-results.py:38
flowmon-parse-results.FiveTuple.destinationPort
destinationPort
destination port
Definition
flowmon-parse-results.py:41
flowmon-parse-results.FiveTuple.destinationAddress
destinationAddress
destination address
Definition
flowmon-parse-results.py:39
flowmon-parse-results.FiveTuple.__init__
__init__(self, el)
The initializer.
Definition
flowmon-parse-results.py:33
flowmon-parse-results.Flow
Flow.
Definition
flowmon-parse-results.py:69
flowmon-parse-results.Flow.flowInterruptionsHistogram
flowInterruptionsHistogram
flow histogram
Definition
flowmon-parse-results.py:153
flowmon-parse-results.Flow.txBitrate
txBitrate
transmit bit rate
Definition
flowmon-parse-results.py:141
flowmon-parse-results.Flow.packetSizeMean
packetSizeMean
packet size mean
Definition
flowmon-parse-results.py:132
flowmon-parse-results.Flow.rxBitrate
rxBitrate
receive bit rate
Definition
flowmon-parse-results.py:137
flowmon-parse-results.Flow.packetLossRatio
packetLossRatio
packet loss ratio
Definition
flowmon-parse-results.py:147
flowmon-parse-results.Flow.__init__
__init__(self, flow_el)
The initializer.
Definition
flowmon-parse-results.py:107
flowmon-parse-results.Flow.rx_duration
rx_duration
receive duration
Definition
flowmon-parse-results.py:124
flowmon-parse-results.Flow.delayMean
delayMean
mean delay
Definition
flowmon-parse-results.py:131
flowmon-parse-results.Flow.flowId
flowId
class variablesdelay ID
Definition
flowmon-parse-results.py:112
flowmon-parse-results.Flow.hopCount
hopCount
hop count
Definition
flowmon-parse-results.py:127
flowmon-parse-results.Flow.probe_stats_unsorted
probe_stats_unsorted
unsirted probe stats
Definition
flowmon-parse-results.py:125
flowmon-parse-results.Histogram
Histogram.
Definition
flowmon-parse-results.py:46
flowmon-parse-results.Histogram.__init__
__init__(self, el=None)
The initializer.
Definition
flowmon-parse-results.py:54
flowmon-parse-results.Histogram.bins
bins
class variableshistogram bins
Definition
flowmon-parse-results.py:59
flowmon-parse-results.ProbeFlowStats
ProbeFlowStats.
Definition
flowmon-parse-results.py:159
flowmon-parse-results.Simulation
Simulation.
Definition
flowmon-parse-results.py:171
flowmon-parse-results.Simulation.flows
flows
class variableslist of flows
Definition
flowmon-parse-results.py:180
flowmon-parse-results.Simulation.__init__
__init__(self, simulation_el)
The initializer.
Definition
flowmon-parse-results.py:175
object
flowmon-parse-results.parse_time_ns
parse_time_ns(tm)
Definition
flowmon-parse-results.py:10
src
flow-monitor
examples
flowmon-parse-results.py
Generated on Sat Dec 14 2024 09:20:39 for ns-3 by
1.11.0