]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/sdk/test/_metrics/exact_aggregator_test.cc
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
4 #include <gtest/gtest.h>
6 #ifdef ENABLE_METRICS_PREVIEW
9 # include "opentelemetry/sdk/_metrics/aggregator/exact_aggregator.h"
11 using namespace opentelemetry::sdk::metrics
;
12 namespace metrics_api
= opentelemetry::metrics
;
14 TEST(ExactAggregatorOrdered
, Update
)
16 ExactAggregator
<int> agg(metrics_api::InstrumentKind::Counter
);
18 std::vector
<int> correct
;
20 ASSERT_EQ(agg
.get_values(), correct
);
25 ASSERT_EQ(agg
.get_values(), std::vector
<int>{1});
27 for (int i
= 2; i
<= 5; ++i
)
32 ASSERT_EQ(agg
.get_values(), correct
);
35 TEST(ExactAggregatorOrdered
, Checkpoint
)
37 ExactAggregator
<int> agg(metrics_api::InstrumentKind::Counter
);
39 std::vector
<int> correct
;
41 ASSERT_EQ(agg
.get_checkpoint(), correct
);
47 ASSERT_EQ(agg
.get_checkpoint(), correct
);
50 TEST(ExactAggregatorOrdered
, Merge
)
52 ExactAggregator
<int> agg1(metrics_api::InstrumentKind::Counter
);
53 ExactAggregator
<int> agg2(metrics_api::InstrumentKind::Counter
);
59 std::vector
<int> correct
{1, 2};
61 ASSERT_EQ(agg1
.get_values(), correct
);
64 TEST(ExactAggregatorOrdered
, BadMerge
)
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
);
76 // Verify that the aggregators did NOT merge
77 std::vector
<int> correct
{1};
78 ASSERT_EQ(agg1
.get_values(), correct
);
81 TEST(ExactAggregatorOrdered
, Types
)
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
);
90 for (int i
= 1; i
<= 5; ++i
)
96 for (float i
= 1.0; i
<= 5.0; i
+= 1)
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};
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
);
113 TEST(ExactAggregatorQuant
, Update
)
115 ExactAggregator
<int> agg(metrics_api::InstrumentKind::Counter
, true);
117 std::vector
<int> correct
;
119 ASSERT_EQ(agg
.get_values(), correct
);
122 correct
.push_back(1);
124 ASSERT_EQ(agg
.get_values(), std::vector
<int>{1});
126 for (int i
= 2; i
<= 5; ++i
)
128 correct
.push_back(i
);
131 ASSERT_EQ(agg
.get_values(), correct
);
134 TEST(ExactAggregatorQuant
, Checkpoint
)
136 // This test verifies that the aggregator updates correctly when
137 // quantile estimation is turned on.
139 ExactAggregator
<int> agg(metrics_api::InstrumentKind::Counter
, true);
141 std::vector
<int> correct
;
143 ASSERT_EQ(agg
.get_checkpoint(), correct
);
149 // The vector MUST be sorted when checkpointed
150 correct
.push_back(-1);
151 correct
.push_back(0);
152 correct
.push_back(1);
155 ASSERT_EQ(agg
.get_checkpoint(), correct
);
158 TEST(ExactAggregatorQuant
, Quantile
)
160 // This test verifies that the quantile estimation function returns
161 // the correct values.
163 ExactAggregator
<int> agg(metrics_api::InstrumentKind::Counter
, true);
165 std::vector
<int> tmp
{3, 9, 42, 57, 163, 210, 272, 300};
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);
176 TEST(ExactAggregatorInOrder
, Quantile
)
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
);
182 std::vector
<int> tmp
{3, 9, 42, 57, 163, 210, 272, 300};
189 ASSERT_THROW(agg
.get_quantiles(0.5), std::domain_error
);
194 void callback(ExactAggregator
<int> &agg
)
196 for (int i
= 1; i
<= 10000; ++i
)
202 TEST(ExactAggregatorQuant
, Concurrency
)
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);
208 std::thread
first(&callback
, std::ref(agg
));
209 std::thread
second(&callback
, std::ref(agg
));
214 std::vector
<int> correct
;
215 for (int i
= 1; i
<= 10000; ++i
)
217 correct
.push_back(i
);
218 correct
.push_back(i
);
222 ASSERT_EQ(agg
.get_checkpoint(), correct
);
225 TEST(ExactAggregatorQuant
, DummyTest
)