randomlog_compared.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # coding: utf-8
  2. import re
  3. import sys,os
  4. import time
  5. import dateutil, pylab,random
  6. from pylab import *
  7. import datetime
  8. class Logstatistics(object):
  9. def __init__(self):
  10. self.log_base = r'C:\Users\Administrator\Desktop\minions'
  11. self.date = '11-25'
  12. self.log_name = 'random_danmu.log'
  13. self.base_log_path = r'C:\Users\Administrator\Desktop\lags'
  14. self.chatroom_log = os.path.join(self.log_base,self.date+self.log_name)
  15. def writelog(self,logname,content):
  16. with open(os.path.join(self.base_log_path,logname),'w') as f:
  17. f.write(content)
  18. def log_list(self):
  19. self.logs = []
  20. for root,dirs,files in os.walk(self.log_base):
  21. if self.date+self.log_name in files and root != self.log_base:
  22. self.logs.append(os.path.join(root,self.date+self.log_name))
  23. return self.logs
  24. def compare(self):
  25. chatroom_result,name,count = self.build_dict(self.chatroom_log)
  26. for log in self.logs:
  27. d,point_name,logcount = self.build_dict(log)
  28. times = []
  29. lags = []
  30. n = 0
  31. #for key,values in d.items():
  32. for key,values in sorted(d.items(),key = lambda d:d[1][0]):
  33. if values[-1] > '1':
  34. try:
  35. times.append(datetime.datetime.strptime(values[0].split()[0],'%H:%M:%S.%f'))
  36. lags.append(int(values[-1]))
  37. except Exception,e:
  38. print e
  39. pass
  40. if key not in chatroom_result:
  41. n+=1
  42. pylab.plot_date(pylab.date2num(times), lags, linestyle='-')
  43. text(24,120, '')
  44. pylab.rc('font',size = 8)
  45. xtext = xlabel(u'time')
  46. ytext = ylabel(u'lag: s')
  47. title_string = point_name + '\n' + 'lost package: ' + str(n) + '\n' + 'lost percent: ' + str('%.2f' % float(float(n)/float(logcount)))
  48. ttext = title(title_string)
  49. grid(True)
  50. setp(ttext, size='large', color='r')
  51. setp(xtext, size='medium', weight='bold', color='g')
  52. setp(ytext, size='medium', weight='light', color='g')
  53. savefig(os.path.join(self.base_log_path , (point_name + '.png')),dpi = 120)
  54. #show()
  55. pylab.cla() #clear any prior graph
  56. print point_name,n,logcount,float(n)/float(logcount)
  57. #print point_name,n,'%.2f' % float(float(n)/float(logcount))
  58. # elif switch == 'lag':
  59. # strings = ''
  60. # l= []
  61. # for values in d.values():
  62. # mi_co = values[-1]
  63. # if int(mi_co) > 0 :
  64. # l.append(values)
  65. # l.sort(key=lambda x:int(x[2]),reverse=True)
  66. # for each in l:
  67. # string = '\t'.join(each)
  68. # strings += string + '\n'
  69. # self.writelog(point_name,strings)
  70. def build_dict(self,file):
  71. point_name = file.split(self.log_base)[1].split('\\')[1]
  72. d = {}
  73. with open(file,'r') as f:
  74. n = 0
  75. try:
  76. for line in f:
  77. if line[:-1].strip():
  78. n += 1
  79. line = line.strip()
  80. split_line = line.split()
  81. id = split_line[5]
  82. ci = split_line[7].replace(',','')
  83. time = split_line[1].replace(']:','')
  84. mi_co = split_line[-1]
  85. d[id] = (time,ci,mi_co)
  86. except:
  87. print line
  88. pass
  89. return d,point_name,n
  90. def log_result(self):
  91. self.compare(switch='lag')
  92. def plot(self):
  93. pass
  94. A = Logstatistics()
  95. A.log_list()
  96. #print A.compare(switch='lag') #write log
  97. print A.compare()