test_process_profiler.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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/process_profiler.hpp"
  24. namespace cnstream {
  25. TEST(CoreProcessProfiler, GetName) {
  26. PipelineTracer tracer;
  27. ProfilerConfig config;
  28. config.enable_profiling = true;
  29. config.enable_tracing = true;
  30. const std::string profiler_name = "profiler";
  31. ProcessProfiler profiler(config, profiler_name, &tracer);
  32. EXPECT_EQ(profiler_name, profiler.GetName());
  33. }
  34. TEST(CoreProcessProfiler, SetModuleName) {
  35. PipelineTracer tracer;
  36. ProfilerConfig config;
  37. config.enable_profiling = true;
  38. config.enable_tracing = true;
  39. const std::string profiler_name = "profiler";
  40. ProcessProfiler profiler(config, profiler_name, &tracer);
  41. const std::string module_name = "module";
  42. profiler.SetModuleName(module_name).SetTraceLevel(TraceEvent::Level::MODULE);
  43. profiler.RecordStart(std::make_pair("stream0", 100));
  44. PipelineTrace trace = tracer.GetTrace(Time::min(), Time::max());
  45. EXPECT_NE(trace.module_traces.find(module_name), trace.module_traces.end());
  46. EXPECT_EQ(trace.module_traces[module_name].size(), 1);
  47. EXPECT_NE(trace.module_traces[module_name].find(profiler_name), trace.module_traces[module_name].end());
  48. EXPECT_EQ(trace.module_traces[module_name][profiler_name].size(), 1);
  49. }
  50. TEST(CoreProcessProfiler, SetTraceLevel) {
  51. PipelineTracer tracer;
  52. ProfilerConfig config;
  53. config.enable_profiling = true;
  54. config.enable_tracing = true;
  55. const std::string profiler_name = "profiler";
  56. ProcessProfiler profiler(config, profiler_name, &tracer);
  57. const std::string module_name = "module";
  58. profiler.SetModuleName(module_name).SetTraceLevel(TraceEvent::Level::PIPELINE);
  59. profiler.RecordStart(std::make_pair("stream0", 100));
  60. PipelineTrace trace = tracer.GetTrace(Time::min(), Time::max());
  61. EXPECT_EQ(trace.module_traces.find(module_name), trace.module_traces.end());
  62. EXPECT_NE(trace.process_traces.find(profiler_name), trace.process_traces.end());
  63. EXPECT_EQ(trace.process_traces[profiler_name].size(), 1);
  64. }
  65. TEST(CoreProcessProfiler, RecordStartModule) {
  66. PipelineTracer tracer;
  67. ProfilerConfig config;
  68. config.enable_profiling = true;
  69. config.enable_tracing = true;
  70. const std::string profiler_name = "profiler";
  71. ProcessProfiler profiler(config, profiler_name, &tracer);
  72. const std::string module_name = "module";
  73. profiler.SetModuleName(module_name).SetTraceLevel(TraceEvent::Level::MODULE);
  74. RecordKey key = std::make_pair("stream0", 100);
  75. profiler.RecordStart(key);
  76. PipelineTrace trace = tracer.GetTrace(Time::min(), Time::max());
  77. EXPECT_NE(trace.module_traces.find(module_name), trace.module_traces.end());
  78. EXPECT_EQ(trace.module_traces[module_name].size(), 1);
  79. EXPECT_NE(trace.module_traces[module_name].find(profiler_name), trace.module_traces[module_name].end());
  80. EXPECT_EQ(trace.module_traces[module_name][profiler_name].size(), 1);
  81. TraceElem elem = trace.module_traces[module_name][profiler_name][0];
  82. EXPECT_EQ(elem.type, TraceEvent::Type::START);
  83. EXPECT_EQ(elem.key, key);
  84. }
  85. TEST(CoreProcessProfiler, RecordEndModule) {
  86. PipelineTracer tracer;
  87. ProfilerConfig config;
  88. config.enable_profiling = true;
  89. config.enable_tracing = true;
  90. const std::string profiler_name = "profiler";
  91. ProcessProfiler profiler(config, profiler_name, &tracer);
  92. const std::string module_name = "module";
  93. profiler.SetModuleName(module_name).SetTraceLevel(TraceEvent::Level::MODULE);
  94. RecordKey key = std::make_pair("stream0", 100);
  95. profiler.RecordEnd(key);
  96. PipelineTrace trace = tracer.GetTrace(Time::min(), Time::max());
  97. ASSERT_NE(trace.module_traces.find(module_name), trace.module_traces.end());
  98. ASSERT_EQ(trace.module_traces[module_name].size(), 1);
  99. ASSERT_NE(trace.module_traces[module_name].find(profiler_name), trace.module_traces[module_name].end());
  100. ASSERT_EQ(trace.module_traces[module_name][profiler_name].size(), 1);
  101. TraceElem elem = trace.module_traces[module_name][profiler_name][0];
  102. EXPECT_EQ(elem.type, TraceEvent::Type::END);
  103. EXPECT_EQ(elem.key, key);
  104. }
  105. TEST(CoreProcessProfiler, RecordStartPipeline) {
  106. PipelineTracer tracer;
  107. ProfilerConfig config;
  108. config.enable_profiling = true;
  109. config.enable_tracing = true;
  110. const std::string profiler_name = "profiler";
  111. ProcessProfiler profiler(config, profiler_name, &tracer);
  112. profiler.SetTraceLevel(TraceEvent::Level::PIPELINE);
  113. RecordKey key = std::make_pair("stream0", 100);
  114. profiler.RecordStart(key);
  115. PipelineTrace trace = tracer.GetTrace(Time::min(), Time::max());
  116. ASSERT_NE(trace.process_traces.find(profiler_name), trace.process_traces.end());
  117. ASSERT_EQ(trace.process_traces[profiler_name].size(), 1);
  118. TraceElem elem = trace.process_traces[profiler_name][0];
  119. EXPECT_EQ(elem.type, TraceEvent::Type::START);
  120. EXPECT_EQ(elem.key, key);
  121. }
  122. TEST(CoreProcessProfiler, RecordEndPipeline) {
  123. PipelineTracer tracer;
  124. ProfilerConfig config;
  125. config.enable_profiling = true;
  126. config.enable_tracing = true;
  127. const std::string profiler_name = "profiler";
  128. ProcessProfiler profiler(config, profiler_name, &tracer);
  129. profiler.SetTraceLevel(TraceEvent::Level::PIPELINE);
  130. RecordKey key = std::make_pair("stream0", 100);
  131. profiler.RecordEnd(key);
  132. PipelineTrace trace = tracer.GetTrace(Time::min(), Time::max());
  133. ASSERT_NE(trace.process_traces.find(profiler_name), trace.process_traces.end());
  134. ASSERT_EQ(trace.process_traces[profiler_name].size(), 1);
  135. TraceElem elem = trace.process_traces[profiler_name][0];
  136. EXPECT_EQ(elem.type, TraceEvent::Type::END);
  137. EXPECT_EQ(elem.key, key);
  138. }
  139. TEST(CoreProcessProfiler, RecordEndRecordEnd) {
  140. PipelineTracer tracer;
  141. ProfilerConfig config;
  142. config.enable_profiling = true;
  143. config.enable_tracing = true;
  144. const std::string profiler_name = "profiler";
  145. ProcessProfiler profiler(config, profiler_name, &tracer);
  146. profiler.SetTraceLevel(TraceEvent::Level::PIPELINE);
  147. const std::string stream_name = "stream0";
  148. RecordKey key = std::make_pair("stream0", 100);
  149. profiler.RecordEnd(key);
  150. profiler.RecordEnd(key);
  151. ProcessProfile profile = profiler.GetProfile();
  152. EXPECT_EQ(profile.completed, 2);
  153. }
  154. TEST(CoreProcessProfiler, DropData) {
  155. static constexpr uint64_t kDEFAULT_MAX_DPB_SIZE = 16;
  156. PipelineTracer tracer;
  157. ProfilerConfig config;
  158. config.enable_profiling = true;
  159. config.enable_tracing = true;
  160. const std::string profiler_name = "profiler";
  161. ProcessProfiler profiler(config, profiler_name, &tracer);
  162. profiler.SetTraceLevel(TraceEvent::Level::PIPELINE);
  163. const std::string stream_name = "stream0";
  164. uint64_t dropped = 2;
  165. uint64_t ts = 0;
  166. for (; ts < dropped; ++ts) {
  167. RecordKey key = std::make_pair(stream_name, ts);
  168. profiler.RecordStart(key);
  169. }
  170. for (; ts < kDEFAULT_MAX_DPB_SIZE + dropped + 1; ++ts) {
  171. RecordKey key = std::make_pair(stream_name, ts);
  172. profiler.RecordStart(key);
  173. profiler.RecordEnd(key);
  174. }
  175. ProcessProfile profile = profiler.GetProfile();
  176. EXPECT_EQ(profile.dropped, dropped);
  177. EXPECT_EQ(profile.completed, kDEFAULT_MAX_DPB_SIZE + 1);
  178. EXPECT_EQ(profile.ongoing, 0);
  179. EXPECT_EQ(profile.stream_profiles.size(), 1);
  180. EXPECT_EQ(profile.stream_profiles[0].stream_name, stream_name);
  181. EXPECT_EQ(profile.stream_profiles[0].dropped, dropped);
  182. EXPECT_EQ(profile.stream_profiles[0].completed, kDEFAULT_MAX_DPB_SIZE + 1);
  183. }
  184. TEST(CoreProcessProfiler, GetProfile0) {
  185. PipelineTracer tracer;
  186. ProfilerConfig config;
  187. config.enable_profiling = true;
  188. config.enable_tracing = true;
  189. const std::string profiler_name = "profiler";
  190. ProcessProfiler profiler(config, profiler_name, &tracer);
  191. profiler.SetTraceLevel(TraceEvent::Level::PIPELINE);
  192. const std::string stream_name = "stream0";
  193. RecordKey key = std::make_pair("stream0", 100);
  194. profiler.RecordEnd(key);
  195. ProcessProfile profile = profiler.GetProfile();
  196. EXPECT_EQ(profile.dropped, 0);
  197. EXPECT_EQ(profile.completed, 1);
  198. EXPECT_EQ(profile.ongoing, 0);
  199. EXPECT_EQ(profile.stream_profiles.size(), 1);
  200. EXPECT_EQ(profile.stream_profiles[0].completed, 1);
  201. EXPECT_EQ(profile.stream_profiles[0].dropped, 0);
  202. }
  203. TEST(CoreProcessProfiler, GetProfile1) {
  204. PipelineTracer tracer;
  205. ProfilerConfig config;
  206. config.enable_profiling = true;
  207. config.enable_tracing = true;
  208. const std::string profiler_name = "profiler";
  209. ProcessProfiler profiler(config, profiler_name, &tracer);
  210. profiler.SetTraceLevel(TraceEvent::Level::PIPELINE);
  211. const std::string stream_name = "stream0";
  212. RecordKey key1 = std::make_pair(stream_name, 100);
  213. RecordKey key2 = std::make_pair(stream_name, 200);
  214. ProcessTrace trace;
  215. TraceElem elem_start1;
  216. elem_start1.key = key1;
  217. elem_start1.time = Time(std::chrono::duration_cast<Time::duration>(std::chrono::duration<double, std::milli>(50)));
  218. elem_start1.type = TraceEvent::Type::START;
  219. TraceElem elem_start2;
  220. elem_start2.key = key2;
  221. elem_start2.time = Time(std::chrono::duration_cast<Time::duration>(std::chrono::duration<double, std::milli>(100)));
  222. elem_start2.type = TraceEvent::Type::START;
  223. TraceElem elem_end1;
  224. elem_end1.key = key1;
  225. elem_end1.time = Time(std::chrono::duration_cast<Time::duration>(std::chrono::duration<double, std::milli>(200)));
  226. elem_end1.type = TraceEvent::Type::END;
  227. TraceElem elem_end2;
  228. elem_end2.key = key2;
  229. elem_end2.time = Time(std::chrono::duration_cast<Time::duration>(std::chrono::duration<double, std::milli>(300)));
  230. elem_end2.type = TraceEvent::Type::END;
  231. trace.push_back(elem_start1);
  232. trace.push_back(elem_start2);
  233. trace.push_back(elem_end1);
  234. trace.push_back(elem_end2);
  235. ProcessProfile profile = profiler.GetProfile(trace);
  236. EXPECT_EQ(profile.completed, 2);
  237. EXPECT_EQ(profile.fps, 1e3 / 250 * 2);
  238. EXPECT_EQ(profile.dropped, 0);
  239. EXPECT_EQ(profile.latency, 175);
  240. EXPECT_EQ(profile.minimum_latency, 150);
  241. EXPECT_EQ(profile.maximum_latency, 200);
  242. EXPECT_EQ(profile.ongoing, 0);
  243. EXPECT_EQ(profile.process_name, profiler_name);
  244. EXPECT_EQ(profile.stream_profiles.size(), 1);
  245. EXPECT_EQ(profile.stream_profiles[0].stream_name, stream_name);
  246. EXPECT_EQ(profile.stream_profiles[0].completed, 2);
  247. EXPECT_EQ(profile.stream_profiles[0].dropped, 0);
  248. EXPECT_EQ(profile.stream_profiles[0].fps, 1e3 / 250 * 2);
  249. EXPECT_EQ(profile.stream_profiles[0].minimum_latency, 150);
  250. EXPECT_EQ(profile.stream_profiles[0].maximum_latency, 200);
  251. }
  252. TEST(CoreProcessProfiler, OnStreamEos) {
  253. PipelineTracer tracer;
  254. ProfilerConfig config;
  255. config.enable_profiling = true;
  256. config.enable_tracing = true;
  257. const std::string profiler_name = "profiler";
  258. ProcessProfiler profiler(config, profiler_name, &tracer);
  259. profiler.SetTraceLevel(TraceEvent::Level::PIPELINE);
  260. const std::string stream_name = "stream0";
  261. RecordKey key = std::make_pair(stream_name, 100);
  262. profiler.RecordStart(key);
  263. profiler.RecordEnd(key);
  264. EXPECT_EQ(profiler.GetProfile().stream_profiles.size(), 1);
  265. profiler.OnStreamEos(stream_name);
  266. EXPECT_EQ(profiler.GetProfile().stream_profiles.size(), 0);
  267. }
  268. TEST(CoreProcessProfiler, OnStreamEosBorderCase) {
  269. PipelineTracer tracer;
  270. ProfilerConfig config;
  271. config.enable_profiling = true;
  272. config.enable_tracing = true;
  273. const std::string profiler_name = "profiler";
  274. ProcessProfiler profiler(config, profiler_name, &tracer);
  275. profiler.SetTraceLevel(TraceEvent::Level::PIPELINE);
  276. const std::string stream_name = "stream0";
  277. EXPECT_EQ(profiler.GetProfile().stream_profiles.size(), 0);
  278. profiler.OnStreamEos(stream_name);
  279. EXPECT_EQ(profiler.GetProfile().stream_profiles.size(), 0);
  280. }
  281. TEST(CoreProcessProfiler, NullTracer) {
  282. ProfilerConfig config;
  283. config.enable_profiling = true;
  284. config.enable_tracing = true;
  285. const std::string profiler_name = "profiler";
  286. ProcessProfiler profiler(config, profiler_name, nullptr);
  287. RecordKey key = std::make_pair("stream0", 100);
  288. profiler.RecordStart(key);
  289. }
  290. } // namespace cnstream