test_pipeline_profiler.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*************************************************************************
  2. * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #include <gtest/gtest.h>
  21. #include <memory>
  22. #include <string>
  23. #include <utility>
  24. #include <vector>
  25. #include "cnstream_module.hpp"
  26. #include "cnstream_pipeline.hpp"
  27. #include "profiler/module_profiler.hpp"
  28. #include "profiler/pipeline_profiler.hpp"
  29. namespace cnstream {
  30. class TestModule : public Module {
  31. public:
  32. explicit TestModule(const std::string& name) : Module(name) {}
  33. int Process(std::shared_ptr<CNFrameInfo>) override { return 0; }
  34. bool Open(ModuleParamSet params) override { return true; }
  35. void Close() override { return; }
  36. }; // class TestModule
  37. static std::vector<std::shared_ptr<Module>>
  38. CreateModules() {
  39. std::vector<std::shared_ptr<Module>> modules = {
  40. std::make_shared<TestModule>("module1"),
  41. std::make_shared<TestModule>("module2")
  42. };
  43. return modules;
  44. }
  45. TEST(CorePipelineProfiler, GetName) {
  46. ProfilerConfig config;
  47. config.enable_tracing = true;
  48. config.enable_profiling = true;
  49. const std::string pipeline_name = "pipeline";
  50. PipelineProfiler profiler(config, pipeline_name, {});
  51. EXPECT_EQ(profiler.GetName(), pipeline_name);
  52. }
  53. TEST(CorePipelineProfiler, GetTracer) {
  54. ProfilerConfig config;
  55. config.enable_tracing = true;
  56. config.enable_profiling = true;
  57. const std::string pipeline_name = "pipeline";
  58. PipelineProfiler profiler(config, pipeline_name, {});
  59. EXPECT_NE(nullptr, profiler.GetTracer());
  60. }
  61. TEST(CorePipelineProfiler, GetModuleProfiler) {
  62. ProfilerConfig config;
  63. config.enable_tracing = true;
  64. config.enable_profiling = true;
  65. const std::string pipeline_name = "pipeline";
  66. const std::string module_name = "module";
  67. std::vector<std::shared_ptr<Module>> modules;
  68. modules.push_back(std::shared_ptr<Module>(new TestModule(module_name)));
  69. PipelineProfiler profiler(config, pipeline_name, modules);
  70. EXPECT_NE(nullptr, profiler.GetModuleProfiler(module_name));
  71. }
  72. TEST(CorePipelineProfiler, GetProfile) {
  73. ProfilerConfig config;
  74. config.enable_tracing = true;
  75. config.enable_profiling = true;
  76. auto modules = CreateModules();
  77. PipelineProfiler profiler(config, "test_pipeline", modules);
  78. const std::string stream_name = "stream0";
  79. Time start_time = Clock::now();
  80. profiler.RecordInput(std::make_pair(stream_name, 0));
  81. profiler.RecordInput(std::make_pair(stream_name, 1));
  82. profiler.RecordOutput(std::make_pair(stream_name, 0));
  83. profiler.RecordOutput(std::make_pair(stream_name, 1));
  84. profiler.GetModuleProfiler(modules[0]->GetName())
  85. ->RecordProcessStart(kPROCESS_PROFILER_NAME, std::make_pair(stream_name, 0));
  86. profiler.GetModuleProfiler(modules[0]->GetName())
  87. ->RecordProcessEnd(kPROCESS_PROFILER_NAME, std::make_pair(stream_name, 0));
  88. PipelineProfile profile = profiler.GetProfile();
  89. EXPECT_EQ(profile.pipeline_name, "test_pipeline");
  90. EXPECT_EQ(profile.overall_profile.completed, 2);
  91. profile = profiler.GetProfile(Time::min(), Time::max());
  92. EXPECT_EQ(profile.overall_profile.completed, 2);
  93. profile = profiler.GetProfileBefore(Clock::now(), Duration(1e10));
  94. EXPECT_EQ(profile.overall_profile.completed, 2);
  95. profile = profiler.GetProfileAfter(start_time, Duration(1e10));
  96. EXPECT_EQ(profile.overall_profile.completed, 2);
  97. }
  98. TEST(CorePipelineProfiler, GetProfile_Disable_Tracing) {
  99. ProfilerConfig config;
  100. config.enable_tracing = false;
  101. config.enable_profiling = true;
  102. auto modules = CreateModules();
  103. PipelineProfiler profiler(config, "test_pipeline", modules);
  104. const std::string stream_name = "stream0";
  105. profiler.RecordInput(std::make_pair(stream_name, 0));
  106. profiler.RecordInput(std::make_pair(stream_name, 1));
  107. profiler.RecordOutput(std::make_pair(stream_name, 0));
  108. profiler.RecordOutput(std::make_pair(stream_name, 1));
  109. PipelineProfile profile = profiler.GetProfile(Time::min(), Time::max());
  110. EXPECT_EQ(profile.overall_profile.completed, 0);
  111. }
  112. TEST(CorePipelineProfiler, RecordInputOutput) {
  113. ProfilerConfig config;
  114. config.enable_tracing = true;
  115. config.enable_profiling = true;
  116. auto modules = CreateModules();
  117. PipelineProfiler profiler(config, "test_pipeline", modules);
  118. const std::string stream_name = "stream0";
  119. Time start_time = Clock::now();
  120. profiler.RecordInput(std::make_pair(stream_name, 0));
  121. profiler.RecordOutput(std::make_pair(stream_name, 0));
  122. PipelineTrace trace = profiler.GetTracer()->GetTrace(start_time, Clock::now());
  123. for (const auto& process_trace : trace.process_traces)
  124. if (process_trace.first == kOVERALL_PROCESS_NAME) {
  125. EXPECT_EQ(process_trace.second.size(), 2);
  126. EXPECT_EQ(process_trace.second[0].type, TraceEvent::Type::START);
  127. EXPECT_EQ(process_trace.second[1].type, TraceEvent::Type::END);
  128. }
  129. }
  130. TEST(CorePipelineProfiler, OnStreamEos) {
  131. ProfilerConfig config;
  132. config.enable_tracing = true;
  133. config.enable_profiling = true;
  134. auto modules = CreateModules();
  135. PipelineProfiler profiler(config, "test_pipeline", modules);
  136. const std::string stream_name = "stream0";
  137. profiler.RecordInput(std::make_pair(stream_name, 0));
  138. profiler.RecordOutput(std::make_pair(stream_name, 0));
  139. PipelineProfile profile = profiler.GetProfile();
  140. EXPECT_EQ(profile.overall_profile.stream_profiles.size(), 1);
  141. profiler.OnStreamEos(stream_name);
  142. profile = profiler.GetProfile();
  143. EXPECT_EQ(profile.overall_profile.stream_profiles.size(), 0);
  144. }
  145. } // namespace cnstream