-
Notifications
You must be signed in to change notification settings - Fork 494
[TEST] Add stress test for histogram metric for multiple threads validation #3388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7862f04
[TEST] Add stress test for histogram metric for multiple threads vali…
ThomsonTan 9e22453
format
ThomsonTan b716dc2
Add new line to the end of stress test fle
ThomsonTan b673bc0
Fix iywu
ThomsonTan 58de6d2
Fix lambda capture
ThomsonTan a3f5a96
Make stop_collecting an atomic variable
ThomsonTan 1d51a7c
Add comment
ThomsonTan cbcd3d5
Increase timeout for stress run
ThomsonTan 2d47d30
Remove .cc from test name
ThomsonTan 23a7cff
Fix bazel format
ThomsonTan ff6c805
Remove uncessary capture
ThomsonTan a332699
Set default lambda capture mode to reference
ThomsonTan 2f30820
Rename variable for better clarify
ThomsonTan 2ab7311
Correct record_thread_count before using it
ThomsonTan 293620e
Reformat
ThomsonTan 6932d54
Test bucket count
ThomsonTan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <stdint.h> | ||
#include <atomic> | ||
#include <chrono> | ||
#include <random> | ||
#include <thread> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "common.h" | ||
#include "opentelemetry/context/context.h" | ||
#include "opentelemetry/metrics/meter.h" | ||
#include "opentelemetry/metrics/sync_instruments.h" | ||
#include "opentelemetry/nostd/function_ref.h" | ||
#include "opentelemetry/nostd/shared_ptr.h" | ||
#include "opentelemetry/nostd/unique_ptr.h" | ||
#include "opentelemetry/nostd/variant.h" | ||
#include "opentelemetry/sdk/common/exporter_utils.h" | ||
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h" | ||
#include "opentelemetry/sdk/metrics/data/metric_data.h" | ||
#include "opentelemetry/sdk/metrics/data/point_data.h" | ||
#include "opentelemetry/sdk/metrics/export/metric_producer.h" | ||
#include "opentelemetry/sdk/metrics/instruments.h" | ||
#include "opentelemetry/sdk/metrics/meter_provider.h" | ||
#include "opentelemetry/sdk/metrics/metric_reader.h" | ||
#include "opentelemetry/sdk/metrics/push_metric_exporter.h" | ||
|
||
using namespace opentelemetry; | ||
using namespace opentelemetry::sdk::instrumentationscope; | ||
using namespace opentelemetry::sdk::metrics; | ||
|
||
class MockMetricExporterForStress : public opentelemetry::sdk::metrics::PushMetricExporter | ||
{ | ||
public: | ||
MockMetricExporterForStress() = default; | ||
|
||
opentelemetry::sdk::metrics::AggregationTemporality GetAggregationTemporality( | ||
opentelemetry::sdk::metrics::InstrumentType) const noexcept override | ||
{ | ||
return AggregationTemporality::kDelta; | ||
} | ||
|
||
opentelemetry::sdk::common::ExportResult Export( | ||
const opentelemetry::sdk::metrics::ResourceMetrics &) noexcept override | ||
{ | ||
return opentelemetry::sdk::common::ExportResult::kSuccess; | ||
} | ||
|
||
bool ForceFlush(std::chrono::microseconds) noexcept override { return true; } | ||
|
||
bool Shutdown(std::chrono::microseconds) noexcept override { return true; } | ||
}; | ||
|
||
TEST(HistogramStress, UnsignedInt64) | ||
{ | ||
MeterProvider mp; | ||
auto m = mp.GetMeter("meter1", "version1", "schema1"); | ||
|
||
std::unique_ptr<MockMetricExporterForStress> exporter(new MockMetricExporterForStress()); | ||
std::shared_ptr<MetricReader> reader{new MockMetricReader(std::move(exporter))}; | ||
mp.AddMetricReader(reader); | ||
|
||
auto h = m->CreateUInt64Histogram("histogram1", "histogram1_description", "histogram1_unit"); | ||
|
||
// | ||
// Start a dedicated thread to collect the metrics | ||
// | ||
std::vector<HistogramPointData> actuals; | ||
auto stop_collecting = std::make_shared<std::atomic<bool>>(false); | ||
auto collect_thread = std::thread([&reader, &actuals, stop_collecting]() { | ||
while (!*stop_collecting) | ||
{ | ||
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); | ||
reader->Collect([&](ResourceMetrics &rm) { | ||
for (const ScopeMetrics &smd : rm.scope_metric_data_) | ||
{ | ||
for (const MetricData &md : smd.metric_data_) | ||
{ | ||
for (const PointDataAttributes &dp : md.point_data_attr_) | ||
{ | ||
actuals.push_back(opentelemetry::nostd::get<HistogramPointData>(dp.point_data)); | ||
} | ||
} | ||
} | ||
return true; | ||
}); | ||
} | ||
}); | ||
|
||
// | ||
// Start logging threads | ||
// | ||
int record_thread_count = std::thread::hardware_concurrency() - 1; | ||
if (record_thread_count <= 0) | ||
{ | ||
record_thread_count = 1; | ||
} | ||
|
||
std::vector<std::thread> threads(record_thread_count); | ||
constexpr int iterations_per_thread = 2000000; | ||
auto expected_sum = std::make_shared<std::atomic<uint64_t>>(0); | ||
|
||
for (int i = 0; i < record_thread_count; ++i) | ||
{ | ||
threads[i] = std::thread([&] { | ||
std::random_device rd; | ||
std::mt19937 random_engine(rd()); | ||
std::uniform_int_distribution<> gen_random(1, 20000); | ||
|
||
for (int j = 0; j < iterations_per_thread; ++j) | ||
{ | ||
int64_t val = gen_random(random_engine); | ||
expected_sum->fetch_add(val, std::memory_order_relaxed); | ||
h->Record(val, {}); | ||
} | ||
}); | ||
} | ||
|
||
for (int i = 0; i < record_thread_count; ++i) | ||
{ | ||
threads[i].join(); | ||
} | ||
|
||
// | ||
// Stop the dedicated collection thread | ||
// | ||
*stop_collecting = true; | ||
collect_thread.join(); | ||
|
||
// | ||
// run the the final collection | ||
// | ||
reader->Collect([&](ResourceMetrics &rm) { | ||
for (const ScopeMetrics &smd : rm.scope_metric_data_) | ||
{ | ||
for (const MetricData &md : smd.metric_data_) | ||
{ | ||
for (const PointDataAttributes &dp : md.point_data_attr_) | ||
{ | ||
actuals.push_back(opentelemetry::nostd::get<HistogramPointData>(dp.point_data)); | ||
} | ||
} | ||
} | ||
return true; | ||
}); | ||
|
||
// | ||
// Aggregate the results | ||
// | ||
int64_t expected_count = record_thread_count * iterations_per_thread; | ||
int64_t collected_count = 0; | ||
int64_t collected_sum = 0; | ||
for (const auto &actual : actuals) | ||
{ | ||
int64_t collected_bucket_sum = 0; | ||
for (const auto &count : actual.counts_) | ||
{ | ||
collected_bucket_sum += count; | ||
} | ||
ASSERT_EQ(collected_bucket_sum, actual.count_); | ||
|
||
collected_sum += opentelemetry::nostd::get<int64_t>(actual.sum_); | ||
collected_count += actual.count_; | ||
} | ||
|
||
ASSERT_EQ(expected_count, collected_count); | ||
ASSERT_EQ(*expected_sum, collected_sum); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.