1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| import math,sys,os
""" 字体颜色 """ class bcolors: HEADER = '\033[95m' OKBLUE = '\033[94m' OKGREEN = '\033[92m' WARNING = '\033[93m' FAIL = '\033[91m' BOLD = '\033[1m' UNDERLINE = '\033[4m' END = '\033[0m '
""" 初始化日志 """ def createLog(): with open(resultPath,'a+',encoding='utf-8') as f: f.write("\n======================================================\n") f.write("%s\t%s\t%s\t%s\t%5s\t%5s\t%5s\n" %("type","Max","Min","Avg","PCT90","PCT95","PCT99"))
""" 获取路径所有文件 """ def getFiles(PATH): for root, dirs, files in os.walk(PATH): return files
""" 运算90、95、99百分位 1.接收要统计的项目名称,接收完整的路径,接收排序好的数组 """ def calc(proName,array): percent1 = 99 percent2 = 95 percent3 = 90 a1 = percent1/100 a2 = percent2/100 a3 = percent3/100
num_count = len(array)
maxValue = round(array[-1],2) mixValue = round(array[0],2) averageValue = round(sum(array)/len(array),2)
if math.ceil(num_count*a1) == num_count: percent_99 = round(array[num_count-1],2) else: percent_99 = round(array[math.ceil(num_count*a1)],2) if math.ceil(num_count*a2) == num_count: percent_95 = round(array[num_count-1],2) else: percent_95 = round(array[math.ceil(num_count*a2)],2) if math.ceil(num_count*a3) == num_count: percent_90 = round(array[num_count-1],2) else: percent_90 = round(array[math.ceil(num_count*a3)],2)
resultDict[proName] = [maxValue, mixValue, averageValue, percent_90, percent_95, percent_99]
with open(resultPath,'a+',encoding='utf-8') as f: f.write("%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n" %(proName,maxValue,mixValue,averageValue,percent_90,percent_95,percent_99))
return resultDict
""" 这段代码后期可以细化,重复代码可拿出来单写,return array
1.判断是buff/resp 2.读取buff/resp路径下的监控日志,排序后调用calc函数 """
def solve(): for i in fileList: array = [] logPath = PATH + i if os.path.getsize(logPath) == 0: for k in range(6): array.append(float(0)) array.sort() proName = i.split(".")[0] calc(proName,array) else: with open(logPath,'r+',encoding='utf-8') as f: for line in f.readlines(): if len(line) >= 15: print(bcolors.WARNING+"WARNING"+bcolors.END+logPath+logPath+"检测结果大于7位数,被抛弃") continue try: array.append(float(line)) except: print(bcolors.FAIL+"ERROR"+bcolors.END+logPath+"该值有问题,",line) array.sort() proName = i.split(".")[0] calc(proName,array)
if __name__ == '__main__': PATH = sys.argv[1] resultPath = PATH + 'result.txt' fileList = getFiles(PATH) if "result.txt" in fileList: fileList.remove("result.txt") key = [] resultDict = {} for i in fileList: key.append(i.split(".")[0]) resultDict = dict.fromkeys(key)
createLog() solve() """ [[[项目index],[监控值index]],[[项目index],[监控值index]], ...] 需要显示的数据 0 最大值 1 最小值 2 平均值 3 90% 4 95% 5 99% 优化,输入要监控的项目和值,自动生成二维数组 """ print('===============================================') keys = ['DMthreadCount','AudioLinstening','NLUthreadCount','DMcpu','DMmem','AudioListenCpu','AudioListenMem','NLUcpu','NLUmem'] indexes = [[0,4], [0,4], [0,4], [0,4], [0], [4], [4], [4], [0]] result = [] for key, idx in zip(keys, indexes): result.extend( (str(resultDict[key][i]) for i in idx))
for i in result: print(i,end="\t") print()
|