blob: 9530213c869603b96a34c0c2f5f6735d99810840 [file] [log] [blame]
Will Bradley7e5b8c12019-07-30 12:44:15 -06001# Copyright 2019 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Tests for the api/metrics library."""
6
Mike Frysinger166fea02021-02-12 05:30:33 -05007from unittest import mock
8
Alex Klein238356d2022-04-19 09:37:02 -06009from chromite.api import metrics as api_metrics
Will Bradley7e5b8c12019-07-30 12:44:15 -060010from chromite.api.gen.chromite.api import build_api_test_pb2
11from chromite.lib import cros_test_lib
Alex Kleinaef41942022-04-19 14:13:17 -060012from chromite.lib import metrics_lib
Will Bradley7e5b8c12019-07-30 12:44:15 -060013
14
15class MetricsTest(cros_test_lib.TestCase):
16 """Test Metrics deserialization functionality at the API layer."""
17
18 def testDeserializeTimer(self):
19 """Test timer math and deserialization into proto objects."""
20 response = build_api_test_pb2.TestResultMessage()
21 mock_events = [
Alex Kleinaef41942022-04-19 14:13:17 -060022 metrics_lib.MetricEvent(
23 600, 'a.b', metrics_lib.OP_START_TIMER, arg='100'),
24 metrics_lib.MetricEvent(
25 1000, 'a.b', metrics_lib.OP_STOP_TIMER, arg='100'),
Will Bradley7e5b8c12019-07-30 12:44:15 -060026 ]
Alex Klein2a11e7f2022-04-19 09:35:35 -060027 with mock.patch(
Alex Kleinaef41942022-04-19 14:13:17 -060028 'chromite.api.metrics.metrics_lib.read_metrics_events',
Alex Klein2a11e7f2022-04-19 09:35:35 -060029 return_value=mock_events):
Alex Klein238356d2022-04-19 09:37:02 -060030 api_metrics.deserialize_metrics_log(response.events)
Will Bradley7e5b8c12019-07-30 12:44:15 -060031 self.assertEqual(len(response.events), 1)
32 self.assertEqual(response.events[0].name, 'a.b')
33 self.assertEqual(response.events[0].timestamp_milliseconds, 1000)
Alex Klein2a11e7f2022-04-19 09:35:35 -060034 self.assertEqual(response.events[0].duration_milliseconds, 1000 - 600)
Will Bradley7e5b8c12019-07-30 12:44:15 -060035
36 def testDeserializeNamedEvent(self):
37 """Test deserialization of a named event.
38
39 This test also includes a prefix to test for proper prepending.
40 """
41 response = build_api_test_pb2.TestResultMessage()
42 mock_events = [
Alex Kleinaef41942022-04-19 14:13:17 -060043 metrics_lib.MetricEvent(
44 1000, 'a.named_event', metrics_lib.OP_NAMED_EVENT, arg=None),
Will Bradley7e5b8c12019-07-30 12:44:15 -060045 ]
Alex Klein2a11e7f2022-04-19 09:35:35 -060046 with mock.patch(
Alex Kleinaef41942022-04-19 14:13:17 -060047 'chromite.api.metrics.metrics_lib.read_metrics_events',
Alex Klein2a11e7f2022-04-19 09:35:35 -060048 return_value=mock_events):
Alex Klein238356d2022-04-19 09:37:02 -060049 api_metrics.deserialize_metrics_log(response.events, prefix='prefix')
Will Bradley7e5b8c12019-07-30 12:44:15 -060050 self.assertEqual(len(response.events), 1)
51 self.assertEqual(response.events[0].name, 'prefix.a.named_event')
52 self.assertEqual(response.events[0].timestamp_milliseconds, 1000)
53 self.assertFalse(response.events[0].duration_milliseconds)
Will Bradley333d22c2019-09-11 15:04:59 -060054
55 def testDeserializeGauge(self):
56 """Test deserialization of a gauge."""
57 response = build_api_test_pb2.TestResultMessage()
58 mock_events = [
Alex Kleinaef41942022-04-19 14:13:17 -060059 metrics_lib.MetricEvent(1000, 'a.gauge', metrics_lib.OP_GAUGE, arg=17),
Will Bradley333d22c2019-09-11 15:04:59 -060060 ]
Alex Klein2a11e7f2022-04-19 09:35:35 -060061 with mock.patch(
Alex Kleinaef41942022-04-19 14:13:17 -060062 'chromite.api.metrics.metrics_lib.read_metrics_events',
Alex Klein2a11e7f2022-04-19 09:35:35 -060063 return_value=mock_events):
Alex Klein238356d2022-04-19 09:37:02 -060064 api_metrics.deserialize_metrics_log(response.events)
Will Bradley333d22c2019-09-11 15:04:59 -060065 self.assertEqual(len(response.events), 1)
66 self.assertEqual(response.events[0].name, 'a.gauge')
67 self.assertEqual(response.events[0].timestamp_milliseconds, 1000)
68 self.assertEqual(response.events[0].gauge, 17)
Alex Klein9964a1c2022-04-19 09:42:13 -060069
70 def testDeserializeCounter(self):
71 """Test deserialization of a counter."""
72 response = build_api_test_pb2.TestResultMessage()
73 mock_events = [
Alex Kleinaef41942022-04-19 14:13:17 -060074 metrics_lib.MetricEvent(
75 1000, 'a.counter', metrics_lib.OP_INCREMENT_COUNTER, arg=1),
76 metrics_lib.MetricEvent(
77 1001, 'a.counter', metrics_lib.OP_INCREMENT_COUNTER, arg=2),
78 metrics_lib.MetricEvent(
79 1002, 'a.counter', metrics_lib.OP_INCREMENT_COUNTER, arg=3),
80 metrics_lib.MetricEvent(
81 1003, 'a.counter', metrics_lib.OP_DECREMENT_COUNTER, arg=4),
Alex Klein9964a1c2022-04-19 09:42:13 -060082 ]
83 with mock.patch(
Alex Kleinaef41942022-04-19 14:13:17 -060084 'chromite.api.metrics.metrics_lib.read_metrics_events',
Alex Klein9964a1c2022-04-19 09:42:13 -060085 return_value=mock_events):
86 api_metrics.deserialize_metrics_log(response.events)
87 self.assertEqual(len(response.events), 1)
88 self.assertEqual(response.events[0].name, 'a.counter')
89 self.assertEqual(response.events[0].timestamp_milliseconds, 1003)
90 self.assertEqual(response.events[0].gauge, 2)