已合并
test: add put_metric coverage #42809
lgxxx创建于 25 天前
test: add put_metric coverage #42809
已合并
共 1 个文件变更+53-2
| @@ -1,3 +1,18 @@ | |||
| 1 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd | ||
| 2 | +# All rights reserved. | ||
| 3 | +# | ||
| 4 | +# Licensed under the BSD 3-Clause License (the "License"); | ||
| 5 | +# you may not use this file except in compliance with the License. | ||
| 6 | +# You may obtain a copy of the License at | ||
| 7 | +# | ||
| 8 | +# https://opensource.org/licenses/BSD-3-Clause | ||
| 9 | +# | ||
| 10 | +# Unless required by applicable law or agreed to in writing, software | ||
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | +# See the License for the specific language governing permissions and | ||
| 14 | +# limitations under the License. | ||
| 15 | + | ||
| 1 | """ | 16 | """ |
| 2 | Add validation cases for torch.distributed.elastic.metrics APIs: | 17 | Add validation cases for torch.distributed.elastic.metrics APIs: |
| 3 | 18 | ||
| @@ -6,6 +21,7 @@ Add validation cases for torch.distributed.elastic.metrics APIs: | |||
| 6 | torch.distributed.elastic.metrics.api.ConsoleMetricHandler | 21 | torch.distributed.elastic.metrics.api.ConsoleMetricHandler |
| 7 | torch.distributed.elastic.metrics.api.NullMetricHandler | 22 | torch.distributed.elastic.metrics.api.NullMetricHandler |
| 8 | torch.distributed.elastic.metrics.configure | 23 | torch.distributed.elastic.metrics.configure |
| 24 | +torch.distributed.elastic.metrics.put_metric | ||
| 9 | (extendable) | 25 | (extendable) |
| 10 | """ | 26 | """ |
| 11 | 27 | ||
| @@ -14,7 +30,7 @@ import unittest.mock as mock | |||
| 14 | import torch.distributed.elastic.metrics.api as metrics_api | 30 | import torch.distributed.elastic.metrics.api as metrics_api |
| 15 | from torch.testing._internal.common_utils import TestCase, run_tests | 31 | from torch.testing._internal.common_utils import TestCase, run_tests |
| 16 | 32 | ||
| 17 | -from torch.distributed.elastic.metrics import configure | 33 | +from torch.distributed.elastic.metrics import configure, put_metric |
| 18 | from torch.distributed.elastic.metrics.api import ( | 34 | from torch.distributed.elastic.metrics.api import ( |
| 19 | ConsoleMetricHandler, | 35 | ConsoleMetricHandler, |
| 20 | MetricData, | 36 | MetricData, |
| @@ -37,6 +53,41 @@ class ElasticMetricsApiTest(TestCase): | |||
| 37 | metrics_api._metrics_map.clear() | 53 | metrics_api._metrics_map.clear() |
| 38 | metrics_api._metrics_map.update(self._original_metrics_map) | 54 | metrics_api._metrics_map.update(self._original_metrics_map) |
| 39 | 55 | ||
| 56 | + def test_put_metric(self): | ||
| 57 | + default_handler = mock.Mock(spec=MetricHandler) | ||
| 58 | + custom_handler = mock.Mock(spec=MetricHandler) | ||
| 59 | + configure(default_handler) | ||
| 60 | + configure(custom_handler, group="custom_group") | ||
| 61 | + | ||
| 62 | + put_metric("default_metric", 3) | ||
| 63 | + put_metric("", -1, "custom_group") | ||
| 64 | + put_metric("zero_metric", 0, "custom_group") | ||
| 65 | + | ||
| 66 | + self.assertEqual(default_handler.emit.call_count, 1) | ||
| 67 | + self.assertEqual(custom_handler.emit.call_count, 2) | ||
| 68 | + | ||
| 69 | + default_record = default_handler.emit.call_args.args[0] | ||
| 70 | + self.assertIsInstance(default_record, MetricData) | ||
| 71 | + self.assertEqual(default_record.group_name, "torchelastic") | ||
| 72 | + self.assertEqual(default_record.name, "default_metric") | ||
| 73 | + self.assertEqual(default_record.value, 3) | ||
| 74 | + self.assertGreater(default_record.timestamp, 0) | ||
| 75 | + | ||
| 76 | + custom_records = [ | ||
| 77 | + call.args[0] for call in custom_handler.emit.call_args_list | ||
| 78 | + ] | ||
| 79 | + self.assertEqual( | ||
| 80 | + [ | ||
| 81 | + (record.group_name, record.name, record.value) | ||
| 82 | + for record in custom_records | ||
| 83 | + ], | ||
| 84 | + [ | ||
| 85 | + ("custom_group", "", -1), | ||
| 86 | + ("custom_group", "zero_metric", 0), | ||
| 87 | + ], | ||
| 88 | + ) | ||
| 89 | + self.assertTrue(all(record.timestamp > 0 for record in custom_records)) | ||
| 90 | + | ||
| 40 | def test_console_metric_handler_emit(self): | 91 | def test_console_metric_handler_emit(self): |
| 41 | handler = ConsoleMetricHandler() | 92 | handler = ConsoleMetricHandler() |
| 42 | metric_data = MetricData( | 93 | metric_data = MetricData( |
| @@ -146,4 +197,4 @@ class ElasticMetricsApiTest(TestCase): | |||
| 146 | 197 | ||
| 147 | 198 | ||
| 148 | if __name__ == "__main__": | 199 | if __name__ == "__main__": |
| 149 | - run_tests() | 200 | + run_tests() |