1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (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 * http://www.apache.org/licenses/LICENSE-2.0
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 */
16
17 #ifndef ART_LIBARTBASE_BASE_METRICS_METRICS_TEST_H_
18 #define ART_LIBARTBASE_BASE_METRICS_METRICS_TEST_H_
19
20 #include "metrics.h"
21
22 #pragma clang diagnostic push
23 #pragma clang diagnostic error "-Wconversion"
24
25 namespace art {
26 namespace metrics {
27 namespace test {
28
29 // This namespace contains functions that are helpful for testing metrics but should not be used in
30 // production code.
31
32 // A trivial MetricsBackend that does nothing for all of the members. This can be overridden by
33 // test cases to test specific behaviors.
34 class TestBackendBase : public MetricsBackend {
35 public:
BeginOrUpdateSession(const SessionData & session_data)36 void BeginOrUpdateSession([[maybe_unused]] const SessionData& session_data) override {}
37
BeginReport(uint64_t timestamp_since_start_ms)38 void BeginReport([[maybe_unused]] uint64_t timestamp_since_start_ms) override {}
39
ReportCounter(DatumId counter_type,uint64_t value)40 void ReportCounter([[maybe_unused]] DatumId counter_type,
41 [[maybe_unused]] uint64_t value) override {}
42
ReportHistogram(DatumId histogram_type,int64_t low_value_,int64_t high_value,const std::vector<uint32_t> & buckets)43 void ReportHistogram([[maybe_unused]] DatumId histogram_type,
44 [[maybe_unused]] int64_t low_value_,
45 [[maybe_unused]] int64_t high_value,
46 [[maybe_unused]] const std::vector<uint32_t>& buckets) override {}
47
EndReport()48 void EndReport() override {}
49 };
50
51 template <typename MetricType>
CounterValue(const MetricType & counter)52 uint64_t CounterValue(const MetricType& counter) {
53 uint64_t counter_value{0};
54 struct CounterBackend : public TestBackendBase {
55 explicit CounterBackend(uint64_t* counter_value) : counter_value_{counter_value} {}
56
57 void ReportCounter(DatumId, uint64_t value) override { *counter_value_ = value; }
58
59 uint64_t* counter_value_;
60 } backend{&counter_value};
61 counter.Report({&backend});
62 return counter_value;
63 }
64
65 template <DatumId histogram_type, size_t num_buckets, int64_t low_value, int64_t high_value>
GetBuckets(const MetricsHistogram<histogram_type,num_buckets,low_value,high_value> & histogram)66 std::vector<uint32_t> GetBuckets(
67 const MetricsHistogram<histogram_type, num_buckets, low_value, high_value>& histogram) {
68 std::vector<uint32_t> buckets;
69 struct HistogramBackend : public TestBackendBase {
70 explicit HistogramBackend(std::vector<uint32_t>* buckets) : buckets_{buckets} {}
71
72 void ReportHistogram(DatumId, int64_t, int64_t, const std::vector<uint32_t>& buckets) override {
73 *buckets_ = buckets;
74 }
75
76 std::vector<uint32_t>* buckets_;
77 } backend{&buckets};
78 histogram.Report({&backend});
79 return buckets;
80 }
81
82 } // namespace test
83 } // namespace metrics
84 } // namespace art
85
86 #pragma clang diagnostic pop // -Wconversion
87
88 #endif // ART_LIBARTBASE_BASE_METRICS_METRICS_TEST_H_
89