Skip to content

Commit ec7740d

Browse files
authored
Merge pull request #1014 from habibayassin/genElapsed-issue
util: genElapsedTime module and test adjustments
2 parents 1b094f1 + 688fa92 commit ec7740d

File tree

3 files changed

+75
-37
lines changed

3 files changed

+75
-37
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Test Util Scripts
2+
on:
3+
schedule:
4+
- cron: "0 8 * * SUN"
5+
push:
6+
paths:
7+
- 'flow/util/genElapsedTime.py'
8+
- 'flow/test/test_genElapsedTime.py'
9+
pull_request:
10+
paths:
11+
- 'flow/util/genElapsedTime.py'
12+
- 'flow/test/test_genElapsedTime.py'
13+
# Allows you to run this workflow manually from the Actions tab
14+
workflow_dispatch:
15+
16+
jobs:
17+
testUtilScripts:
18+
strategy:
19+
fail-fast: false
20+
runs-on: ubuntu-latest
21+
steps:
22+
- name: Check out repository code
23+
uses: actions/checkout@v3
24+
with:
25+
fetch-depth: 1
26+
submodules: recursive
27+
- name: Install Dependencies
28+
run: |
29+
sudo apt update
30+
sudo apt install -y python3-pip
31+
- name: Run Tests
32+
run: |
33+
cd flow/test
34+
for file in *.py; do
35+
python "$file"
36+
done
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@
44
from unittest.mock import patch
55
from unittest.mock import MagicMock
66
from io import StringIO
7-
import io
87
import sys
98
import os
109
import tempfile
11-
from datetime import datetime
12-
import argparse # argument parsing
13-
from pathlib import Path
10+
import importlib # module reloading
1411

1512
# make sure the working dir is flow/
16-
os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)) , '..'))
13+
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'util'))
14+
os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__))))
1715

1816
class TestElapsedTime(unittest.TestCase):
19-
2017
def setUp(self):
2118
self.tmp_dir = tempfile.TemporaryDirectory()
2219
self.log_file = os.path.join(self.tmp_dir.name, "1_test.log")
20+
self.module_name = "genElapsedTime"
2321

2422
@patch("sys.stdout", new_callable=StringIO)
2523
def test_elapsed_time(self, mock_stdout):
@@ -28,23 +26,24 @@ def test_elapsed_time(self, mock_stdout):
2826
f.write("Some log entry\n")
2927
f.write("Elapsed time: 01:30:00[h:]min:sec.\n")
3028
# call the script with the test log file
31-
sys.argv = ["util/genElapsedTime.py", "--logDir", str(self.tmp_dir.name)]
29+
sys.argv = ["./genElapsedTime.py", "--logDir", str(self.tmp_dir.name)]
3230
with patch.object(sys, 'argv', sys.argv):
33-
import genElapsedTime
31+
module = importlib.import_module(self.module_name)
3432
# check if output is correct
3533
expected_output = "1_test 5400\n"
3634
self.assertEqual(mock_stdout.getvalue(), expected_output)
3735

3836
@patch("sys.stdout", new_callable=StringIO)
39-
def test_longer_elapsed_time(self, mock_stdout):
37+
def test_elapsed_time_longer_duration(self, mock_stdout):
4038
# create a log file with elapsed time
4139
with open(self.log_file, "w") as f:
4240
f.write("Some log entry\n")
4341
f.write("Elapsed time: 12:24.14[h:]min:sec. CPU time: user 5081.82 sys 170.18 (705%). Peak memory: 9667132KB.\n")
4442
# call the script with the test log file
4543
sys.argv = ["util/genElapsedTime.py", "--logDir", str(self.tmp_dir.name)]
4644
with patch.object(sys, 'argv', sys.argv):
47-
import genElapsedTime
45+
module = importlib.import_module(self.module_name)
46+
importlib.reload(module)
4847
# check if output is correct
4948
expected_output = "1_test 44654\n"
5049
self.assertEqual(mock_stdout.getvalue(), expected_output)
@@ -53,20 +52,21 @@ def test_missing_arg(self):
5352
with self.assertRaises(SystemExit):
5453
with patch('sys.stderr', new=StringIO()) as fake_err_output:
5554
with patch('sys.argv', ["util/genElapsedTime.py"]):
56-
import genElapsedTime
55+
module = importlib.import_module(self.module_name)
56+
importlib.reload(module)
5757
self.assertIn('--logDir', fake_err_output.getvalue())
5858

59-
6059
def test_no_elapsed_time(self):
6160
with open(self.log_file, "w") as f:
6261
f.write('Other log message')
6362
with patch('sys.argv', ["util/genElapsedTime.py", "--logDir", str(self.tmp_dir.name)]):
64-
with patch('sys.stderr', new=StringIO()) as fake_err_output:
65-
import genElapsedTime
66-
self.assertIn('No elapsed time found in', fake_err_output.getvalue())
63+
with patch('sys.stderr', new=StringIO()) as fake_err_output:
64+
module = importlib.import_module(self.module_name)
65+
importlib.reload(module)
66+
self.assertIn('No elapsed time found in', fake_err_output.getvalue())
6767

6868
def tearDown(self):
6969
self.tmp_dir.cleanup()
7070

7171
if __name__ == '__main__':
72-
unittest.main()
72+
unittest.main()

flow/util/genElapsedTime.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,30 @@
2626
# Loop on all log files in the directory
2727
for f in sorted(pathlib.Path(args.logDir).glob('**/[0-9]_*.log')):
2828
# Extract Elapsed Time line from log file
29-
for line in open(str(f)):
30-
elapsedTime = 0
31-
32-
if 'Elapsed time' in line:
33-
# Extract the portion that has the time
34-
timePor = line.strip().replace('Elapsed time: ', '')
35-
# Remove the units from the time portion
36-
timePor = timePor.split('[h:]', 1)[0]
37-
# Ensure that hours, min and seconds are separated by ':' not '.'
38-
timePor = timePor.replace('.',':')
39-
# Calculate elapsed time that has this format 'h:m:s'
40-
timeList = timePor.split(':')
41-
if len(timeList) == 2:
42-
# Only minutes and seconds are present
43-
elapsedTime = int(timeList[0])*60 + int(timeList[1])
44-
elif len(timeList) == 3:
45-
# Hours, minutes, and seconds are present
46-
elapsedTime = int(timeList[0])*3600 + int(timeList[1])*60 + int(timeList[2])
47-
if elapsedTime == 0:
48-
print('No elapsed time found in', str(f), file=sys.stderr)
29+
with open(str(f)) as logfile:
30+
for line in logfile:
31+
elapsedTime = 0
32+
33+
if 'Elapsed time' in line:
34+
# Extract the portion that has the time
35+
timePor = line.strip().replace('Elapsed time: ', '')
36+
# Remove the units from the time portion
37+
timePor = timePor.split('[h:]', 1)[0]
38+
# Ensure that hours, min and seconds are separated by ':' not '.'
39+
timePor = timePor.replace('.',':')
40+
# Calculate elapsed time that has this format 'h:m:s'
41+
timeList = timePor.split(':')
42+
if len(timeList) == 2:
43+
# Only minutes and seconds are present
44+
elapsedTime = int(timeList[0])*60 + int(timeList[1])
45+
elif len(timeList) == 3:
46+
# Hours, minutes, and seconds are present
47+
elapsedTime = int(timeList[0])*3600 + int(timeList[1])*60 + int(timeList[2])
48+
if elapsedTime == 0:
49+
print('No elapsed time found in', str(f), file=sys.stderr)
4950

5051
# Print the name of the step and the corresponding elapsed time
51-
print('%-25s %10s' % (os.path.splitext(os.path.basename(str(f)))[0], elapsedTime))
52+
if elapsedTime != 0:
53+
print('%-25s %10s' % (os.path.splitext(os.path.basename(str(f)))[0], elapsedTime))
5254

5355

0 commit comments

Comments
 (0)