Skip to content

Commit

Permalink
benchmark fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb committed Feb 16, 2022
1 parent 96a1ef5 commit e223cb7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <functional>
Expand Down
5 changes: 5 additions & 0 deletions sdk/test/metrics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
40 changes: 2 additions & 38 deletions sdk/test/metrics/attributes_hashmap_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <functional>
# include <vector>
Expand All @@ -21,9 +20,7 @@ namespace
void BM_AttributseHashMap(benchmark::State &state)
{

AttributesHashMap<MetricAttributes, Aggregation,
&opentelemetry::sdk::common::GetHashForAttributeMap>
hash_map;
AttributesHashMap hash_map;
std::vector<std::thread> workers;
std::vector<MetricAttributes> attributes = {{{"k1", "v1"}, {"k2", "v2"}},
{{"k1", "v1"}, {"k2", "v2"}, {"k3", "v3"}}};
Expand Down Expand Up @@ -52,39 +49,6 @@ void BM_AttributseHashMap(benchmark::State &state)
}

BENCHMARK(BM_AttributseHashMap);

void BM_AttributseHashMapUnordered(benchmark::State &state)
{

AttributesHashMapUnordered hash_map;
std::vector<std::thread> workers;
std::vector<MetricAttributes> attributes = {{{"k1", "v1"}, {"k2", "v2"}},
{{"k1", "v1"}, {"k2", "v2"}, {"k3", "v3"}}};

std::function<std::unique_ptr<Aggregation>()> create_default_aggregation =
[]() -> std::unique_ptr<Aggregation> {
auto agg = std::unique_ptr<Aggregation>(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();
23 changes: 10 additions & 13 deletions sdk/test/metrics/attributes_hashmap_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,41 @@
#ifndef ENABLE_METRICS_PREVIEW
# include "opentelemetry/sdk/metrics/state/attributes_hashmap.h"
# include <gtest/gtest.h>
# 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"

# include <functional>

using namespace opentelemetry::sdk::metrics;
namespace nostd = opentelemetry::nostd;

TEST(AttributesHashMap, BasicTests)
{

// Empty map
AttributesHashMap<MetricAttributes, Aggregation,
&opentelemetry::sdk::common::GetHashForAttributeMap>
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<Aggregation>(new DropAggregation);
std::unique_ptr<Aggregation> aggregation1(
new DropAggregation()); // = std::unique_ptr<Aggregation>(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<Aggregation>(new DropAggregation);
auto aggregation2 = std::unique_ptr<Aggregation>(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<Aggregation>(new DropAggregation);
auto aggregation3 = std::unique_ptr<Aggregation>(new DropAggregation());
MetricAttributes m3 = {{"k1", "v1"}, {"k2", "v2"}};
hash_map.Set(m3, std::move(aggregation3));
EXPECT_EQ(hash_map.Has(m1), true);
Expand All @@ -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());
}

Expand Down

0 comments on commit e223cb7

Please sign in to comment.