@@ -98,11 +98,15 @@ def parse_log_file(target_file, log_format):
9898 # 请求URL
9999 urls = []
100100 # 请求响应时间
101- cost_times_list = []
102- if 'cost_time_index' in log_format .keys () and config .cost_time_flag :
103- cost_times_flag = True
104- else :
105- cost_times_flag = False
101+ cost_time_list = []
102+ cost_time_flag = False
103+ cost_time_percentile_flag = False
104+ if 'cost_time_index' in log_format .keys ():
105+ if config .cost_time_flag :
106+ cost_time_flag = True
107+ if config .cost_time_percentile_flag :
108+ cost_time_percentile_flag = True
109+
106110 # 请求方法计数器
107111 method_counts = {'post' : 0 , 'post_percentile' : 0 , 'get' : 0 , 'get_percentile' : 0 }
108112
@@ -136,8 +140,8 @@ def parse_log_file(target_file, log_format):
136140 method_counts ['get' ] += 1
137141 protocol = match .group (log_format .get ('protocol_index' ))
138142 urls .append (method + ' ' + url + ' ' + protocol )
139- if cost_times_flag :
140- cost_times_list .append ({'time' : log_time , 'cost_time' : int (float (match .group (log_format .get ('cost_time_index' )))* 1000 )})
143+ if 'cost_time_index' in log_format . keys () :
144+ cost_time_list .append ({'time' : log_time , 'cost_time' : int (float (match .group (log_format .get ('cost_time_index' )))* 1000 )})
141145
142146 # 计算PV、UV、平均请求数、GET/POST占比
143147 pv = len (times )
@@ -200,7 +204,7 @@ def parse_log_file(target_file, log_format):
200204 # 统计不同响应时间范围的请求数量
201205 cost_time_range = {'r1' : 0 , 'r2' : 0 , 'r3' : 0 , 'r4' : 0 , 'r5' : 0 , 'r6' : 0 ,
202206 'r7' : 0 , 'r8' : 0 , 'r9' : 0 , 'r10' : 0 , 'r11' : 0 }
203- for cost_time in cost_times_list :
207+ for cost_time in cost_time_list :
204208 if cost_time ['cost_time' ] <= 50 :
205209 cost_time_range ['r1' ] += 1
206210 elif 50 < cost_time ['cost_time' ] <= 100 :
@@ -223,12 +227,11 @@ def parse_log_file(target_file, log_format):
223227 cost_time_range ['r10' ] += 1
224228 else :
225229 cost_time_range ['r11' ] += 1
226-
227230 # 计算不同响应时间范围的请求占比
228231 cost_time_range_percentile = {'r1p' : 0 , 'r2p' : 0 , 'r3p' : 0 , 'r4p' : 0 , 'r5p' : 0 , 'r6p' : 0 ,
229232 'r7p' : 0 , 'r8p' : 0 , 'r9p' : 0 , 'r10p' : 0 , 'r11p' : 0 }
230- if cost_times_list :
231- total_cost_time_pv = float (len (cost_times_list ))
233+ if cost_time_list :
234+ total_cost_time_pv = float (len (cost_time_list ))
232235 if cost_time_range ['r1' ]:
233236 cost_time_range_percentile ['r1p' ] = '%0.3f' % float (cost_time_range ['r1' ]* 100 / total_cost_time_pv )
234237 if cost_time_range ['r2' ]:
@@ -255,8 +258,9 @@ def parse_log_file(target_file, log_format):
255258 total_data = {'pv' : pv , 'uv' : uv , 'response_avg' : response_avg , 'response_peak' : response_peak ,
256259 'response_peak_time' : response_peak_time , 'url_data_list' : url_data_list ,
257260 'source_file' : target_file , 'hours_hits' : hours_counter , 'minutes_hits' : minutes_counter ,
258- 'second_hits' : times_counter , 'cost_times_list' : cost_times_list , 'cost_times_flag' : cost_times_flag ,
259- 'cost_time_range_percentile' : cost_time_range_percentile , 'method_counts' : method_counts }
261+ 'second_hits' : times_counter , 'cost_time_list' : cost_time_list , 'cost_time_flag' : cost_time_flag ,
262+ 'cost_time_range_percentile' : cost_time_range_percentile , 'method_counts' : method_counts ,
263+ 'cost_time_percentile_flag' : cost_time_percentile_flag }
260264 generate_web_log_parser_report (total_data )
261265
262266 total_data = {'source_file' : target_file , 'urls' : urls_counter }
0 commit comments