Python解析Qualnet.trace[xml解析]

來源:互聯網
上載者:User

統計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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.