Skip to content

Commit 4eb811a

Browse files
committed
makefile: print out maximum memory usage in elapsed
Signed-off-by: Øyvind Harboe <[email protected]>
1 parent afccf3f commit 4eb811a

File tree

2 files changed

+63
-57
lines changed

2 files changed

+63
-57
lines changed

flow/test/test_genElapsedTime.py

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import sys
88
import os
99
import tempfile
10-
import importlib # module reloading
1110

1211
# make sure the working dir is flow/
13-
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'util'))
12+
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)),
13+
'..', 'util'))
1414
os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__))))
1515

16+
import genElapsedTime
17+
1618
class TestElapsedTime(unittest.TestCase):
1719
def setUp(self):
1820
self.tmp_dir = tempfile.TemporaryDirectory()
@@ -24,29 +26,25 @@ def test_elapsed_time(self, mock_stdout):
2426
# create a log file with elapsed time
2527
with open(self.log_file, "w") as f:
2628
f.write("Some log entry\n")
27-
f.write("Elapsed time: 01:30:00[h:]min:sec.\n")
29+
f.write("Elapsed time: 01:30:00[h:]min:sec. Peak memory: 9667132KB.\n")
2830
# call the script with the test log file
29-
sys.argv = ["./genElapsedTime.py", "--logDir", str(self.tmp_dir.name),
30-
"--noHeader"]
31-
with patch.object(sys, 'argv', sys.argv):
32-
module = importlib.import_module(self.module_name)
31+
genElapsedTime.scan_logs(["--logDir", str(self.tmp_dir.name),
32+
"--noHeader"])
3333
# check if output is correct
34-
expected_output = self.tmp_dir.name + "\n1_test 5400\nTotal 5400\n"
34+
expected_output = self.tmp_dir.name + "\n1_test 5400 9667132\nTotal 5400 9667132\n"
3535
self.assertEqual(mock_stdout.getvalue(), expected_output)
3636

3737
@patch("sys.stdout", new_callable=StringIO)
3838
def test_zero_time(self, mock_stdout):
3939
# create a log file with elapsed time
4040
with open(self.log_file, "w") as f:
4141
f.write("Some log entry\n")
42-
f.write("Elapsed time: 00:00:74[h:]min:sec.\n")
42+
f.write("Elapsed time: 00:00:74[h:]min:sec. Peak memory: 9667132KB.\n")
4343
# call the script with the test log file
44-
sys.argv = ["./genElapsedTime.py", "--logDir", str(self.tmp_dir.name),
45-
"--noHeader"]
46-
with patch.object(sys, 'argv', sys.argv):
47-
module = importlib.import_module(self.module_name)
48-
# check if output is correct
49-
self.assertEqual(mock_stdout.getvalue(), "")
44+
genElapsedTime.scan_logs(["--logDir", str(self.tmp_dir.name),
45+
"--noHeader"])
46+
expected_output = self.tmp_dir.name + "\n1_test 74 9667132\nTotal 74 9667132\n"
47+
self.assertEqual(mock_stdout.getvalue(), expected_output)
5048

5149
@patch("sys.stdout", new_callable=StringIO)
5250
def test_elapsed_time_longer_duration(self, mock_stdout):
@@ -55,36 +53,30 @@ def test_elapsed_time_longer_duration(self, mock_stdout):
5553
f.write("Some log entry\n")
5654
f.write("Elapsed time: 12:24.14[h:]min:sec. CPU time: user 5081.82 sys 170.18 (705%). Peak memory: 9667132KB.\n")
5755
# call the script with the test log file
58-
sys.argv = ["util/genElapsedTime.py", "--logDir", str(self.tmp_dir.name),
59-
"--noHeader"]
60-
with patch.object(sys, 'argv', sys.argv):
61-
module = importlib.import_module(self.module_name)
62-
importlib.reload(module)
56+
genElapsedTime.scan_logs(["--logDir", str(self.tmp_dir.name),
57+
"--noHeader"])
6358
# check if output is correct
64-
expected_output = self.tmp_dir.name + "\n1_test 744\nTotal 744\n"
59+
expected_output = (self.tmp_dir.name +
60+
"\n1_test 744 9667132\nTotal 744 9667132\n")
6561
self.assertEqual(mock_stdout.getvalue(), expected_output)
6662

6763
def test_missing_arg(self):
6864
with self.assertRaises(SystemExit):
6965
with patch('sys.stderr', new=StringIO()) as fake_err_output:
70-
with patch('sys.argv', ["util/genElapsedTime.py"]):
71-
module = importlib.import_module(self.module_name)
72-
importlib.reload(module)
66+
genElapsedTime.scan_logs(["util/genElapsedTime.py"])
7367
self.assertIn('--logDir', fake_err_output.getvalue())
7468

75-
def test_no_elapsed_time(self):
69+
@patch("sys.stderr", new_callable=StringIO)
70+
def test_no_elapsed_time(self, fake_err_output):
7671
with open(self.log_file, "w") as f:
7772
f.write('Other log message')
78-
with patch('sys.argv', ["util/genElapsedTime.py",
79-
"--logDir", str(self.tmp_dir.name),
80-
"--noHeader"]):
81-
with patch('sys.stderr', new=StringIO()) as fake_err_output:
82-
module = importlib.import_module(self.module_name)
83-
importlib.reload(module)
84-
self.assertIn('No elapsed time found in', fake_err_output.getvalue())
73+
genElapsedTime.scan_logs(["--logDir", str(self.tmp_dir.name),
74+
"--noHeader"])
75+
self.assertIn('No elapsed time found in', fake_err_output.getvalue())
8576

8677
def tearDown(self):
87-
self.tmp_dir.cleanup()
78+
self.tmp_dir.cleanup()
79+
8880

8981
if __name__ == '__main__':
9082
unittest.main()

flow/util/genElapsedTime.py

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,12 @@
1111

1212
# Parse and validate arguments
1313
# ==============================================================================
14-
parser = argparse.ArgumentParser(
15-
description='Print elapsed time for every step in the flow')
16-
parser.add_argument('--logDir', '-d', required=True, nargs='+',
17-
help='Log files directories')
18-
parser.add_argument('--noHeader', action='store_true',
19-
help='Skip the header')
20-
args = parser.parse_args()
21-
22-
if not args.logDir:
23-
print('[ERROR] Please add a logDir'
24-
'-d/--logDir.', file=sys.stderr)
25-
parser.print_help()
26-
sys.exit(1)
27-
28-
29-
def print_log_dir_times(logdir):
14+
15+
16+
def print_log_dir_times(logdir, args):
3017
first = True
3118
totalElapsed = 0
19+
total_max_memory = 0
3220
print(logdir)
3321

3422
# Loop on all log files in the directory
@@ -39,8 +27,11 @@ def print_log_dir_times(logdir):
3927
with open(str(f)) as logfile:
4028
found = False
4129
for line in logfile:
42-
elapsedTime = 0
30+
elapsedTime = None
31+
peak_memory = None
4332

33+
# Example line:
34+
# Elapsed time: 0:04.26[h:]min:sec. CPU time: user 4.08 sys 0.17 (99%). Peak memory: 671508KB.
4435
if 'Elapsed time' in line:
4536
found = True
4637
# Extract the portion that has the time
@@ -61,23 +52,46 @@ def print_log_dir_times(logdir):
6152
else:
6253
print('Elapsed time not understood in',
6354
str(line), file=sys.stderr)
55+
# Find Peak Memory
56+
peak_memory = line.split('Peak memory: ')[1].split('KB')[0]
6457

6558
if not found:
6659
print('No elapsed time found in', str(f), file=sys.stderr)
6760
continue
6861

6962
# Print the name of the step and the corresponding elapsed time
70-
if elapsedTime != 0:
63+
format_str = "%-25s %20s %14s"
64+
if elapsedTime is not None and peak_memory is not None:
7165
if first and not args.noHeader:
72-
print("%-25s %10s" % ("Log", "Elapsed seconds"))
66+
print(format_str %
67+
("Log", "Elapsed seconds", "Peak Memory/KB"))
7368
first = False
74-
print('%-25s %10s' % (os.path.splitext(
75-
os.path.basename(str(f)))[0], elapsedTime))
69+
print(format_str % (os.path.splitext(
70+
os.path.basename(str(f)))[0], elapsedTime, peak_memory))
7671
totalElapsed += elapsedTime
72+
total_max_memory = max(total_max_memory, int(peak_memory))
7773

7874
if totalElapsed != 0:
79-
print("%-25s %10s" % ("Total", totalElapsed))
75+
print(format_str % ("Total", totalElapsed, total_max_memory))
76+
77+
78+
def scan_logs(args):
79+
parser = argparse.ArgumentParser(description='Print elapsed time for every step in the flow')
80+
parser.add_argument('--logDir', '-d', required=True, nargs='+',
81+
help='Log files directories')
82+
parser.add_argument('--noHeader', action='store_true',
83+
help='Skip the header')
84+
args = parser.parse_args(args)
85+
86+
if not args.logDir:
87+
print('[ERROR] Please add a logDir'
88+
'-d/--logDir.', file=sys.stderr)
89+
parser.print_help()
90+
sys.exit(1)
91+
92+
for log_dir in args.logDir:
93+
print_log_dir_times(log_dir, args)
8094

8195

82-
for log_dir in args.logDir:
83-
print_log_dir_times(log_dir)
96+
if __name__ == '__main__':
97+
scan_logs(sys.argv[1:])

0 commit comments

Comments
 (0)