forked from mlcommons/logging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_mllog.py
133 lines (117 loc) · 4.47 KB
/
test_mllog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Copyright 2019 MLBenchmark Group. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import collections
from contextlib import contextmanager
import io
import json
import os
import sys
import time
import unittest
from unittest import mock
from mlperf_logging import mllog
@contextmanager
def _captured_stdout():
cap_out = io.StringIO()
old_out = sys.stdout
try:
sys.stdout = cap_out
yield sys.stdout
finally:
sys.stdout = old_out
class TestMlperfLog(unittest.TestCase):
def setUp(self):
self.origin_get_caller = mllog.mllog.get_caller
mllog.mllog.get_caller = mock.MagicMock(
return_value={"file": "mybenchmark/file.py", "lineno": 42})
self.origin_do_log = mllog.mllog.MLLogger._do_log
mllog.mllog.MLLogger._do_log = mock.Mock(side_effect=self._fake_do_log)
self.origin_time = time.time
time.time = mock.MagicMock(return_value=1234567890.123)
def tearDown(self):
mllog.mllog.get_caller = self.origin_get_caller
time.time = self.origin_time
def _fake_do_log(self, level, message, clear_line=False):
if clear_line:
print("\n" + message)
else:
print(message)
def test_mllog_start_simple(self):
prefix = ":::MLLOG"
expected_log_json = json.dumps(json.loads(r'''
{
"namespace": "",
"time_ms": 1234567890123,
"event_type": "INTERVAL_START",
"key": "run_start",
"value": null,
"metadata": {"file": "mybenchmark/file.py", "lineno": 42}
}''', object_pairs_hook=collections.OrderedDict))
expected_output = " ".join([prefix, expected_log_json])
with _captured_stdout() as out:
mllogger = mllog.get_mllogger()
mllogger.start(mllog.constants.RUN_START, None)
self.assertEqual(out.getvalue().splitlines()[0], expected_output)
def test_mllog_end_simple(self):
prefix = ":::MLLOG"
expected_log_json = json.dumps(json.loads(r'''
{
"namespace": "",
"time_ms": 1234567890123,
"event_type": "INTERVAL_END",
"key": "run_stop",
"value": null,
"metadata": {"file": "mybenchmark/file.py", "lineno": 42}
}''', object_pairs_hook=collections.OrderedDict))
expected_output = " ".join([prefix, expected_log_json])
with _captured_stdout() as out:
mllogger = mllog.get_mllogger()
mllogger.end(mllog.constants.RUN_STOP, None)
self.assertEqual(out.getvalue().splitlines()[0], expected_output)
def test_mllog_event_simple(self):
prefix = ":::MLLOG"
expected_log_json = json.dumps(json.loads(r'''
{
"namespace": "",
"time_ms": 1234567890123,
"event_type": "POINT_IN_TIME",
"key": "eval_accuracy",
"value": 0.99,
"metadata": {"file": "mybenchmark/file.py", "lineno": 42}
}''', object_pairs_hook=collections.OrderedDict))
expected_output = " ".join([prefix, expected_log_json])
with _captured_stdout() as out:
mllogger = mllog.get_mllogger()
mllogger.event(mllog.constants.EVAL_ACCURACY, 0.99)
self.assertEqual(out.getvalue().splitlines()[0], expected_output)
def test_mllog_event_override_param(self):
prefix = ":::MLLOG"
expected_log_json = json.dumps(json.loads(r'''
{
"namespace": "worker1",
"time_ms": 1231231230123,
"event_type": "POINT_IN_TIME",
"key": "eval_accuracy",
"value": 0.99,
"metadata": {"file": "mybenchmark/file.py", "lineno": 42}
}''', object_pairs_hook=collections.OrderedDict))
expected_output = "\n" + " ".join([prefix, expected_log_json]) + "\n"
with _captured_stdout() as out:
mllogger = mllog.get_mllogger()
mllogger.event(mllog.constants.EVAL_ACCURACY, 0.99, namespace="worker1",
time_ms=1231231230123, clear_line=True)
self.assertEqual(out.getvalue(), expected_output)
if __name__ == "__main__":
unittest.main()