Coverage for tests / test_gatherResourceUsage.py: 35%

48 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-22 09:09 +0000

1# This file is part of analysis_tools. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21 

22from __future__ import annotations 

23 

24import os 

25from unittest import main 

26 

27import lsst.utils.tests 

28import numpy as np 

29import pandas as pd 

30from lsst.analysis.tools.tasks import ( 

31 ConsolidateResourceUsageConfig, 

32 ConsolidateResourceUsageTask, 

33 GatherResourceUsageConfig, 

34 GatherResourceUsageTask, 

35) 

36from lsst.daf.butler import Butler, DatasetType, DeferredDatasetHandle 

37from lsst.daf.butler.tests.utils import makeTestTempDir, removeTestTempDir 

38from lsst.pipe.base import TaskMetadata 

39 

40TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

41 

42test_metadata_json = """{"metadata": {"quantum": 

43 {"scalars":{"__version__":1}, 

44 "arrays": {"endMaxResidentSetSize":[1234567890], 

45 "prepCpuTime":[200.0], 

46 "initCpuTime":[201.0], 

47 "startCpuTime":[203.0], 

48 "endCpuTime":[228.0], 

49 "prepUtc":["2025-01-08T22:20:00+00:00"], 

50 "endUtc":["2025-01-08T22:20:45+00:00"]}}}} 

51 """ 

52 

53 

54class TestGatherResourceUsage(lsst.utils.tests.TestCase): 

55 """Tests for the GatherResourceUsage class and methods.""" 

56 

57 def setUp(self): 

58 self.root = makeTestTempDir(TESTDIR) 

59 Butler.makeRepo(self.root) 

60 self.butler = Butler(self.root, run="test_run") 

61 

62 def tearDown(self): 

63 removeTestTempDir(self.root) 

64 

65 def test_gatherResourceUsage(self): 

66 """Test extraction of resource usage from a TaskMetadata object.""" 

67 test_dataset_type = DatasetType( 

68 "test_dataset_type", dimensions=[], universe=self.butler.dimensions, storageClass="TaskMetadata" 

69 ) 

70 self.butler.registry.registerDatasetType(test_dataset_type) 

71 task_metadata = TaskMetadata.model_validate_json(test_metadata_json) 

72 ref = self.butler.put(task_metadata, test_dataset_type) 

73 config = GatherResourceUsageConfig(dimensions=[]) 

74 gather_resource_usage_task = GatherResourceUsageTask(config=config) 

75 input_metadata = [ 

76 DeferredDatasetHandle(butler=self.butler, ref=ref, storageClass="TaskMetadata", parameters=None) 

77 ] 

78 ru_output = gather_resource_usage_task.run( 

79 universe=self.butler.dimensions, input_metadata=input_metadata 

80 ).getDict()["output_table"] 

81 self.assertFloatsAlmostEqual(ru_output["memory"].values[0], 1.23456789e09, atol=1e-3, rtol=1e-4) 

82 self.assertFloatsAlmostEqual(ru_output["init_time"].values[0], 2.0, atol=1e-3, rtol=1e-4) 

83 self.assertFloatsAlmostEqual(ru_output["run_time"].values[0], 25.0, atol=1e-3, rtol=1e-4) 

84 self.assertFloatsAlmostEqual(ru_output["wall_time"].values[0], 45.0, atol=1e-3, rtol=1e-4) 

85 

86 def test_consolidateResourceUsage(self): 

87 """Test that the expected columns and values are generated by the 

88 ConsolidateResourceUsageTask""" 

89 nrows = 101 

90 ru_table = pd.DataFrame( 

91 { 

92 "memory": np.arange(nrows) * 1024.0**3, 

93 "init_time": np.ones(nrows), 

94 "run_time": np.arange(nrows), 

95 "wall_time": np.arange(nrows), 

96 } 

97 ) 

98 config = ConsolidateResourceUsageConfig() 

99 consolidate_resource_usage_task = ConsolidateResourceUsageTask(config=config) 

100 consolidate_ru_output = consolidate_resource_usage_task.run(testTask_resource_usage=ru_table) 

101 df = consolidate_ru_output.getDict()["output_table"] 

102 self.assertEqual(df["quanta"][0], nrows) 

103 self.assertAlmostEqual(df["integrated_runtime_hrs"][0], nrows * (nrows - 1) / 2 / 3600.0) 

104 for prefix in ("mem_GB", "runtime_s", "walltime_s"): 

105 for quantile in (1, 5, 32, 50, 68, 95, 99, 100): 

106 column = f"{prefix}_p{quantile:03d}" 

107 self.assertAlmostEqual(df[column][0], quantile) 

108 

109 

110if __name__ == "__main__": 110 ↛ 111line 110 didn't jump to line 111 because the condition on line 110 was never true

111 lsst.utils.tests.init() 

112 main()