script for finding slow logs

This commit is contained in:
Jacek Sieka 2021-03-30 09:39:16 +02:00
parent 49a5667288
commit 714620b13f
No known key found for this signature in database
GPG Key ID: A1B09461ABB656B8
1 changed files with 32 additions and 0 deletions

32
scripts/slowlogs.py Executable file
View File

@ -0,0 +1,32 @@
#!/usr/bin/env python3
# Print logs that have gaps between them larger than the threshold - useful for
# finding slowdowns in the code where the thread is busy for long periods of
# time
# usage:
# tail -F logfile | python slowlogs.py 0.75
import sys, re
from datetime import datetime
THRESHOLD = 0.75
if len(sys.argv) > 0:
THRESHOLD = float(sys.argv[1])
last = None
prevline = None
dt = re.compile(r"([0-9-]+ [0-9:.]+)")
for line in sys.stdin:
match = dt.search(line)
if match:
current = datetime.strptime(match.group(1), "%Y-%m-%d %H:%M:%S.%f")
if last != None and (current - last).total_seconds() > THRESHOLD:
print((current - last).total_seconds())
print(prevline, end="")
print(line)
last = current
prevline = line