from xml.dom.minidom import parse
import xml.dom.minidom
# 使用minidom解析器打开 XML 文档
DOMTree = xml.dom.minidom.parse("xmls/flowmonitor_dsdv_5.xml")
flowMonitor = DOMTree.documentElement
# 在集合中获取所有
flows = flowMonitor.getElementsByTagName("Flow")
# 定义变量总字节数、总丢包数、总传输包数、总接收字节数、总时延
totalBytes = 0
totalLostPackets = 0
totalTxPackets = 0
totalRxBytes = 0
delaySum = 0
count = 0
# 计算
for flow in flows:
if flow.hasAttribute("timeFirstRxPacket") and int(flow.getAttribute("rxPackets")) != 0:
totalLostPackets += int(flow.getAttribute("lostPackets"))
totalTxPackets += int(flow.getAttribute("txPackets"))
totalRxBytes += int(flow.getAttribute("rxBytes"))
delaySum += float(flow.getAttribute("delaySum")[:-2])/1000000 / int(flow.getAttribute("rxPackets"))
count += 1
print("totalBytes:", totalBytes)
print("totalLostPackets:", totalLostPackets)
print("totalTxPackets", totalTxPackets)
print("totalRxBytes", totalRxBytes)
print("delaySum", delaySum)
lsp = totalLostPackets / totalTxPackets
throughOutput = totalRxBytes * 8 / 100 / 1000
delay = delaySum / count
print("丢包率(%):", lsp)
print("吞吐量(kbps):", throughOutput)
print("时延(ms):", delay)