統計Qualnet.trace裡各條流的總吞吐率/平均延遲/即時吞吐率,對做Qualnet模擬的應該會有用。
使用lxml解析,占記憶體比內建的ElementTree少一半,速度也是杠杠的
用dict存的資料,改成class更好,也不難,不過懶得改了,能用就行
#import xml.etree.ElementTree as etreefrom lxml import etreeimport stringtp =dict()tps = dict()start_time = dict()end_time = dict()delay = dict()pkgs = dict()node = dict()cbr_num = dict()dt = 0.2tree = etree.parse('Qualnet.trace')print "load success"root = tree.getroot()recs = root[1].findall('rec')print "traverse rec"for rec in recs: rechdr = rec.find('rechdr') head = str(rechdr.text).split() #print("head"+rechdr.text) now_time = float(head[2]) #print("first_time "+first_time) action = int(str(rechdr.find('action').text).split()[0]) #print(action) if action == 2: recbody = rec.find('recbody') cbrs = recbody.findall('cbr') for cbr in cbrs : cbrstr = str(cbr.text).split() name = cbrstr[0] size = float(cbrstr[3]) size *= 8 send_time = float(cbrstr[4]) if name not in node: node[name]= head[4] if name not in cbr_num: cbr_num[name] = cbrstr[0] if name not in start_time: start_time[name]=now_time end_time[name]=now_time if name in tp: tp[name]+=size else: tp[name]=size pps = dict() for i in range(0,int(250/dt)): pps[i]=0 tps[name]=pps tps[name][int(now_time/dt)]+=size if name in delay: delay[name]+=now_time-send_time else: delay[name]=now_time-send_time if name in pkgs: pkgs[name]+=1 else: pkgs[name]=1 #print(size)for k in tp: tp[k]=tp[k]/(end_time[k]-start_time[k]) delay[k]=delay[k]/pkgs[k] for m in tps[k]: tps[k][m] /= dt print "%s.%s: %f %f" % (node[k], cbr_num[k], tp[k], delay[k]) #print tps[k] f = file(str(node[k])+'.'+cbr_num[k],'w') for n in tps[k]: #print >> f,"%10.3f\n"%(tps[k][n]) f.write(str(tps[k][n])+'\n') f.close()#print (tp[k])# print (delay[k]) print tpprint delay