我有一个像下面的文件
ElapsedTime2.68s: PlaceOrder
ElapsedTime2.69s: ClassARestCAll
ElapsedTime0.11s: GetOrderList
ElapsedTime0.11s: ClassARestCAll
ElapsedTime2.10s: PlaceOrder
ElapsedTime2.11s: ClassARestCAll
ElapsedTime0.10s: GetOrderList
ElapsedTime0.10s: ClassARestCAll
ElapsedTime2.00s: PlaceOrder
ElapsedTime2.01s: ClassARestCAll
ElapsedTime0.28s: GetOrderList
ElapsedTime0.28s: ClassARestCAll
ElapsedTime1.64s: PlaceOrder
ElapsedTime1.65s: ClassARestCAll
ElapsedTime0.11s: GetOrderList
ElapsedTime0.11s: ClassARestCAll
ElapsedTime1.99s: PlaceOrder
ElapsedTime2.01s: ClassARestCAll
如何解析文件以获得此结果?
average min max
ClassARestCAll 1.23 0.1 2.69
GetOrderList 0.15 0.1 0.28
PlaceOrder 2.082 1.64 2.68
我开发了一种使用 RE 和 List 操作来解决它的方法。但是,我的方法为每个新方法名称扫描整个列表一次。
我们如何通过仅扫描一次列表来获取所有 API 名称的统计信息。
import re
def get_stats(N, p_api):
list_of_rt = []
for line in N:
y= re.split("\s+", line)
if y[1] == p_api:
curr_rt = float(y[0][11:-2])
list_of_rt.append(curr_rt)
min_rt ,max_rt = min(list_of_rt), max(list_of_rt)
total_rt, total_cnt = sum(list_of_rt), len(list_of_rt)
print p_api, min_rt, max_rt, "%.3f" %round(total_rt/total_cnt,3), total_cnt
ifile = open('data1.txt','r').read()
api_rts= re.findall(r'ElapsedTime\d*.\d*s: \S*',ifile)
list_of_api_names = []
for api_rt in api_rts:
y= re.split("\s+", api_rt)
list_of_api_names.append(y[1])
#get distinct list of API names
distinct_apis = set(list_of_api_names)
print 'api min, max, average, total occurences'
# for each API name call get_stat
for api in distinct_apis:
get_stats(api_rts ,api)