]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/sdk/test/_metrics/min_max_sum_count_aggregator_test.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / sdk / test / _metrics / min_max_sum_count_aggregator_test.cc
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 #ifdef ENABLE_METRICS_PREVIEW
5 # include <gtest/gtest.h>
6 # include <thread>
7
8 # include "opentelemetry/sdk/_metrics/aggregator/min_max_sum_count_aggregator.h"
9
10 using namespace opentelemetry::sdk::metrics;
11 namespace metrics_api = opentelemetry::metrics;
12
13 TEST(MinMaxSumCountAggregator, Update)
14 {
15 // This tests that the aggregator updates the maintained value correctly
16 // after a call to the update() function.
17 MinMaxSumCountAggregator<int> agg(metrics_api::InstrumentKind::Counter);
18 auto value_set = agg.get_values();
19 ASSERT_EQ(value_set[0], 0);
20 ASSERT_EQ(value_set[1], 0);
21 ASSERT_EQ(value_set[2], 0);
22 ASSERT_EQ(value_set[3], 0);
23
24 // 1 + 2 + 3 + ... + 10 = 55
25 for (int i = 1; i <= 10; ++i)
26 {
27 agg.update(i);
28 }
29
30 value_set = agg.get_values();
31 ASSERT_EQ(value_set[0], 1); // min
32 ASSERT_EQ(value_set[1], 10); // max
33 ASSERT_EQ(value_set[2], 55); // sum
34 ASSERT_EQ(value_set[3], 10); // count
35 }
36
37 TEST(MinMaxSumCountAggregator, FirstUpdate)
38 {
39 // This tests that the aggregator appropriately maintains the min and
40 // max values after a single update call.
41 MinMaxSumCountAggregator<int> agg(metrics_api::InstrumentKind::Counter);
42 agg.update(1);
43 auto value_set = agg.get_values();
44 ASSERT_EQ(value_set[0], 1); // min
45 ASSERT_EQ(value_set[1], 1); // max
46 ASSERT_EQ(value_set[2], 1); // sum
47 ASSERT_EQ(value_set[3], 1); // count
48 }
49
50 TEST(MinMaxSumCountAggregator, Checkpoint)
51 {
52 // This test verifies that the default checkpoint is set correctly
53 // and that the checkpoint values update correctly after a call
54 // to the checkpoint() function.
55 MinMaxSumCountAggregator<int> agg(metrics_api::InstrumentKind::Counter);
56
57 // Verify that the default checkpoint is set correctly.
58 auto checkpoint_set = agg.get_checkpoint();
59 ASSERT_EQ(checkpoint_set[0], 0); // min
60 ASSERT_EQ(checkpoint_set[1], 0); // max
61 ASSERT_EQ(checkpoint_set[2], 0); // sum
62 ASSERT_EQ(checkpoint_set[3], 0); // count
63
64 // 1 + 2 + 3 + ... + 10 = 55
65 for (int i = 1; i <= 10; ++i)
66 {
67 agg.update(i);
68 }
69
70 agg.checkpoint();
71
72 // Verify that the checkpoint values were updated.
73 checkpoint_set = agg.get_checkpoint();
74 ASSERT_EQ(checkpoint_set[0], 1); // min
75 ASSERT_EQ(checkpoint_set[1], 10); // max
76 ASSERT_EQ(checkpoint_set[2], 55); // sum
77 ASSERT_EQ(checkpoint_set[3], 10); // count
78
79 // Verify that the current values were reset to the default state.
80 auto value_set = agg.get_values();
81 ASSERT_EQ(value_set[0], 0); // min
82 ASSERT_EQ(value_set[1], 0); // max
83 ASSERT_EQ(value_set[2], 0); // sum
84 ASSERT_EQ(value_set[3], 0); // count
85 }
86
87 TEST(MinMaxSumCountAggregator, Merge)
88 {
89 // This tests that the values_ vector is updated correctly after
90 // two aggregators are merged together.
91 MinMaxSumCountAggregator<int> agg1(metrics_api::InstrumentKind::Counter);
92 MinMaxSumCountAggregator<int> agg2(metrics_api::InstrumentKind::Counter);
93
94 // 1 + 2 + 3 + ... + 10 = 55
95 for (int i = 1; i <= 10; ++i)
96 {
97 agg1.update(i);
98 }
99
100 // 1 + 2 + 3 + ... + 20 = 210
101 for (int i = 1; i <= 20; ++i)
102 {
103 agg2.update(i);
104 }
105
106 agg1.merge(agg2);
107
108 // Verify that the current values were changed by the merge.
109 auto value_set = agg1.get_values();
110 ASSERT_EQ(value_set[0], 1); // min
111 ASSERT_EQ(value_set[1], 20); // max
112 ASSERT_EQ(value_set[2], 265); // sum
113 ASSERT_EQ(value_set[3], 30); // count
114 }
115
116 TEST(MinMaxSumCountAggregator, BadMerge)
117 {
118 // This verifies that we encounter and error when we try to merge
119 // two aggregators of different numeric types together.
120 MinMaxSumCountAggregator<int> agg1(metrics_api::InstrumentKind::Counter);
121 MinMaxSumCountAggregator<int> agg2(metrics_api::InstrumentKind::ValueRecorder);
122
123 agg1.update(1);
124 agg2.update(2);
125
126 agg1.merge(agg2);
127
128 // Verify that the values did NOT merge
129 auto value_set = agg1.get_values();
130 ASSERT_EQ(value_set[0], 1); // min
131 ASSERT_EQ(value_set[0], 1); // max
132 ASSERT_EQ(value_set[0], 1); // sum
133 ASSERT_EQ(value_set[0], 1); // count
134 }
135
136 TEST(MinMaxSumCountAggregator, Types)
137 {
138 // This test verifies that we do not encounter any errors when
139 // using various numeric types.
140 MinMaxSumCountAggregator<int> agg_int(metrics_api::InstrumentKind::Counter);
141 MinMaxSumCountAggregator<long> agg_long(metrics_api::InstrumentKind::Counter);
142 MinMaxSumCountAggregator<float> agg_float(metrics_api::InstrumentKind::Counter);
143 MinMaxSumCountAggregator<double> agg_double(metrics_api::InstrumentKind::Counter);
144
145 for (int i = 1; i <= 10; ++i)
146 {
147 agg_int.update(i);
148 agg_long.update(i);
149 }
150
151 for (float i = 1.0; i <= 10.0; i += 1)
152 {
153 agg_float.update(i);
154 agg_double.update(i);
155 }
156
157 auto value_set = agg_int.get_values();
158 ASSERT_EQ(value_set[0], 1); // min
159 ASSERT_EQ(value_set[1], 10); // max
160 ASSERT_EQ(value_set[2], 55); // sum
161 ASSERT_EQ(value_set[3], 10); // count
162
163 auto value_set2 = agg_long.get_values();
164 ASSERT_EQ(value_set[0], 1); // min
165 ASSERT_EQ(value_set[1], 10); // max
166 ASSERT_EQ(value_set[2], 55); // sum
167 ASSERT_EQ(value_set[3], 10); // count
168
169 auto value_set3 = agg_float.get_values();
170 ASSERT_EQ(value_set[0], 1.0); // min
171 ASSERT_EQ(value_set[1], 10.0); // max
172 ASSERT_EQ(value_set[2], 55.0); // sum
173 ASSERT_EQ(value_set[3], 10); // count
174
175 auto value_set4 = agg_double.get_values();
176 ASSERT_EQ(value_set[0], 1.0); // min
177 ASSERT_EQ(value_set[1], 10.0); // max
178 ASSERT_EQ(value_set[2], 55.0); // sum
179 ASSERT_EQ(value_set[3], 10); // count
180 }
181
182 static void callback(MinMaxSumCountAggregator<int> &agg)
183 {
184 // 1 + 2 + ... + 10000 = 50005000
185 for (int i = 1; i <= 10000; ++i)
186 {
187 agg.update(i);
188 }
189 }
190
191 TEST(MinMaxSumCountAggregator, Concurrency)
192 {
193 // This test checks that the aggregator updates appropriately
194 // when called in a multi-threaded context.
195 MinMaxSumCountAggregator<int> agg(metrics_api::InstrumentKind::Counter);
196
197 std::thread first(&callback, std::ref(agg));
198 std::thread second(&callback, std::ref(agg));
199
200 first.join();
201 second.join();
202
203 auto value_set = agg.get_values();
204 ASSERT_EQ(value_set[0], 1);
205 ASSERT_EQ(value_set[1], 10000);
206 ASSERT_EQ(value_set[2], 2 * 50005000);
207 ASSERT_EQ(value_set[3], 2 * 10000);
208 }
209 #endif