标签:log python ret num file interface path line 时长
1 import time 2 import re 3 import os 4 5 6 def read_file(path, newfile, ret): 7 """ 8 读取path文件夹里面的所有文件,并在path的父目录下新建newfile文件,利用正则表达式将匹配到的内容写入到newfile文件中 9 :param path: 要读取的文件夹 10 :param newfile: 要存放的文件名 11 :param ret: 正则表达式,用于提取需要的数据 12 :return: 利用正则表达式将文件写入完成后,返回newfile的绝对路径 13 """ 14 files = os.listdir(path) 15 current_dir = os.path.dirname(path) 16 filepath = current_dir + newfile 17 if not os.path.exists(filepath): 18 for file in files: 19 file = path + '\\' + file 20 with open(file, 'r', encoding='utf-8') as f: 21 with open(filepath, 'a', encoding='utf-8') as fb: 22 for line in f: 23 res = re.search(ret, line) 24 if res is not None: 25 fb.write(line) 26 else: 27 os.remove(filepath) 28 for file in files: 29 file = path + '\\' + file 30 with open(file, 'r', encoding='utf-8') as f: 31 with open(filepath, 'a', encoding='utf-8') as fb: 32 for line in f: 33 res = re.search(ret, line) 34 if res is not None: 35 fb.write(line) 36 return filepath 37 38 39 def read_log(path, newpath, ret, split): 40 """ 41 调用函数read_file()读取path文件夹里面的所有文件,并在path的父目录下新建newfile文件,利用正则表达式将匹配到的内容写入到newfile文件中。 42 读取newfile文件的内容,将需要的内容用正则分割(e.g.:接口号,接口调用时长),提取需要的内容 43 :param path: 要读取的文件夹 44 :param newpath: 要存放的文件名 45 :param ret: 正则表达式,用于提取需要的数据 46 :param split: 正在表达式,将提取到的数据进行分割 47 :return: 统计分割后的数据的第一个值和第二个值 48 """ 49 global interface_dict 50 start = time.time() 51 file = read_file(path, newpath, ret) 52 end = time.time() 53 print("函数read_file()运行耗时共:%.2f秒" % (end - start)) 54 if os.path.exists(file): 55 n = 0 56 avg = 0 57 count = 0 58 interface_sum = {} 59 interface_dict = {} 60 with open(file, 'r', encoding='utf-8') as f: 61 for line in f: 62 res = re.search(ret, line) 63 if res is not None: 64 ctime = re.search(ret, line).group() 65 num = re.split(split, ctime) 66 if num[0] in interface_sum: 67 interface_value = int(num[1]) 68 interface_sum[num[0]] += interface_value 69 interface_dict[num[0]] += 1 70 else: 71 interface_value = int(num[1]) 72 interface_sum[num[0]] = interface_value 73 interface_dict[num[0]] = 1 74 count += int(num[1]) 75 n += 1 76 avg = count / n 77 interface_list = sorted(interface_dict.items(), key=lambda x: x[1]) 78 for d in interface_list: 79 if d[1] > 1: 80 interface_s = interface_sum[d[0]] 81 interface_avg = interface_s / d[1] 82 print('接口%s调用总次数为:%d次,接口总调用时长为:%dms,平均时长为:%.2fms' % (d[0], d[1], interface_s, interface_avg)) 83 end = time.time() 84 print('函数read_log()运行耗时共:%.2f秒' % (end - start)) 85 return '调用接口总次数为:%d次,接口运行总时长为:%dms,平均时长为:%.2fms' % (n, count, avg) 86 else: 87 return '文件不存在或没有权限访问!请管理员检查!' 88 89 90 c = read_log(r'E:\common', 'common.log', '[GCPU][0-9]*\s+执行时间\s+[0-9]*', '\s+执行时间\s+') 91 print(c)
标签:log,python,ret,num,file,interface,path,line,时长 来源: https://www.cnblogs.com/chen/p/11904586.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。