|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#ifdef ENABLE_LOGS_PREVIEW |
| 5 | + |
| 6 | +# include "opentelemetry/common/timestamp.h" |
| 7 | +# include "opentelemetry/logs/logger.h" |
| 8 | +# include "opentelemetry/logs/provider.h" |
| 9 | +# include "opentelemetry/nostd/shared_ptr.h" |
| 10 | + |
| 11 | +# include "opentelemetry/exporters/user_events/logs/exporter.h" |
| 12 | +# include "opentelemetry/logs/provider.h" |
| 13 | +# include "opentelemetry/sdk/logs/logger_provider_factory.h" |
| 14 | +# include "opentelemetry/sdk/logs/simple_log_record_processor_factory.h" |
| 15 | + |
| 16 | +# include <chrono> |
| 17 | +# include <condition_variable> |
| 18 | +# include <functional> |
| 19 | +# include <mutex> |
| 20 | +# include <thread> |
| 21 | +# include <vector> |
| 22 | + |
| 23 | +# include <benchmark/benchmark.h> |
| 24 | + |
| 25 | +using opentelemetry::logs::EventId; |
| 26 | +using opentelemetry::logs::Logger; |
| 27 | +using opentelemetry::logs::LoggerProvider; |
| 28 | +using opentelemetry::logs::Provider; |
| 29 | +using opentelemetry::logs::Severity; |
| 30 | +using opentelemetry::nostd::shared_ptr; |
| 31 | +using opentelemetry::nostd::span; |
| 32 | +using opentelemetry::nostd::string_view; |
| 33 | + |
| 34 | +namespace common = opentelemetry::common; |
| 35 | +namespace nostd = opentelemetry::nostd; |
| 36 | +namespace trace = opentelemetry::trace; |
| 37 | +namespace log_api = opentelemetry::logs; |
| 38 | + |
| 39 | +namespace logs_api = opentelemetry::logs; |
| 40 | +namespace logs_sdk = opentelemetry::sdk::logs; |
| 41 | +namespace user_events_logs = opentelemetry::exporter::user_events::logs; |
| 42 | + |
| 43 | +namespace |
| 44 | +{ |
| 45 | + |
| 46 | +constexpr int64_t kMaxIterations = 1000000; |
| 47 | + |
| 48 | +class Barrier |
| 49 | +{ |
| 50 | +public: |
| 51 | + explicit Barrier(std::size_t iCount) : mThreshold(iCount), mCount(iCount), mGeneration(0) {} |
| 52 | + |
| 53 | + void Wait() |
| 54 | + { |
| 55 | + std::unique_lock<std::mutex> lLock{mMutex}; |
| 56 | + auto lGen = mGeneration; |
| 57 | + if (!--mCount) |
| 58 | + { |
| 59 | + mGeneration++; |
| 60 | + mCount = mThreshold; |
| 61 | + mCond.notify_all(); |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + mCond.wait(lLock, [this, lGen] { return lGen != mGeneration; }); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | +private: |
| 70 | + std::mutex mMutex; |
| 71 | + std::condition_variable mCond; |
| 72 | + std::size_t mThreshold; |
| 73 | + std::size_t mCount; |
| 74 | + std::size_t mGeneration; |
| 75 | +}; |
| 76 | + |
| 77 | +static void ThreadRoutine(Barrier &barrier, |
| 78 | + benchmark::State &state, |
| 79 | + int thread_id, |
| 80 | + std::function<void()> func) |
| 81 | +{ |
| 82 | + barrier.Wait(); |
| 83 | + |
| 84 | + if (thread_id == 0) |
| 85 | + { |
| 86 | + state.ResumeTiming(); |
| 87 | + } |
| 88 | + |
| 89 | + barrier.Wait(); |
| 90 | + |
| 91 | + func(); |
| 92 | + |
| 93 | + if (thread_id == 0) |
| 94 | + { |
| 95 | + state.PauseTiming(); |
| 96 | + } |
| 97 | + |
| 98 | + barrier.Wait(); |
| 99 | +} |
| 100 | + |
| 101 | +void MultiThreadRunner(benchmark::State &state, std::function<void()> func) |
| 102 | +{ |
| 103 | + int num_threads = std::thread::hardware_concurrency(); |
| 104 | + |
| 105 | + Barrier barrier(num_threads); |
| 106 | + |
| 107 | + std::vector<std::thread> threads; |
| 108 | + |
| 109 | + for (int i = 0; i < num_threads; i++) |
| 110 | + { |
| 111 | + threads.emplace_back(ThreadRoutine, std::ref(barrier), std::ref(state), i, func); |
| 112 | + } |
| 113 | + |
| 114 | + for (auto &thread : threads) |
| 115 | + { |
| 116 | + thread.join(); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +void InitLogger() |
| 121 | +{ |
| 122 | + // Create user_events log exporter instance |
| 123 | + auto exporter_options = user_events_logs::ExporterOptions(); |
| 124 | + auto exporter = |
| 125 | + std::unique_ptr<user_events_logs::Exporter>(new user_events_logs::Exporter(exporter_options)); |
| 126 | + auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter)); |
| 127 | + std::shared_ptr<logs_api::LoggerProvider> provider( |
| 128 | + logs_sdk::LoggerProviderFactory::Create(std::move(processor))); |
| 129 | + |
| 130 | + // Set the global logger provider |
| 131 | + logs_api::Provider::SetLoggerProvider(provider); |
| 132 | +} |
| 133 | + |
| 134 | +static void BM_StructuredLogWithEventIdStructAndTwoAttributes(benchmark::State &state) |
| 135 | +{ |
| 136 | + InitLogger(); |
| 137 | + |
| 138 | + auto lp = Provider::GetLoggerProvider(); |
| 139 | + auto logger = lp->GetLogger("StructuredLogWithEventId"); |
| 140 | + |
| 141 | + const EventId function_name_event_id{0x12345678, "Company.Component.SubComponent.FunctionName"}; |
| 142 | + |
| 143 | + for (auto _ : state) |
| 144 | + { |
| 145 | + state.PauseTiming(); |
| 146 | + |
| 147 | + MultiThreadRunner(state, [&logger, &function_name_event_id]() { |
| 148 | + for (int64_t i = 0; i < kMaxIterations; i++) |
| 149 | + { |
| 150 | + logger->Trace( |
| 151 | + function_name_event_id, |
| 152 | + "Simulate function enter trace message from {process_id}:{thread_id}", |
| 153 | + opentelemetry::common::MakeAttributes({{"process_id", 12347}, {"thread_id", 12348}})); |
| 154 | + } |
| 155 | + }); |
| 156 | + |
| 157 | + state.ResumeTiming(); |
| 158 | + } |
| 159 | +} |
| 160 | +BENCHMARK(BM_StructuredLogWithEventIdStructAndTwoAttributes); |
| 161 | + |
| 162 | +} // namespace |
| 163 | + |
| 164 | +int main(int argc, char **argv) |
| 165 | +{ |
| 166 | + benchmark::Initialize(&argc, argv); |
| 167 | + benchmark::RunSpecifiedBenchmarks(); |
| 168 | +} |
| 169 | + |
| 170 | +#endif |
0 commit comments