Skip to content

Commit fc54430

Browse files
author
miranov25
committed
Commit latest working version of perfoemance_logger.py
1 parent 257d2ea commit fc54430

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

UTILS/perfmonitor/performance_logger.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pandas as pd
66
import matplotlib.pyplot as plt
77
from typing import Union, List, Dict, Optional
8+
import sys
89

910
class PerformanceLogger:
1011
def __init__(self, log_path: str, sep: str = "|"):
@@ -32,32 +33,35 @@ def log_to_dataframe(log_paths: Union[str, List[str]], sep: str = "|") -> pd.Dat
3233

3334
rows = []
3435
for log_id, path in enumerate(log_paths):
35-
with open(path) as f:
36-
for row_id, line in enumerate(f):
37-
parts = [x.strip() for x in line.strip().split(sep)]
38-
if len(parts) < 5:
39-
continue
40-
timestamp, step, elapsed_str, rss_str, user, host = parts[:6]
41-
row = {
42-
"timestamp": timestamp,
43-
"step": step,
44-
"elapsed_sec": float(elapsed_str),
45-
"rss_gb": float(rss_str),
46-
"user": user,
47-
"host": host,
48-
"logfile": path,
49-
"rowID": row_id,
50-
"logID": log_id
51-
}
52-
53-
if "[" in step and "]" in step:
54-
base, idx = step.split("[")
55-
row["step"] = base
56-
idx = idx.rstrip("]")
57-
for i, val in enumerate(idx.split(",")):
58-
if val.strip().isdigit():
59-
row[f"index_{i}"] = int(val.strip())
60-
rows.append(row)
36+
try:
37+
with open(path) as f:
38+
for row_id, line in enumerate(f):
39+
parts = [x.strip() for x in line.strip().split(sep)]
40+
if len(parts) < 5:
41+
continue
42+
timestamp, step, elapsed_str, rss_str, user, host = parts[:6]
43+
row = {
44+
"timestamp": timestamp,
45+
"step": step,
46+
"elapsed_sec": float(elapsed_str),
47+
"rss_gb": float(rss_str),
48+
"user": user,
49+
"host": host,
50+
"logfile": path,
51+
"rowID": row_id,
52+
"logID": log_id
53+
}
54+
55+
if "[" in step and "]" in step:
56+
base, idx = step.split("[")
57+
row["step"] = base
58+
idx = idx.rstrip("]")
59+
for i, val in enumerate(idx.split(",")):
60+
if val.strip().isdigit():
61+
row[f"index_{i}"] = int(val.strip())
62+
rows.append(row)
63+
except FileNotFoundError:
64+
continue
6165

6266
return pd.DataFrame(rows)
6367

@@ -134,11 +138,11 @@ def plot(df: pd.DataFrame,
134138
plt.xlabel(xlabel)
135139
plt.ylabel(ylabel)
136140
plt.tight_layout()
137-
141+
is_testing = "pytest" in sys.modules
138142
if output_pdf:
139143
pdf.savefig()
140144
plt.close()
141-
else:
145+
elif not is_testing:
142146
plt.show()
143147

144148
if output_pdf:

0 commit comments

Comments
 (0)