Python parsing qualnet. Trace [XML parsing]

Source: Internet
Author: User

Statistics on the total throughput/average latency/real-time throughput of each stream in qualnet. Trace should be useful for qualnet simulation.

Using lxml for parsing, the memory usage is half that of the built-in elementtree, and the speed is superb.

It is better to use the data stored in dict to change it to a class, but it is not difficult to change it. You can use it.

 

# 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.2 tree = 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 "% (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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.