Skip to content

Commit 65b2d01

Browse files
committed
profiler: only convert non-float/int values to unix to avoid unnecessary strptime calls (they're expensive)
1 parent 49119fd commit 65b2d01

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

slips_files/common/slips_utils.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,16 +448,15 @@ def convert_to_datetime(self, ts):
448448
else datetime.strptime(ts, given_format)
449449
)
450450

451+
def is_unix_ts(self, time) -> bool:
452+
return isinstance(time, (int, float))
453+
451454
def get_time_format(self, time) -> Optional[str]:
452455
if self.is_datetime_obj(time):
453456
return "datetimeobj"
454457

455-
try:
456-
# Try unix timestamp in seconds.
457-
datetime.fromtimestamp(float(time))
458+
if self.is_unix_ts(time):
458459
return "unixtimestamp"
459-
except ValueError:
460-
pass
461460

462461
for time_format in self.time_formats:
463462
try:

slips_files/core/profiler_worker.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,15 @@ def get_private_client_ips(
135135
private_clients.append(ip)
136136
return private_clients
137137

138-
def convert_starttime_to_epoch(self, starttime) -> str:
138+
def convert_starttime_to_unix_ts(self, starttime) -> str:
139+
if utils.is_unix_ts(starttime):
140+
return starttime
141+
139142
try:
140143
return utils.convert_ts_format(starttime, "unixtimestamp")
141144
except ValueError:
142145
self.print(
143-
f"We can not recognize time format of "
146+
f"Slips can not recognize time format of "
144147
f"flow.starttime: {starttime}",
145148
0,
146149
1,
@@ -537,7 +540,7 @@ def add_flow_to_profile(self, flow):
537540
self.print(f"Storing data in the profile: {profileid}", 3, 0)
538541

539542
n = time.time()
540-
flow.starttime = self.convert_starttime_to_epoch(flow.starttime)
543+
flow.starttime = self.convert_starttime_to_unix_ts(flow.starttime)
541544
time_it_Took = time.time() - n
542545

543546
self.log_time("convert_starttime_to_epoch", time_it_Took)

tests/test_profiler_worker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def test_convert_starttime_to_epoch():
199199
) as mock_convert_ts_format:
200200
mock_convert_ts_format.return_value = 1680604800
201201

202-
converted = profiler.convert_starttime_to_epoch(starttime)
202+
converted = profiler.convert_starttime_to_unix_ts(starttime)
203203

204204
mock_convert_ts_format.assert_called_once_with(
205205
"2023-04-04 12:00:00", "unixtimestamp"
@@ -214,7 +214,7 @@ def test_convert_starttime_to_epoch_invalid_format(monkeypatch):
214214
"slips_files.core.profiler.utils.convert_ts_format",
215215
Mock(side_effect=ValueError),
216216
)
217-
converted = profiler.convert_starttime_to_epoch(starttime)
217+
converted = profiler.convert_starttime_to_unix_ts(starttime)
218218
assert converted == "not a real time"
219219

220220

0 commit comments

Comments
 (0)