标签:time output mat 开始时间 instance lin art app count
#encoding=gbk import os import re import math from os import path ‘‘‘ 手动输入文件nmon文件路径,要截取的开始时间,结束时间 ‘‘‘ rootdir=‘E:\\pylianxi\\ceshi‘ #input("请输入文件路径:") start_time="14:46" #截取的开始时间 end_time="14:56" #截取的结束时间 print(rootdir) def avg(list_a): #求平均值函数 avg_list=0.0 float_item_list=[float(i) for i in list_a] for i in float_item_list: if not isinstance(i,(int,float)): print("列表中含有非数字!!") return None if len(float_item_list)>0: avg_list=sum(float_item_list)/len(float_item_list) #return round(avg_list,3) return (‘%.2f‘%avg_list) else: return None write_count=0 #记录是否是第一次生成要输出的文件aa.txt file_names=os.listdir(rootdir) for i in file_names: file_path=path.join(rootdir,i) if path.isfile(file_path) and ".nmon" in i: print(file_path) #file_record=re.findall(r"\w+",i)[0]+re.findall(r"\d+",i)[0] file_record=i[:6] #+i[4:6] #file_record=re.search(r"(\w+)-(\d+)",i).group(1)+re.search(r"(\w+)-(\d+)",i).group(2) print(file_record) #with open(file_path,‘r‘,encoding=‘gbk‘) as fp: with open(file_path,‘r‘,encoding=‘utf-8‘) as fp: data_all=fp.readlines() data_times={} for j in range(len(data_all)): if re.search(r"ZZZZ,T",data_all[j]): #data_times.append(data_all[j].split(‘,‘)[2]) data_times[j]=data_all[j].split(‘,‘)[2] #print(data_times) start_row=0 end_row=max(data_times.keys()) for k,v in data_times.items(): if start_time in v: start_row=k print("循环中的start_row:",start_row) break for k,v in data_times.items(): if end_time in v: end_row=k print("循环中的end_row:",end_row) break print("校验前的start_row:",start_row) if start_row==0: print("未找到开始时间,从文件头开始") print("校验前的end_row:",end_row) if end_row==max(data_times.keys()): print("未找到结束时间,计算至文件结尾") performance_cpu=[] performance_mem=[] performance_disk_single=[] performance_disk=[] for n in data_all[start_row:end_row]: if re.search(r"CPU_ALL,T.*",n): #print(n.split(",")[5]) performance_cpu.append(100-float(n.split(",")[5])) if re.search(r"MEM,T.*",n): performance_mem.append((float(n.split(",")[2])-float(n.split(",")[6])-float(n.split(",")[11]))*100/float(n.split(",")[2])) if re.search(r"DISKXFER,T.*",n): performance_disk_single=n.split(",")[2:] sum_disk=0.0 for nn in performance_disk_single: sum_disk+=float(nn) performance_disk.append(sum_disk) #print(performance_cpu) #print(performance_mem) #print(performance_disk) cpu_avg=avg(performance_cpu) mem_avg=avg(performance_mem) disk_avg=avg(performance_disk) output_filename=‘aa.txt‘ output_path=path.join(rootdir,output_filename) if write_count==0: fp1=open(output_path,‘w‘,encoding=‘gbk‘) fp1.write("") write_count+=1 fp1.close() with open(output_path,‘a+‘,encoding=‘gbk‘) as fp2: fp2.write(file_record+‘,‘+str(cpu_avg)+‘,‘+str(mem_avg)+‘,‘+str(disk_avg)+‘\n‘)
标签:time output mat 开始时间 instance lin art app count
原文地址:https://www.cnblogs.com/xiaoxiao075/p/10186818.html