A Discrete-Event Network Simulator
API
flowmon-parse-results.py
Go to the documentation of this file.
1 from __future__ import division
2 
3 import os
4 import sys
5 
6 try:
7  from xml.etree import cElementTree as ElementTree
8 except ImportError:
9  from xml.etree import ElementTree
10 
11 
12 def parse_time_ns(tm):
13  if tm.endswith("ns"):
14  return float(tm[:-2])
15  raise ValueError(tm)
16 
17 
18 
20 
33  __slots_ = ["sourceAddress", "destinationAddress", "protocol", "sourcePort", "destinationPort"]
34 
35  def __init__(self, el):
36  """! The initializer.
37  @param self The object pointer.
38  @param el The element.
39  """
40  self.sourceAddresssourceAddress = el.get("sourceAddress")
41  self.destinationAddressdestinationAddress = el.get("destinationAddress")
42  self.sourcePortsourcePort = int(el.get("sourcePort"))
43  self.destinationPortdestinationPort = int(el.get("destinationPort"))
44  self.protocolprotocol = int(el.get("protocol"))
45 
46 
47 
49 
54  __slots_ = "bins", "nbins", "number_of_flows"
55 
56  def __init__(self, el=None):
57  """! The initializer.
58  @param self The object pointer.
59  @param el The element.
60  """
61  self.binsbins = []
62  if el is not None:
63  # self.nbins = int(el.get('nBins'))
64  for bin in el.findall("bin"):
65  self.binsbins.append(
66  (float(bin.get("start")), float(bin.get("width")), int(bin.get("count")))
67  )
68 
69 
70 
71 class Flow(object):
72 
95  __slots_ = [
96  "flowId",
97  "delayMean",
98  "packetLossRatio",
99  "rxBitrate",
100  "txBitrate",
101  "fiveTuple",
102  "packetSizeMean",
103  "probe_stats_unsorted",
104  "hopCount",
105  "flowInterruptionsHistogram",
106  "rx_duration",
107  ]
108 
109  def __init__(self, flow_el):
110  """! The initializer.
111  @param self The object pointer.
112  @param flow_el The element.
113  """
114  self.flowIdflowId = int(flow_el.get("flowId"))
115  rxPackets = float(flow_el.get("rxPackets"))
116  txPackets = float(flow_el.get("txPackets"))
117 
118  tx_duration = (
119  parse_time_ns(flow_el.get("timeLastTxPacket"))
120  - parse_time_ns(flow_el.get("timeFirstTxPacket"))
121  ) * 1e-9
122  rx_duration = (
123  parse_time_ns(flow_el.get("timeLastRxPacket"))
124  - parse_time_ns(flow_el.get("timeFirstRxPacket"))
125  ) * 1e-9
126  self.rx_durationrx_duration = rx_duration
127  self.probe_stats_unsortedprobe_stats_unsorted = []
128  if rxPackets:
129  self.hopCounthopCount = float(flow_el.get("timesForwarded")) / rxPackets + 1
130  else:
131  self.hopCounthopCount = -1000
132  if rxPackets:
133  self.delayMeandelayMean = float(flow_el.get("delaySum")[:-2]) / rxPackets * 1e-9
134  self.packetSizeMeanpacketSizeMean = float(flow_el.get("rxBytes")) / rxPackets
135  else:
136  self.delayMeandelayMean = None
137  self.packetSizeMeanpacketSizeMean = None
138  if rx_duration > 0:
139  self.rxBitraterxBitrate = float(flow_el.get("rxBytes")) * 8 / rx_duration
140  else:
141  self.rxBitraterxBitrate = None
142  if tx_duration > 0:
143  self.txBitratetxBitrate = float(flow_el.get("txBytes")) * 8 / tx_duration
144  else:
145  self.txBitratetxBitrate = None
146  lost = float(flow_el.get("lostPackets"))
147  # print "rxBytes: %s; txPackets: %s; rxPackets: %s; lostPackets: %s" % (flow_el.get('rxBytes'), txPackets, rxPackets, lost)
148  if rxPackets == 0:
149  self.packetLossRatiopacketLossRatio = None
150  else:
151  self.packetLossRatiopacketLossRatio = lost / (rxPackets + lost)
152 
153  interrupt_hist_elem = flow_el.find("flowInterruptionsHistogram")
154  if interrupt_hist_elem is None:
155  self.flowInterruptionsHistogramflowInterruptionsHistogram = None
156  else:
157  self.flowInterruptionsHistogramflowInterruptionsHistogram = Histogram(interrupt_hist_elem)
158 
159 
160 
162 
169  __slots_ = ["probeId", "packets", "bytes", "delayFromFirstProbe"]
170 
171 
172 
174 
177  def __init__(self, simulation_el):
178  """! The initializer.
179  @param self The object pointer.
180  @param simulation_el The element.
181  """
182  self.flowsflows = []
183  (FlowClassifier_el,) = simulation_el.findall("Ipv4FlowClassifier")
184  flow_map = {}
185  for flow_el in simulation_el.findall("FlowStats/Flow"):
186  flow = Flow(flow_el)
187  flow_map[flow.flowId] = flow
188  self.flowsflows.append(flow)
189  for flow_cls in FlowClassifier_el.findall("Flow"):
190  flowId = int(flow_cls.get("flowId"))
191  flow_map[flowId].fiveTuple = FiveTuple(flow_cls)
192 
193  for probe_elem in simulation_el.findall("FlowProbes/FlowProbe"):
194  probeId = int(probe_elem.get("index"))
195  for stats in probe_elem.findall("FlowStats"):
196  flowId = int(stats.get("flowId"))
197  s = ProbeFlowStats()
198  s.packets = int(stats.get("packets"))
199  s.bytes = float(stats.get("bytes"))
200  s.probeId = probeId
201  if s.packets > 0:
202  s.delayFromFirstProbe = parse_time_ns(
203  stats.get("delayFromFirstProbeSum")
204  ) / float(s.packets)
205  else:
206  s.delayFromFirstProbe = 0
207  flow_map[flowId].probe_stats_unsorted.append(s)
208 
209 
210 def main(argv):
211  with open(argv[1], encoding="utf-8") as file_obj:
212  print("Reading XML file ", end=" ")
213 
214  sys.stdout.flush()
215  level = 0
216  sim_list = []
217  for event, elem in ElementTree.iterparse(file_obj, events=("start", "end")):
218  if event == "start":
219  level += 1
220  if event == "end":
221  level -= 1
222  if level == 0 and elem.tag == "FlowMonitor":
223  sim = Simulation(elem)
224  sim_list.append(sim)
225  elem.clear() # won't need this any more
226  sys.stdout.write(".")
227  sys.stdout.flush()
228  print(" done.")
229 
230  for sim in sim_list:
231  for flow in sim.flows:
232  t = flow.fiveTuple
233  proto = {6: "TCP", 17: "UDP"}[t.protocol]
234  print(
235  "FlowID: %i (%s %s/%s --> %s/%i)"
236  % (
237  flow.flowId,
238  proto,
239  t.sourceAddress,
240  t.sourcePort,
241  t.destinationAddress,
242  t.destinationPort,
243  )
244  )
245  if flow.txBitrate is None:
246  print("\tTX bitrate: None")
247  else:
248  print("\tTX bitrate: %.2f kbit/s" % (flow.txBitrate * 1e-3,))
249  if flow.rxBitrate is None:
250  print("\tRX bitrate: None")
251  else:
252  print("\tRX bitrate: %.2f kbit/s" % (flow.rxBitrate * 1e-3,))
253  if flow.delayMean is None:
254  print("\tMean Delay: None")
255  else:
256  print("\tMean Delay: %.2f ms" % (flow.delayMean * 1e3,))
257  if flow.packetLossRatio is None:
258  print("\tPacket Loss Ratio: None")
259  else:
260  print("\tPacket Loss Ratio: %.2f %%" % (flow.packetLossRatio * 100))
261 
262 
263 if __name__ == "__main__":
264  main(sys.argv)
def __init__(self, el)
The initializer.
sourceAddress
class variablessource address
def __init__(self, flow_el)
The initializer.
flowId
class variablesdelay ID
probe_stats_unsorted
unsirted probe stats
def __init__(self, el=None)
The initializer.
bins
class variableshistogram bins
flows
class variableslist of flows
def __init__(self, simulation_el)
The initializer.