11# SPDX-FileCopyrightText: 2021 Sebastian Garcia <[email protected] > 22# SPDX-License-Identifier: GPL-2.0-only
3+ import csv
4+ import os
5+ import time
36from re import split
47from typing import Dict
58from slips_files .common .abstracts .iinput_type import IInputType
@@ -148,6 +151,32 @@ def fill_empty_class_fields(self, flow_values: dict, slips_class):
148151class ZeekJSON (IInputType , Zeek ):
149152 def __init__ (self ):
150153 self .line_processor_cache = {}
154+ self .times = {}
155+ self .init_csv ()
156+
157+ def init_csv (self ):
158+ path = os .path .join ("/tmp/" , "zeek_json_times_each_func_took.csv" )
159+ with open (path , "w" , newline = "" ) as f :
160+ writer = csv .writer (f )
161+ writer .writerow (
162+ [
163+ "get_file_type" ,
164+ "removing_empty_vals" ,
165+ "setting_conn_vals_to_0" ,
166+ "fill_empty_class_fields" ,
167+ "calling_slips_class" ,
168+ ]
169+ )
170+
171+ def log_time (self , what , time ):
172+ self .times [what ] = f"{ time :.2f} "
173+ last_metric = "calling_slips_class"
174+ if what == last_metric :
175+ path = os .path .join ("/tmp/" , "zeek_json_times_each_func_took.csv" )
176+ with open (path , "a" , newline = "" ) as f :
177+ writer = csv .DictWriter (f , fieldnames = self .times .keys ())
178+ writer .writerow (self .times )
179+ self .times = {}
151180
152181 def process_line (self , new_line : dict ):
153182 line = new_line ["data" ]
@@ -156,7 +185,11 @@ def process_line(self, new_line: dict):
156185 if not isinstance (line , dict ):
157186 return False
158187
188+ n = time .time ()
159189 file_type = self .get_file_type (new_line )
190+ latency = time .time () - n
191+ self .log_time ("get_file_type" , latency )
192+
160193 line_map = LOG_MAP .get (file_type )
161194 if not line_map :
162195 return False
@@ -168,6 +201,7 @@ def process_line(self, new_line: dict):
168201
169202 flow_values = {"starttime" : starttime , "interface" : interface }
170203
204+ n = time .time ()
171205 for zeek_field , slips_field in line_map .items ():
172206 if not slips_field :
173207 continue
@@ -176,25 +210,37 @@ def process_line(self, new_line: dict):
176210 val = ""
177211 flow_values [slips_field ] = val
178212
213+ latency = time .time () - n
214+ self .log_time ("removing_empty_vals" , latency )
215+
179216 if file_type in LINE_TYPE_TO_SLIPS_CLASS :
217+ n = time .time ()
180218 slips_class = LINE_TYPE_TO_SLIPS_CLASS [file_type ]
181-
182219 if file_type == "conn.log" :
183220 flow_values ["dur" ] = float (flow_values .get ("dur" , 0 ) or 0 )
184- for fld in (
221+ for field in (
185222 "sbytes" ,
186223 "dbytes" ,
187224 "spkts" ,
188225 "dpkts" ,
189226 "sport" ,
190227 "dport" ,
191228 ):
192- flow_values [fld ] = int (flow_values .get (fld , 0 ) or 0 )
193-
229+ flow_values [field ] = int (flow_values .get (field , 0 ) or 0 )
230+ latency = time .time () - n
231+ self .log_time ("setting_conn_vals_to_0" , latency )
232+ n = time .time ()
194233 flow_values = self .fill_empty_class_fields (
195234 flow_values , slips_class
196235 )
236+ latency = time .time () - n
237+ self .log_time ("fill_empty_class_fields" , latency )
238+
239+ n = time .time ()
197240 self .flow = slips_class (** flow_values )
241+ latency = time .time () - n
242+ self .log_time ("calling_slips_class" , latency )
243+
198244 return self .flow
199245
200246 print (f"[Profiler] Invalid file_type: { file_type } , line: { line } " )
0 commit comments