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