备份2
calc.py
# -*- coding: utf-8 -*- import re import os import sys def calc(fname): f = open(fname) source = f.read() f.close() r = r'node (.*)]:At time (.*)s on-off application sent packet (.*) with 64 bytes to 10.0.0.(.*) port 1919 total Tx (.*) bytes' s1 = re.findall(r,source) r = r'node (.*)]: At time (.*)s packet sink received packet (.*) with 64 bytes from 10.0.0.(.*) port .* total Rx (.*) bytes' s2 = re.findall(r,source) p = {} #一个字典:用于统计PDR和RTT for out in s1: p.update({out[2]:(float(out[1]),None)}) #packet x 发送时间 接收时间 #print out for out in s2: recv_t=p.get(out[2])[0] p.update({out[2]:(recv_t,float(out[1]))}) #print out #print p #计算投递率PDR 和 时延RTT totalsend=0 totalrecv=0 totalRTT=0 for i in p.values(): totalsend+=1 if i[1]: totalrecv+=1 totalRTT+=(i[1]-i[0]) PDR=-1.0 RTT=-1.0 if totalsend: PDR=float(totalrecv)/totalsend if totalrecv: RTT=totalRTT/totalrecv return PDR,RTT files = os.listdir('./') files.sort() dat={} for f in files: if(re.match('.*.out', f)): n = re.findall('(.*)_.*_.*_(.*).out',f)#得到节点数 r=calc(f) tmp=dat.get(n[0][0],None) if(tmp!=None): if(n[0][1]=='0'): dat.update({n[0][0]:(r[0],tmp[1])}) else: dat.update({n[0][0]:(tmp[0],r[0])}) else: if(n[0][1]=='0'): dat.update({n[0][0]:(r[0],0.0)}) else: dat.update({n[0][0]:(0.0,r[0])}) print 'PDR\t= {}\n'.format(r[0]),'aveRTT\t= {}s'.format(r[1]) out=file("1.dat","w+")#gnuplot画图用的dat文件 for i in dat: out.writelines(["{}\t{}\t{}\n".format(i,dat.get(i)[0],dat.get(i)[1])]) print dat.get(i) out.close()
1.plot
set term png font '/usr/common/fonts/simsun.ttc,14' set output "1.png" set xlabe l "节点数" set ylabe l "PDR" set title "仿真比较" set xrange [0.5:12.5] set xtics 1,1,12 plot "2.dat" u 1:2 w lp pt 5 lc rgbcolor "#2B60DE" axis x1y1 t "aodv", "2.dat" u 1:3 w lp pt 7 lc rgbcolor "#F62817" axis x1y2 t "greedy"