test_module_profiler.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 <string>
  22. #include <utility>
  23. #include "profiler/module_profiler.hpp"
  24. namespace cnstream {
  25. TEST(CoreModuleProfiler, RegisterProcessName) {
  26. PipelineTracer tracer;
  27. ProfilerConfig config;
  28. config.enable_profiling = true;
  29. config.enable_tracing = true;
  30. const std::string module_name = "module";
  31. ModuleProfiler profiler(config, module_name, &tracer);
  32. const std::string process_name = "process";
  33. EXPECT_TRUE(profiler.RegisterProcessName(process_name));
  34. EXPECT_FALSE(profiler.RegisterProcessName(process_name));
  35. }
  36. TEST(CoreModuleProfiler, RecordProcessStart) {
  37. PipelineTracer tracer;
  38. ProfilerConfig config;
  39. config.enable_profiling = true;
  40. config.enable_tracing = true;
  41. const std::string module_name = "module";
  42. ModuleProfiler profiler(config, module_name, &tracer);
  43. const std::string process_name = "process";
  44. ASSERT_TRUE(profiler.RegisterProcessName(process_name));
  45. RecordKey key = std::make_pair("stream0", 0);
  46. profiler.RecordProcessStart(process_name, key);
  47. PipelineTrace trace = tracer.GetTrace(Time::min(), Time::max());
  48. EXPECT_EQ(trace.module_traces[module_name][process_name].size(), 1);
  49. }
  50. TEST(CoreModuleProfiler, RecordProcessEnd) {
  51. PipelineTracer tracer;
  52. ProfilerConfig config;
  53. config.enable_profiling = true;
  54. config.enable_tracing = true;
  55. const std::string module_name = "module";
  56. ModuleProfiler profiler(config, module_name, &tracer);
  57. const std::string process_name = "process";
  58. ASSERT_TRUE(profiler.RegisterProcessName(process_name));
  59. RecordKey key = std::make_pair("stream0", 0);
  60. profiler.RecordProcessEnd(process_name, key);
  61. PipelineTrace trace = tracer.GetTrace(Time::min(), Time::max());
  62. EXPECT_EQ(trace.module_traces[module_name][process_name].size(), 1);
  63. }
  64. TEST(CoreModuleProfiler, OnStreamEos) {
  65. PipelineTracer tracer;
  66. ProfilerConfig config;
  67. config.enable_profiling = true;
  68. config.enable_tracing = true;
  69. const std::string module_name = "module";
  70. ModuleProfiler profiler(config, module_name, &tracer);
  71. const std::string process_name = "process";
  72. ASSERT_TRUE(profiler.RegisterProcessName(process_name));
  73. const std::string stream_name = "stream0";
  74. RecordKey key = std::make_pair(stream_name, 0);
  75. profiler.RecordProcessStart(process_name, key);
  76. profiler.RecordProcessEnd(process_name, key);
  77. ModuleProfile profile = profiler.GetProfile();
  78. for (const auto& process_profile : profile.process_profiles)
  79. if (process_profile.process_name == process_name) {
  80. EXPECT_EQ(process_profile.stream_profiles.size(), 1);
  81. }
  82. profiler.OnStreamEos(stream_name);
  83. profile = profiler.GetProfile();
  84. for (const auto& process_profile : profile.process_profiles)
  85. if (process_profile.process_name == process_name) {
  86. EXPECT_EQ(process_profile.stream_profiles.size(), 0);
  87. }
  88. }
  89. TEST(CoreModuleProfiler, GetName) {
  90. PipelineTracer tracer;
  91. ProfilerConfig config;
  92. config.enable_profiling = true;
  93. config.enable_tracing = true;
  94. const std::string module_name = "module";
  95. ModuleProfiler profiler(config, module_name, &tracer);
  96. EXPECT_EQ(profiler.GetName(), module_name);
  97. }
  98. TEST(CoreModuleProfiler, GetProfile0) {
  99. PipelineTracer tracer;
  100. ProfilerConfig config;
  101. config.enable_profiling = true;
  102. config.enable_tracing = true;
  103. const std::string module_name = "module";
  104. ModuleProfiler profiler(config, module_name, &tracer);
  105. const std::string process_name = "process";
  106. ASSERT_TRUE(profiler.RegisterProcessName(process_name));
  107. const std::string stream_name = "stream0";
  108. RecordKey key = std::make_pair(stream_name, 0);
  109. profiler.RecordProcessStart(process_name, key);
  110. profiler.RecordProcessEnd(process_name, key);
  111. ModuleProfile profile = profiler.GetProfile();
  112. EXPECT_EQ(profile.process_profiles.size(), 1);
  113. }
  114. TEST(CoreModuleProfiler, GetProfile1) {
  115. PipelineTracer tracer;
  116. ProfilerConfig config;
  117. config.enable_profiling = true;
  118. config.enable_tracing = true;
  119. const std::string module_name = "module";
  120. ModuleProfiler profiler(config, module_name, &tracer);
  121. const std::string process_name = "process";
  122. ASSERT_TRUE(profiler.RegisterProcessName(process_name));
  123. const std::string stream_name = "stream0";
  124. RecordKey key1 = std::make_pair(stream_name, 0);
  125. RecordKey key2 = std::make_pair(stream_name, 1);
  126. ModuleTrace trace;
  127. ProcessTrace process_trace;
  128. TraceElem elem_start1;
  129. elem_start1.key = key1;
  130. elem_start1.time = Time(std::chrono::duration_cast<Time::duration>(std::chrono::duration<double, std::milli>(50)));
  131. elem_start1.type = TraceEvent::Type::START;
  132. TraceElem elem_start2;
  133. elem_start2.key = key2;
  134. elem_start2.time = Time(std::chrono::duration_cast<Time::duration>(std::chrono::duration<double, std::milli>(100)));
  135. elem_start2.type = TraceEvent::Type::START;
  136. TraceElem elem_end1;
  137. elem_end1.key = key1;
  138. elem_end1.time = Time(std::chrono::duration_cast<Time::duration>(std::chrono::duration<double, std::milli>(200)));
  139. elem_end1.type = TraceEvent::Type::END;
  140. TraceElem elem_end2;
  141. elem_end2.key = key2;
  142. elem_end2.time = Time(std::chrono::duration_cast<Time::duration>(std::chrono::duration<double, std::milli>(300)));
  143. elem_end2.type = TraceEvent::Type::END;
  144. process_trace.push_back(elem_start1);
  145. process_trace.push_back(elem_start2);
  146. process_trace.push_back(elem_end1);
  147. process_trace.push_back(elem_end2);
  148. trace[process_name] = process_trace;
  149. ModuleProfile profile = profiler.GetProfile(trace);
  150. ProcessProfile process_profile;
  151. for (const auto& it : profile.process_profiles)
  152. if (it.process_name == process_name)
  153. process_profile = it;
  154. EXPECT_EQ(process_profile.completed, 2);
  155. EXPECT_EQ(process_profile.fps, 1e3 / 250 * 2);
  156. EXPECT_EQ(process_profile.dropped, 0);
  157. EXPECT_EQ(process_profile.latency, 175);
  158. EXPECT_EQ(process_profile.minimum_latency, 150);
  159. EXPECT_EQ(process_profile.maximum_latency, 200);
  160. EXPECT_EQ(process_profile.ongoing, 0);
  161. EXPECT_EQ(process_profile.stream_profiles.size(), 1);
  162. EXPECT_EQ(process_profile.stream_profiles[0].stream_name, stream_name);
  163. EXPECT_EQ(process_profile.stream_profiles[0].completed, 2);
  164. EXPECT_EQ(process_profile.stream_profiles[0].dropped, 0);
  165. EXPECT_EQ(process_profile.stream_profiles[0].fps, 1e3 / 250 * 2);
  166. EXPECT_EQ(process_profile.stream_profiles[0].minimum_latency, 150);
  167. EXPECT_EQ(process_profile.stream_profiles[0].maximum_latency, 200);
  168. }
  169. } // namespace cnstream