diff --git a/sdk/include/opentelemetry/sdk/metrics/state/attributes_hashmap.h b/sdk/include/opentelemetry/sdk/metrics/state/attributes_hashmap.h index 686146e86b..32301f8038 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/attributes_hashmap.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/attributes_hashmap.h @@ -5,6 +5,10 @@ #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/common/spin_lock_mutex.h" # include "opentelemetry/nostd/function_ref.h" +# include "opentelemetry/sdk/common/attribute_utils.h" +# include "opentelemetry/sdk/common/attributemap_hash.h" +# include "opentelemetry/sdk/metrics/aggregation/aggregation.h" +# include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/version.h" # include diff --git a/sdk/test/metrics/CMakeLists.txt b/sdk/test/metrics/CMakeLists.txt index 3f118a0c8d..279ff541fd 100644 --- a/sdk/test/metrics/CMakeLists.txt +++ b/sdk/test/metrics/CMakeLists.txt @@ -4,6 +4,7 @@ foreach( view_registry_test aggregation_test attributes_processor_test + attributes_hashmap_test sync_metric_storage_test sync_instruments_test async_instruments_test) @@ -20,3 +21,7 @@ endforeach() add_executable(attributes_processor_benchmark attributes_processor_benchmark.cc) target_link_libraries(attributes_processor_benchmark benchmark::benchmark ${CMAKE_THREAD_LIBS_INIT} opentelemetry_common) + +add_executable(attributes_hashmap_benchmark attributes_hashmap_benchmark.cc) +target_link_libraries(attributes_hashmap_benchmark benchmark::benchmark + ${CMAKE_THREAD_LIBS_INIT} opentelemetry_common) diff --git a/sdk/test/metrics/attributes_hashmap_benchmark.cc b/sdk/test/metrics/attributes_hashmap_benchmark.cc index 80dc0286f8..68160aa579 100644 --- a/sdk/test/metrics/attributes_hashmap_benchmark.cc +++ b/sdk/test/metrics/attributes_hashmap_benchmark.cc @@ -8,7 +8,6 @@ # include "opentelemetry/sdk/metrics/aggregation/drop_aggregation.h" # include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/sdk/metrics/state/attributes_hashmap.h" -# include "opentelemetry/sdk/metrics/state/attributes_hashmap_unordered.h" # include # include @@ -21,9 +20,7 @@ namespace void BM_AttributseHashMap(benchmark::State &state) { - AttributesHashMap - hash_map; + AttributesHashMap hash_map; std::vector workers; std::vector attributes = {{{"k1", "v1"}, {"k2", "v2"}}, {{"k1", "v1"}, {"k2", "v2"}, {"k3", "v3"}}}; @@ -52,39 +49,6 @@ void BM_AttributseHashMap(benchmark::State &state) } BENCHMARK(BM_AttributseHashMap); - -void BM_AttributseHashMapUnordered(benchmark::State &state) -{ - - AttributesHashMapUnordered hash_map; - std::vector workers; - std::vector attributes = {{{"k1", "v1"}, {"k2", "v2"}}, - {{"k1", "v1"}, {"k2", "v2"}, {"k3", "v3"}}}; - - std::function()> create_default_aggregation = - []() -> std::unique_ptr { - auto agg = std::unique_ptr(new DropAggregation); - return std::move(agg); - }; - - while (state.KeepRunning()) - { - for (int i = 0; i < MAX_THREADS; i++) - { - workers.push_back(std::thread([&]() { - hash_map.GetOrSetDefault(attributes[i % 2], create_default_aggregation)->Aggregate(1l); - benchmark::DoNotOptimize(hash_map.Has(attributes[i % 2])); - })); - } - } - - for (auto &t : workers) - { - t.join(); - } -} - -BENCHMARK(BM_AttributseHashMapUnordered); } // namespace #endif -BENCHMARK_MAIN(); +BENCHMARK_MAIN(); \ No newline at end of file diff --git a/sdk/test/metrics/attributes_hashmap_test.cc b/sdk/test/metrics/attributes_hashmap_test.cc index c2500420e5..454ece1ea0 100644 --- a/sdk/test/metrics/attributes_hashmap_test.cc +++ b/sdk/test/metrics/attributes_hashmap_test.cc @@ -4,8 +4,6 @@ #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/sdk/metrics/state/attributes_hashmap.h" # include -# include "opentelemetry/sdk/common/attributemap_hash.h" -# include "opentelemetry/sdk/metrics/aggregation/aggregation.h" # include "opentelemetry/sdk/metrics/aggregation/drop_aggregation.h" # include "opentelemetry/sdk/metrics/instruments.h" @@ -13,34 +11,34 @@ using namespace opentelemetry::sdk::metrics; namespace nostd = opentelemetry::nostd; + TEST(AttributesHashMap, BasicTests) { // Empty map - AttributesHashMap - hash_map; + AttributesHashMap hash_map; EXPECT_EQ(hash_map.Size(), 0); MetricAttributes m1 = {{"k1", "v1"}}; EXPECT_EQ(hash_map.Get(m1), nullptr); EXPECT_EQ(hash_map.Has(m1), false); // Set - auto aggregation1 = std::unique_ptr(new DropAggregation); + std::unique_ptr aggregation1( + new DropAggregation()); // = std::unique_ptr(new DropAggregation); hash_map.Set(m1, std::move(aggregation1)); EXPECT_NO_THROW(hash_map.Get(m1)->Aggregate(1l)); EXPECT_EQ(hash_map.Size(), 1); EXPECT_EQ(hash_map.Has(m1), true); // Set same key again - auto aggregation2 = std::unique_ptr(new DropAggregation); + auto aggregation2 = std::unique_ptr(new DropAggregation()); hash_map.Set(m1, std::move(aggregation2)); EXPECT_NO_THROW(hash_map.Get(m1)->Aggregate(1l)); EXPECT_EQ(hash_map.Size(), 1); EXPECT_EQ(hash_map.Has(m1), true); // Set more enteria - auto aggregation3 = std::unique_ptr(new DropAggregation); + auto aggregation3 = std::unique_ptr(new DropAggregation()); MetricAttributes m3 = {{"k1", "v1"}, {"k2", "v2"}}; hash_map.Set(m3, std::move(aggregation3)); EXPECT_EQ(hash_map.Has(m1), true); @@ -64,11 +62,10 @@ TEST(AttributesHashMap, BasicTests) // GetAllEnteries size_t count = 0; - hash_map.GetAllEnteries( - [&count](hashcode_t hash, const MetricAttributes &attributes, Aggregation &aggregation) { - count++; - return true; - }); + hash_map.GetAllEnteries([&count](const MetricAttributes &attributes, Aggregation &aggregation) { + count++; + return true; + }); EXPECT_EQ(count, hash_map.Size()); }