xref: /aosp_15_r20/tools/asuite/atest/metrics/metrics.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1# Copyright 2018, The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Metrics class."""
16from atest import constants
17from atest.metrics import metrics_base
18
19
20class AtestStartEvent(metrics_base.MetricsBase):
21  """Create Atest start event and send to clearcut.
22
23  Usage:
24      metrics.AtestStartEvent(
25          command_line='example_atest_command',
26          test_references=['example_test_reference'],
27          cwd='example/working/dir',
28          os='example_os')
29  """
30
31  _EVENT_NAME = 'atest_start_event'
32  command_line = constants.INTERNAL
33  test_references = constants.INTERNAL
34  cwd = constants.INTERNAL
35  os = constants.INTERNAL
36  source_root = constants.INTERNAL
37  hostname = constants.INTERNAL
38
39
40class AtestExitEvent(metrics_base.MetricsBase):
41  """Create Atest exit event and send to clearcut.
42
43  Usage:
44      metrics.AtestExitEvent(
45          duration=metrics_utils.convert_duration(end-start),
46          exit_code=0,
47          stacktrace='some_trace',
48          logs='some_logs')
49  """
50
51  _EVENT_NAME = 'atest_exit_event'
52  duration = constants.EXTERNAL
53  exit_code = constants.EXTERNAL
54  stacktrace = constants.INTERNAL
55  logs = constants.INTERNAL
56
57
58class FindTestFinishEvent(metrics_base.MetricsBase):
59  """Create find test finish event and send to clearcut.
60
61  Occurs after a SINGLE test reference has been resolved to a test or
62  not found.
63
64  Usage:
65      metrics.FindTestFinishEvent(
66          duration=metrics_utils.convert_duration(end-start),
67          success=true,
68          test_reference='hello_world_test',
69          test_finders=['example_test_reference', 'ref2'],
70          test_info="test_name: hello_world_test -
71              test_runner:AtestTradefedTestRunner -
72              build_targets:
73                  set(['MODULES-IN-platform_testing-tests-example-native']) -
74              data:{'rel_config':
75                  'platform_testing/tests/example/native/AndroidTest.xml',
76                  'filter': frozenset([])} -
77              suite:None - module_class: ['NATIVE_TESTS'] -
78              install_locations:set(['device', 'host'])")
79  """
80
81  _EVENT_NAME = 'find_test_finish_event'
82  duration = constants.EXTERNAL
83  success = constants.EXTERNAL
84  test_reference = constants.INTERNAL
85  test_finders = constants.INTERNAL
86  test_info = constants.INTERNAL
87
88
89class BuildFinishEvent(metrics_base.MetricsBase):
90  """Create build finish event and send to clearcut.
91
92  Occurs after the build finishes, either successfully or not.
93
94  Usage:
95      metrics.BuildFinishEvent(
96          duration=metrics_utils.convert_duration(end-start),
97          success=true,
98          targets=['target1', 'target2'])
99  """
100
101  _EVENT_NAME = 'build_finish_event'
102  duration = constants.EXTERNAL
103  success = constants.EXTERNAL
104  targets = constants.INTERNAL
105
106
107class RunnerFinishEvent(metrics_base.MetricsBase):
108  """Create run finish event and send to clearcut.
109
110  Occurs when a single test runner has completed.
111
112  Usage:
113      metrics.RunnerFinishEvent(
114          duration=metrics_utils.convert_duration(end-start),
115          success=true,
116          runner_name='AtestTradefedTestRunner'
117          test=[{name:'hello_world_test', result:0, stacktrace:''},
118                {name:'test2', result:1, stacktrace:'xxx'}])
119  """
120
121  _EVENT_NAME = 'runner_finish_event'
122  duration = constants.EXTERNAL
123  success = constants.EXTERNAL
124  runner_name = constants.EXTERNAL
125  test = constants.INTERNAL
126
127
128class RunTestsFinishEvent(metrics_base.MetricsBase):
129  """Create run tests finish event and send to clearcut.
130
131  Occurs after all test runners and tests have finished.
132
133  Usage:
134      metrics.RunTestsFinishEvent(
135          duration=metrics_utils.convert_duration(end-start))
136  """
137
138  _EVENT_NAME = 'run_tests_finish_event'
139  duration = constants.EXTERNAL
140
141
142class LocalDetectEvent(metrics_base.MetricsBase):
143  """Create local detection event and send it to clearcut.
144
145  Usage:
146      metrics.LocalDetectEvent(
147          detect_type=0,
148          result=0)
149      detect_type: a value from atest_enum.DetectType.
150      result: the value corresponding to the result of the detected event.
151  """
152
153  _EVENT_NAME = 'local_detect_event'
154  detect_type = constants.EXTERNAL
155  result = constants.EXTERNAL
156
157
158def get_run_id() -> str:
159  """Returns the unique run id set for the current invocation."""
160  return metrics_base.MetricsBase.get_run_id()
161
162
163def is_internal_user() -> bool:
164  """Returns whether the user is a google internal user."""
165  return metrics_base.get_user_type() == metrics_base.INTERNAL_USER
166