Skip to content

Commit

Permalink
makefile: print out maximum memory usage in elapsed
Browse files Browse the repository at this point in the history
Signed-off-by: Øyvind Harboe <[email protected]>
  • Loading branch information
oharboe committed Apr 16, 2024
1 parent afccf3f commit b492312
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
20 changes: 11 additions & 9 deletions flow/test/test_genElapsedTime.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'util'))
os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__))))


class TestElapsedTime(unittest.TestCase):
def setUp(self):
self.tmp_dir = tempfile.TemporaryDirectory()
Expand All @@ -24,27 +25,27 @@ def test_elapsed_time(self, mock_stdout):
# create a log file with elapsed time
with open(self.log_file, "w") as f:
f.write("Some log entry\n")
f.write("Elapsed time: 01:30:00[h:]min:sec.\n")
f.write("Elapsed time: 01:30:00[h:]min:sec. Peak memory: 9667132KB.\n")
# call the script with the test log file
sys.argv = ["./genElapsedTime.py", "--logDir", str(self.tmp_dir.name),
"--noHeader"]
with patch.object(sys, 'argv', sys.argv):
module = importlib.import_module(self.module_name)
_ = importlib.import_module(self.module_name)
# check if output is correct
expected_output = self.tmp_dir.name + "\n1_test 5400\nTotal 5400\n"
expected_output = self.tmp_dir.name + "\n1_test 5400 9667132\nTotal 5400 9667132\n"
self.assertEqual(mock_stdout.getvalue(), expected_output)

@patch("sys.stdout", new_callable=StringIO)
def test_zero_time(self, mock_stdout):
# create a log file with elapsed time
with open(self.log_file, "w") as f:
f.write("Some log entry\n")
f.write("Elapsed time: 00:00:74[h:]min:sec.\n")
f.write("Elapsed time: 00:00:74[h:]min:sec. Peak memory: 9667132KB.\n")
# call the script with the test log file
sys.argv = ["./genElapsedTime.py", "--logDir", str(self.tmp_dir.name),
"--noHeader"]
with patch.object(sys, 'argv', sys.argv):
module = importlib.import_module(self.module_name)
_ = importlib.import_module(self.module_name)
# check if output is correct
self.assertEqual(mock_stdout.getvalue(), "")

Expand All @@ -58,10 +59,10 @@ def test_elapsed_time_longer_duration(self, mock_stdout):
sys.argv = ["util/genElapsedTime.py", "--logDir", str(self.tmp_dir.name),
"--noHeader"]
with patch.object(sys, 'argv', sys.argv):
module = importlib.import_module(self.module_name)
importlib.reload(module)
_ = importlib.import_module(self.module_name)
# check if output is correct
expected_output = self.tmp_dir.name + "\n1_test 744\nTotal 744\n"
expected_output = (self.tmp_dir.name +
"\n1_test 744 9667132\nTotal 744 9667132\n")
self.assertEqual(mock_stdout.getvalue(), expected_output)

def test_missing_arg(self):
Expand All @@ -84,7 +85,8 @@ def test_no_elapsed_time(self):
self.assertIn('No elapsed time found in', fake_err_output.getvalue())

def tearDown(self):
self.tmp_dir.cleanup()
self.tmp_dir.cleanup()


if __name__ == '__main__':
unittest.main()
21 changes: 15 additions & 6 deletions flow/util/genElapsedTime.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
def print_log_dir_times(logdir):
first = True
totalElapsed = 0
total_max_memory = 0
print(logdir)

# Loop on all log files in the directory
Expand All @@ -39,8 +40,11 @@ def print_log_dir_times(logdir):
with open(str(f)) as logfile:
found = False
for line in logfile:
elapsedTime = 0
elapsedTime = None
peak_memory = None

# Example line:
# Elapsed time: 0:04.26[h:]min:sec. CPU time: user 4.08 sys 0.17 (99%). Peak memory: 671508KB.
if 'Elapsed time' in line:
found = True
# Extract the portion that has the time
Expand All @@ -61,22 +65,27 @@ def print_log_dir_times(logdir):
else:
print('Elapsed time not understood in',
str(line), file=sys.stderr)
# Find Peak Memory
peak_memory = line.split('Peak memory: ')[1].split('KB')[0]

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

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

if totalElapsed != 0:
print("%-25s %10s" % ("Total", totalElapsed))
print(format_str % ("Total", totalElapsed, total_max_memory))


for log_dir in args.logDir:
Expand Down

0 comments on commit b492312

Please sign in to comment.