Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

# 

# This file is part of ap_verify. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (http://www.lsst.org). 

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

# for details of code ownership. 

# 

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

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

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

# (at your option) any later version. 

# 

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

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

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

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

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

# 

 

"""Code for measuring software performance metrics. 

 

All measurements assume the necessary information is present in a task's metadata. 

""" 

 

__all__ = ["measureRuntime", "TimingMetricConfig", "TimingMetricTask"] 

 

import astropy.units as u 

 

import lsst.pex.config as pexConfig 

from lsst.pipe.base import Struct 

from lsst.verify import Measurement, Name, MetricComputationError 

from lsst.verify.compatibility import MetricTask 

 

 

def measureRuntime(metadata, taskName, metricName): 

"""Compute a wall-clock measurement from metadata provided 

by @`lsst.pipe.base.timeMethod`. 

 

Parameters 

---------- 

metadata : `lsst.daf.base.PropertySet` 

The metadata to search for timing information. 

taskName : `str` 

The name of the task, e.g., "processCcd". Subtask names must be the 

ones assigned by the parent task and may be disambiguated using the 

parent task name, as in "processCcd:calibrate". 

If `taskName` matches multiple runs of a subtask in different 

contexts, the information for only one run will be provided. 

metricName : `str` 

The fully qualified name of the metric being measured, e.g., 

"pipe_tasks.ProcessCcdTime" 

 

Returns 

------- 

measurement : `lsst.verify.Measurement` 

the value of `metricName`, or `None` if the timing information for 

`taskName` is not present in `metadata` 

""" 

# Some tasks have only run, others only runDataRef 

# If both are present, run takes precedence 

for methodName in ("run", "runDataRef"): 

endKey = "%s.%sEndCpuTime" % (taskName, methodName) 

 

keys = metadata.paramNames(topLevelOnly=False) 

timedMethods = [(key.replace("EndCpuTime", "StartCpuTime"), key) 

for key in keys if key.endswith(endKey)] 

if timedMethods: 

start, end = (metadata.getAsDouble(key) for key in timedMethods[0]) 

meas = Measurement(metricName, (end - start) * u.second) 

meas.notes['estimator'] = 'pipe.base.timeMethod' 

return meas 

 

return None 

 

 

class TimingMetricConfig(MetricTask.ConfigClass): 

"""Information that distinguishes one timing metric from another. 

""" 

# It would be more user-friendly to identify the top-level task and call 

# CmdLineTask._getMetadataName, but doing so bypasses the public API and 

# requires reconstruction of the full task just in case the dataset is 

# config-dependent. 

metadataDataset = pexConfig.Field(dtype=str, 

doc="The dataset type of the timed top-level task's metadata, " 

"such as 'processCcd_metadata'.") 

target = pexConfig.Field(dtype=str, 

doc="The method to time, optionally prefixed by one or more tasks " 

"in the format of `lsst.pipe.base.Task.getFullMetadata()`. " 

"The times of all matching methods/tasks are added together.") 

metric = pexConfig.Field(dtype=str, 

doc="The fully qualified name of the metric to store the timing information.") 

 

 

class TimingMetricTask(MetricTask): 

"""A Task that measures a timing metric using metadata produced by the 

`lsst.pipe.base.timeMethod` decorator. 

 

Parameters 

---------- 

args 

kwargs 

Constructor parameters are the same as for 

`lsst.verify.compatibility.MetricTask`. 

""" 

 

ConfigClass = TimingMetricConfig 

_DefaultName = "timingMetric" 

 

@classmethod 

def _getInputMetadataKeyRoot(cls, config): 

"""Get a search string for the metadata. 

 

The string contains the name of the target method, optionally 

prefixed by one or more tasks in the format of 

`lsst.pipe.base.Task.getFullMetadata()`. 

 

Parameters 

---------- 

config : ``cls.ConfigClass`` 

Configuration for this task. 

 

Returns 

------- 

keyRoot : `str` 

A string identifying the class(es) and method(s) for this task. 

""" 

return config.target 

 

@staticmethod 

def _searchMetadataKeys(metadata, keyFragment): 

"""Search the metadata for all keys matching a substring. 

 

Parameters 

---------- 

metadata : `lsst.daf.base.PropertySet` 

A metadata object with task-qualified keys as returned by 

`lsst.pipe.base.Task.getFullMetadata()`. 

keyFragment : `str` 

A substring for a full metadata key. 

 

Returns 

------- 

keys : `set` of `str` 

All keys in ``metadata`` that have ``keyFragment`` as a substring. 

""" 

keys = metadata.paramNames(topLevelOnly=False) 

return {key for key in keys if keyFragment in key} 

 

def run(self, metadata): 

"""Compute a wall-clock measurement from metadata provided by 

`lsst.pipe.base.timeMethod`. 

 

Parameters 

---------- 

metadata : iterable of `lsst.daf.base.PropertySet` 

A collection of metadata objects, one for each unit of science 

processing to be incorporated into this metric. Its elements 

may be `None` to represent missing data. 

 

Returns 

------- 

result : `lsst.pipe.base.Struct` 

A `~lsst.pipe.base.Struct` containing the following component: 

 

- ``measurement``: the total running time of the target method 

across all elements of ``metadata`` (`lsst.verify.Measurement` 

or `None`) 

 

Raises 

------ 

MetricComputationError 

Raised if any of the timing metadata are invalid. 

 

Notes 

----- 

This method does not return a measurement if any element of 

``metadata`` is ``None``. The reason for this policy is that if a 

science processing run was aborted without writing metadata, then any 

timing measurement cannot be compared to other results anyway. This 

method also does not return a measurement if no timing information was 

provided by any of the metadata. 

""" 

keyBase = self._getInputMetadataKeyRoot(self.config) 

endBase = keyBase + "EndCpuTime" 

 

timingFound = False # some timings are indistinguishable from 0, so don't test totalTime > 0 

totalTime = 0.0 

for singleMetadata in metadata: 

if singleMetadata is not None: 

matchingKeys = TimingMetricTask._searchMetadataKeys(singleMetadata, endBase) 

for endKey in matchingKeys: 

startKey = endKey.replace("EndCpuTime", "StartCpuTime") 

try: 

start, end = (singleMetadata.getAsDouble(key) for key in (startKey, endKey)) 

except (LookupError, TypeError) as e: 

raise MetricComputationError("Invalid metadata") from e 

totalTime += end - start 

timingFound = True 

else: 

self.log.warn("At least one task run did not write metadata; aborting.") 

return Struct(measurement=None) 

 

if timingFound: 

meas = Measurement(self.getOutputMetricName(self.config), totalTime * u.second) 

meas.notes['estimator'] = 'pipe.base.timeMethod' 

else: 

self.log.info("Nothing to do: no timing information for %s found.", keyBase) 

meas = None 

return Struct(measurement=meas) 

 

@classmethod 

def getInputDatasetTypes(cls, config): 

"""Return input dataset types for this task. 

 

Parameters 

---------- 

config : ``cls.ConfigClass`` 

Configuration for this task. 

 

Returns 

------- 

metadata : `dict` from `str` to `str` 

Dictionary from ``"metadata"`` to the dataset type of the target task's metadata. 

""" 

return {'metadata': config.metadataDataset} 

 

@classmethod 

def getOutputMetricName(cls, config): 

return Name(config.metric)