]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/sdk/test/_metrics/exact_aggregator_test.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / sdk / test / _metrics / exact_aggregator_test.cc
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <gtest/gtest.h>
5
6 #ifdef ENABLE_METRICS_PREVIEW
7 # include <thread>
8
9 # include "opentelemetry/sdk/_metrics/aggregator/exact_aggregator.h"
10
11 using namespace opentelemetry::sdk::metrics;
12 namespace metrics_api = opentelemetry::metrics;
13
14 TEST(ExactAggregatorOrdered, Update)
15 {
16 ExactAggregator<int> agg(metrics_api::InstrumentKind::Counter);
17
18 std::vector<int> correct;
19
20 ASSERT_EQ(agg.get_values(), correct);
21
22 agg.update(1);
23 correct.push_back(1);
24
25 ASSERT_EQ(agg.get_values(), std::vector<int>{1});
26
27 for (int i = 2; i <= 5; ++i)
28 {
29 correct.push_back(i);
30 agg.update(i);
31 }
32 ASSERT_EQ(agg.get_values(), correct);
33 }
34
35 TEST(ExactAggregatorOrdered, Checkpoint)
36 {
37 ExactAggregator<int> agg(metrics_api::InstrumentKind::Counter);
38
39 std::vector<int> correct;
40
41 ASSERT_EQ(agg.get_checkpoint(), correct);
42
43 agg.update(1);
44 correct.push_back(1);
45 agg.checkpoint();
46
47 ASSERT_EQ(agg.get_checkpoint(), correct);
48 }
49
50 TEST(ExactAggregatorOrdered, Merge)
51 {
52 ExactAggregator<int> agg1(metrics_api::InstrumentKind::Counter);
53 ExactAggregator<int> agg2(metrics_api::InstrumentKind::Counter);
54
55 agg1.update(1);
56 agg2.update(2);
57 agg1.merge(agg2);
58
59 std::vector<int> correct{1, 2};
60
61 ASSERT_EQ(agg1.get_values(), correct);
62 }
63
64 TEST(ExactAggregatorOrdered, BadMerge)
65 {
66 // This verifies that we encounter and error when we try to merge
67 // two aggregators of different numeric types together.
68 ExactAggregator<int> agg1(metrics_api::InstrumentKind::Counter);
69 ExactAggregator<int> agg2(metrics_api::InstrumentKind::ValueRecorder);
70
71 agg1.update(1);
72 agg2.update(2);
73
74 agg1.merge(agg2);
75
76 // Verify that the aggregators did NOT merge
77 std::vector<int> correct{1};
78 ASSERT_EQ(agg1.get_values(), correct);
79 }
80
81 TEST(ExactAggregatorOrdered, Types)
82 {
83 // This test verifies that we do not encounter any errors when
84 // using various numeric types.
85 ExactAggregator<int> agg_int(metrics_api::InstrumentKind::Counter);
86 ExactAggregator<long> agg_long(metrics_api::InstrumentKind::Counter);
87 ExactAggregator<float> agg_float(metrics_api::InstrumentKind::Counter);
88 ExactAggregator<double> agg_double(metrics_api::InstrumentKind::Counter);
89
90 for (int i = 1; i <= 5; ++i)
91 {
92 agg_int.update(i);
93 agg_long.update(i);
94 }
95
96 for (float i = 1.0; i <= 5.0; i += 1)
97 {
98 agg_float.update(i);
99 agg_double.update(i);
100 }
101
102 std::vector<int> correct_int{1, 2, 3, 4, 5};
103 std::vector<long> correct_long{1, 2, 3, 4, 5};
104 std::vector<float> correct_float{1.0, 2.0, 3.0, 4.0, 5.0};
105 std::vector<double> correct_double{1.0, 2.0, 3.0, 4.0, 5.0};
106
107 ASSERT_EQ(agg_int.get_values(), correct_int);
108 ASSERT_EQ(agg_long.get_values(), correct_long);
109 ASSERT_EQ(agg_float.get_values(), correct_float);
110 ASSERT_EQ(agg_double.get_values(), correct_double);
111 }
112
113 TEST(ExactAggregatorQuant, Update)
114 {
115 ExactAggregator<int> agg(metrics_api::InstrumentKind::Counter, true);
116
117 std::vector<int> correct;
118
119 ASSERT_EQ(agg.get_values(), correct);
120
121 agg.update(1);
122 correct.push_back(1);
123
124 ASSERT_EQ(agg.get_values(), std::vector<int>{1});
125
126 for (int i = 2; i <= 5; ++i)
127 {
128 correct.push_back(i);
129 agg.update(i);
130 }
131 ASSERT_EQ(agg.get_values(), correct);
132 }
133
134 TEST(ExactAggregatorQuant, Checkpoint)
135 {
136 // This test verifies that the aggregator updates correctly when
137 // quantile estimation is turned on.
138
139 ExactAggregator<int> agg(metrics_api::InstrumentKind::Counter, true);
140
141 std::vector<int> correct;
142
143 ASSERT_EQ(agg.get_checkpoint(), correct);
144
145 agg.update(1);
146 agg.update(0);
147 agg.update(-1);
148
149 // The vector MUST be sorted when checkpointed
150 correct.push_back(-1);
151 correct.push_back(0);
152 correct.push_back(1);
153 agg.checkpoint();
154
155 ASSERT_EQ(agg.get_checkpoint(), correct);
156 }
157
158 TEST(ExactAggregatorQuant, Quantile)
159 {
160 // This test verifies that the quantile estimation function returns
161 // the correct values.
162
163 ExactAggregator<int> agg(metrics_api::InstrumentKind::Counter, true);
164
165 std::vector<int> tmp{3, 9, 42, 57, 163, 210, 272, 300};
166 for (int i : tmp)
167 {
168 agg.update(i);
169 }
170 agg.checkpoint();
171 ASSERT_EQ(agg.get_quantiles(.25), 42);
172 ASSERT_EQ(agg.get_quantiles(0.5), 163);
173 ASSERT_EQ(agg.get_quantiles(0.75), 272);
174 }
175
176 TEST(ExactAggregatorInOrder, Quantile)
177 {
178 // This test verifies that if the user has an exact aggregator in "in-order" mode
179 // an exception will be thrown if they call the quantile() function.
180 ExactAggregator<int> agg(metrics_api::InstrumentKind::Counter);
181
182 std::vector<int> tmp{3, 9, 42, 57, 163, 210, 272, 300};
183 for (int i : tmp)
184 {
185 agg.update(i);
186 }
187 agg.checkpoint();
188 # if __EXCEPTIONS
189 ASSERT_THROW(agg.get_quantiles(0.5), std::domain_error);
190 # else
191 # endif
192 }
193
194 void callback(ExactAggregator<int> &agg)
195 {
196 for (int i = 1; i <= 10000; ++i)
197 {
198 agg.update(i);
199 }
200 }
201
202 TEST(ExactAggregatorQuant, Concurrency)
203 {
204 // This test checks that the aggregator updates appropriately
205 // when called in a multi-threaded context.
206 ExactAggregator<int> agg(metrics_api::InstrumentKind::Counter, true);
207
208 std::thread first(&callback, std::ref(agg));
209 std::thread second(&callback, std::ref(agg));
210
211 first.join();
212 second.join();
213
214 std::vector<int> correct;
215 for (int i = 1; i <= 10000; ++i)
216 {
217 correct.push_back(i);
218 correct.push_back(i);
219 }
220 agg.checkpoint();
221
222 ASSERT_EQ(agg.get_checkpoint(), correct);
223 }
224 #else
225 TEST(ExactAggregatorQuant, DummyTest)
226 {
227 // empty
228 }
229 #endif