计算后台日志的耗时时间`
`
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time, datetime
import sys
maxtime = "00000000000000000000"
mintime = "99999999999999999999"
def to_date(text):
if(len(text)==20):
return datetime.datetime.strptime(text, "%Y%m%d%H%M%S%f")
else:
print ("请检查日期格式")
def convert_file(filepath):
global maxtime,mintime
starttime = ""
endtime = ""
sum = 0
count = 0
with open(filepath,'r') as f:
for line in f.readlines():
line_split = line.split('|')
starttime =line_split[0].split(']')[-1] + line_split[1].replace(':', '')+'000'
if(starttime<mintime):
mintime = starttime
endtime = line_split[0].split(']')[-1] + line_split[2].replace(':', '')+'000'
if(endtime>maxtime):
maxtime = endtime
sum = sum + int(line_split[7])
count = count+1
#print( mintime, maxtime, sum, count)
return mintime, maxtime, sum, count
def total_seconds(time_delta):
return 1.0 * (time_delta.microseconds + (time_delta.seconds + time_delta.days * 24 * 3600) * 10**6) / 10**6
# parm = sys.argv[1]
# filepath = parm.strip()
filepath = 'C:/Users/Administrator/Desktop/1.txt'
result = convert_file(filepath)
time = to_date(result[1]) -to_date(result[0])
costtime = total_seconds(time)
print ("总记录数:[%d],平均耗时:[%.3f],总耗时:[%.3f]" % (result[3], float(result[2]/result[3]/1000), costtime))