2019-05-19 07:51:58 +00:00
|
|
|
#! /usr/bin/python
|
|
|
|
import sys
|
|
|
|
from tabulate import tabulate
|
|
|
|
import re
|
2019-06-16 11:16:45 +00:00
|
|
|
import os
|
|
|
|
import json
|
2019-05-19 07:51:58 +00:00
|
|
|
|
2019-05-20 07:00:57 +00:00
|
|
|
sortkey = []
|
2019-05-19 07:51:58 +00:00
|
|
|
ifile = ""
|
2019-06-16 11:16:45 +00:00
|
|
|
cache = True
|
2019-05-19 07:51:58 +00:00
|
|
|
|
|
|
|
argc = len(sys.argv)
|
|
|
|
first = 1
|
|
|
|
i = 1
|
|
|
|
while i < argc:
|
|
|
|
if sys.argv[i] == "-s" or sys.argv[i] == "--sort-by":
|
|
|
|
i += 1
|
2019-05-20 07:00:57 +00:00
|
|
|
if len(sortkey) <= 3:
|
2019-06-18 04:54:26 +00:00
|
|
|
if sys.argv[i] in ["max", "occ", "avg", "sum"]:
|
|
|
|
sortkey.append(sys.argv[i])
|
|
|
|
else:
|
|
|
|
print("ERROR: Unknown key " + sys.argv[i] + ": Aborting!")
|
|
|
|
sys.exit(1)
|
2019-05-20 07:00:57 +00:00
|
|
|
else:
|
|
|
|
print("WARNING: Too many sort criteria given! Will ignore last argument.")
|
2019-06-16 11:16:45 +00:00
|
|
|
elif sys.argv[i] == "-n" or sys.argv[i] == "--no-cache":
|
|
|
|
cache = False
|
2019-05-19 07:51:58 +00:00
|
|
|
elif sys.argv[i] == "-h" or sys.argv[i] == "--help":
|
|
|
|
print("usage: analyse-run-time.py <log-path> [OPTIONS]")
|
|
|
|
print("Showing, how long certain tasks took")
|
|
|
|
print("")
|
|
|
|
print("OPTIONS")
|
2019-05-20 07:02:02 +00:00
|
|
|
print(" -s, --sort-by Sort by key. Can be specified up to 3 times.")
|
|
|
|
print(" First key has highest priority")
|
2019-05-19 07:51:58 +00:00
|
|
|
print(" Valid keys are:")
|
|
|
|
print(" max (default) Maximum execution time")
|
|
|
|
print(" avg Average execution time")
|
|
|
|
print(" sum Total execution time")
|
|
|
|
print(" occ Occurrences in the log file")
|
2019-06-16 11:16:45 +00:00
|
|
|
print(" -n, --no-cache Doesn't read and write from/to cache file")
|
2019-05-19 07:51:58 +00:00
|
|
|
print(" -h, --help Shows this help and exit")
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
if first == 1:
|
|
|
|
first = 0
|
|
|
|
ifile = sys.argv[i]
|
|
|
|
else:
|
|
|
|
print("Unknown option " + sys.argv[i])
|
|
|
|
sys.exit(1)
|
|
|
|
i += 1
|
|
|
|
|
2019-05-20 07:00:57 +00:00
|
|
|
if len(sortkey) == 0:
|
|
|
|
sortkey.append("max")
|
|
|
|
|
2019-06-16 11:16:45 +00:00
|
|
|
from_cache = False
|
|
|
|
if os.path.isfile(ifile + ".cache") and cache:
|
|
|
|
read_src = False
|
|
|
|
from_cache = True
|
|
|
|
with open(ifile + ".cache") as json_data:
|
|
|
|
tasks = json.load(json_data)
|
|
|
|
else:
|
|
|
|
read_src = True
|
2019-05-19 07:51:58 +00:00
|
|
|
|
2019-06-16 11:16:45 +00:00
|
|
|
if read_src:
|
|
|
|
times = []
|
|
|
|
try:
|
|
|
|
with open(ifile) as f:
|
2019-06-18 04:48:28 +00:00
|
|
|
for line in f:
|
2019-06-16 11:16:45 +00:00
|
|
|
match = re.findall("SpawnPoolWorker-\d+ root INFO +Time used in seconds for (.*): (\d+\.\d+)", line)
|
|
|
|
if match != []:
|
|
|
|
times.append(match[0])
|
|
|
|
except:
|
|
|
|
print("err")
|
|
|
|
sys.exit(1)
|
2019-05-19 07:51:58 +00:00
|
|
|
|
2019-06-16 11:16:45 +00:00
|
|
|
tasks = []
|
|
|
|
for time in times:
|
|
|
|
found = False
|
|
|
|
for i in range(0, len(tasks)):
|
|
|
|
if tasks[i]['name'] == time[0]:
|
|
|
|
tasks[i]['occ'] += 1
|
|
|
|
tasks[i]['sum'] += float(time[1])
|
|
|
|
if float(time[1]) > tasks[i]['max']:
|
|
|
|
tasks[i]['max'] = float(time[1])
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
if not found:
|
|
|
|
tasks.append({})
|
|
|
|
tasks[len(tasks) -1]['name'] = time[0]
|
|
|
|
tasks[len(tasks) -1]['occ'] = 1
|
|
|
|
tasks[len(tasks) -1]['sum'] = float(time[1])
|
|
|
|
tasks[len(tasks) -1]['max'] = float(time[1])
|
|
|
|
|
|
|
|
for task in tasks:
|
|
|
|
task['avg'] = task['sum'] / task['occ']
|
|
|
|
|
|
|
|
if cache:
|
|
|
|
with open(ifile + ".cache", "w") as f:
|
|
|
|
f.write(json.dumps(tasks, default=lambda o: o.__dict__))
|
|
|
|
|
|
|
|
if from_cache:
|
|
|
|
print("Data from cache used")
|
2019-05-19 07:51:58 +00:00
|
|
|
|
2019-05-20 07:00:57 +00:00
|
|
|
i = len(sortkey) - 1
|
|
|
|
while i >= 0:
|
|
|
|
tasks.sort(key=lambda tasks: tasks[sortkey[i]], reverse=True)
|
|
|
|
i -= 1
|
2019-05-19 07:51:58 +00:00
|
|
|
print tabulate(tasks,headers="keys",floatfmt=".2f")
|