]>
git.proxmox.com Git - ceph.git/blob - 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
4 #ifdef ENABLE_METRICS_PREVIEW
5 # include <gtest/gtest.h>
8 # include "opentelemetry/sdk/_metrics/aggregator/min_max_sum_count_aggregator.h"
10 using namespace opentelemetry::sdk::metrics
;
11 namespace metrics_api
= opentelemetry::metrics
;
13 TEST(MinMaxSumCountAggregator
, Update
)
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);
24 // 1 + 2 + 3 + ... + 10 = 55
25 for (int i
= 1; i
<= 10; ++i
)
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
37 TEST(MinMaxSumCountAggregator
, FirstUpdate
)
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
);
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
50 TEST(MinMaxSumCountAggregator
, Checkpoint
)
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
);
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
64 // 1 + 2 + 3 + ... + 10 = 55
65 for (int i
= 1; i
<= 10; ++i
)
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
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
87 TEST(MinMaxSumCountAggregator
, Merge
)
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
);
94 // 1 + 2 + 3 + ... + 10 = 55
95 for (int i
= 1; i
<= 10; ++i
)
100 // 1 + 2 + 3 + ... + 20 = 210
101 for (int i
= 1; i
<= 20; ++i
)
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
116 TEST(MinMaxSumCountAggregator
, BadMerge
)
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
);
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
136 TEST(MinMaxSumCountAggregator
, Types
)
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
);
145 for (int i
= 1; i
<= 10; ++i
)
151 for (float i
= 1.0; i
<= 10.0; i
+= 1)
154 agg_double
.update(i
);
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
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
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
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
182 static void callback(MinMaxSumCountAggregator
<int> &agg
)
184 // 1 + 2 + ... + 10000 = 50005000
185 for (int i
= 1; i
<= 10000; ++i
)
191 TEST(MinMaxSumCountAggregator
, Concurrency
)
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
);
197 std::thread
first(&callback
, std::ref(agg
));
198 std::thread
second(&callback
, std::ref(agg
));
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);