blob: 07b4e9dfcd4c8e294bc5497b9df84603d7e3b5b2 [file] [log] [blame]
Paul Duffin7d020882021-02-08 10:16:27 +00001/*
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
25namespace art {
26namespace metrics {
27namespace 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.
34class TestBackendBase : public MetricsBackend {
35 public:
Martin Stjernholm25f410c2021-07-05 01:07:38 +010036 void BeginOrUpdateSession([[maybe_unused]] const SessionData& session_data) override {}
Martin Stjernholmc82f8f02021-02-10 13:23:48 +000037
38 void BeginReport([[maybe_unused]] uint64_t timestamp_since_start_ms) override {}
Paul Duffin7d020882021-02-08 10:16:27 +000039
40 void ReportCounter([[maybe_unused]] DatumId counter_type,
41 [[maybe_unused]] uint64_t value) override {}
42
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 {}
Martin Stjernholmc82f8f02021-02-10 13:23:48 +000047
48 void EndReport() override {}
Paul Duffin7d020882021-02-08 10:16:27 +000049};
50
Martin Stjernholm4fb51112021-04-30 11:53:52 +010051template <typename MetricType>
52uint64_t CounterValue(const MetricType& counter) {
Paul Duffin7d020882021-02-08 10:16:27 +000053 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};
Fairphone ODM25c12f52023-12-15 17:24:06 +080061 counter.Report({&backend});
Paul Duffin7d020882021-02-08 10:16:27 +000062 return counter_value;
63}
64
65template <DatumId histogram_type, size_t num_buckets, int64_t low_value, int64_t high_value>
66std::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};
Fairphone ODM25c12f52023-12-15 17:24:06 +080078 histogram.Report({&backend});
Paul Duffin7d020882021-02-08 10:16:27 +000079 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_