etw_perf_test.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #ifdef _WIN32
  4. # include <benchmark/benchmark.h>
  5. # include <gtest/gtest.h>
  6. # include <map>
  7. # include <string>
  8. # include <unordered_map>
  9. # include "opentelemetry/exporters/etw/etw_tracer_exporter.h"
  10. # include "opentelemetry/sdk/trace/simple_processor.h"
  11. using namespace OPENTELEMETRY_NAMESPACE;
  12. using namespace opentelemetry::exporter::etw;
  13. namespace
  14. {
  15. static constexpr const char *providerName = "OpenTelemetry-ETW-StressTest";
  16. static exporter::etw::TelemetryProviderOptions providerOptions = {
  17. {"enableTraceId", false},
  18. {"enableSpanId", false},
  19. {"enableActivityId", false},
  20. {"enableRelatedActivityId", false},
  21. {"enableAutoParent", false}};
  22. class ETWProviderStressTest
  23. {
  24. exporter::etw::TracerProvider provider_;
  25. std::string mode_;
  26. nostd::shared_ptr<trace::Tracer> tracer_;
  27. nostd::shared_ptr<trace::Span> span_;
  28. public:
  29. /**
  30. * @brief Construct ETW Provider stress test object
  31. * @param mode Operational mode: "TLD" or "MsgPack"
  32. */
  33. ETWProviderStressTest(std::string mode = "TLD") : mode_(mode), provider_(providerOptions) {}
  34. /**
  35. * @brief Initializer tracer and start a Span
  36. */
  37. void Initialize()
  38. {
  39. tracer_ = provider_.GetTracer(providerName, mode_);
  40. span_ = tracer_->StartSpan("Span");
  41. }
  42. /**
  43. * @brief Obtain the tracer
  44. */
  45. nostd::shared_ptr<trace::Tracer> GetTracer() { return tracer_; }
  46. /**
  47. * @brief Add event using Properties container
  48. */
  49. bool AddProperties()
  50. {
  51. std::string eventName = "MyEvent";
  52. Properties event = {{"uint32Key", (uint32_t)1234},
  53. {"uint64Key", (uint64_t)1234567890},
  54. {"strKey", "someValue"}};
  55. span_->AddEvent(eventName, event);
  56. return true;
  57. }
  58. /**
  59. * @brief Add event using static preallocated "reusable" Properties container.
  60. * This approach works well for single-threaded flows, but may not be safe in
  61. * some multithreaded scenarios in case if reusable `Properties` get concurrently
  62. * modified by different threads (writes to Properties are not thread-safe).
  63. */
  64. bool AddPropertiesStatic()
  65. {
  66. std::string eventName = "MyEvent";
  67. static Properties event = {{"uint32Key", (uint32_t)1234},
  68. {"uint64Key", (uint64_t)1234567890},
  69. {"strKey", "someValue"}};
  70. span_->AddEvent(eventName, event);
  71. return true;
  72. }
  73. /**
  74. * @brief Add event using initializer list
  75. */
  76. bool AddInitList()
  77. {
  78. std::string eventName = "MyEvent";
  79. span_->AddEvent(eventName, {{"uint32Key", (uint32_t)1234},
  80. {"uint64Key", (uint64_t)1234567890},
  81. {"strKey", "someValue"}});
  82. return true;
  83. }
  84. /**
  85. * @brief Add event using unordered_map
  86. */
  87. bool AddMap()
  88. {
  89. std::string eventName = "MyEvent";
  90. std::unordered_map<const char *, common::AttributeValue> m = {
  91. {"uint32Key", (uint32_t)1234},
  92. {"uint64Key", (uint64_t)1234567890},
  93. {"strKey", "someValue"}};
  94. span_->AddEvent(eventName, m);
  95. return true;
  96. }
  97. /**
  98. * @brief End Span and close tracer.
  99. */
  100. void Teardown()
  101. {
  102. span_->End();
  103. # if OPENTELEMETRY_ABI_VERSION_NO == 1
  104. tracer_->CloseWithMicroseconds(0);
  105. # endif
  106. }
  107. };
  108. ETWProviderStressTest provider;
  109. /**
  110. * @brief Create Properties and AddEvent(Properties) to Tracer
  111. * @param state Benchmark state.
  112. */
  113. void BM_AddPropertiesToTracer(benchmark::State &state)
  114. {
  115. provider.Initialize();
  116. while (state.KeepRunning())
  117. {
  118. benchmark::DoNotOptimize(provider.AddProperties());
  119. }
  120. provider.Teardown();
  121. }
  122. BENCHMARK(BM_AddPropertiesToTracer);
  123. /**
  124. * @brief Create static Properties and AddEvent(Properties) to Tracer
  125. * @param state Benchmark state.
  126. */
  127. void BM_AddPropertiesStaticToTracer(benchmark::State &state)
  128. {
  129. provider.Initialize();
  130. while (state.KeepRunning())
  131. {
  132. benchmark::DoNotOptimize(provider.AddPropertiesStatic());
  133. }
  134. provider.Teardown();
  135. }
  136. BENCHMARK(BM_AddPropertiesStaticToTracer);
  137. /**
  138. * @brief Create event via initializer list and AddEvent({...}) to Tracer
  139. * @param state Benchmark state.
  140. */
  141. void BM_AddInitListToTracer(benchmark::State &state)
  142. {
  143. provider.Initialize();
  144. while (state.KeepRunning())
  145. {
  146. benchmark::DoNotOptimize(provider.AddInitList());
  147. }
  148. provider.Teardown();
  149. }
  150. BENCHMARK(BM_AddInitListToTracer);
  151. /**
  152. * @brief Create event as `std::map<std::string, common::AttributeValue>`
  153. * and AddEvent(event) to Tracer.
  154. * @param state Benchmark state.
  155. */
  156. void BM_AddMapToTracer(benchmark::State &state)
  157. {
  158. provider.Initialize();
  159. while (state.KeepRunning())
  160. {
  161. benchmark::DoNotOptimize(provider.AddMap());
  162. }
  163. provider.Teardown();
  164. }
  165. BENCHMARK(BM_AddMapToTracer);
  166. } // namespace
  167. BENCHMARK_MAIN();
  168. #endif