123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- /*************************************************************************
- * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *************************************************************************/
- #include <gtest/gtest.h>
- #include <string>
- #include <utility>
- #include "profiler/process_profiler.hpp"
- namespace cnstream {
- TEST(CoreProcessProfiler, GetName) {
- PipelineTracer tracer;
- ProfilerConfig config;
- config.enable_profiling = true;
- config.enable_tracing = true;
- const std::string profiler_name = "profiler";
- ProcessProfiler profiler(config, profiler_name, &tracer);
- EXPECT_EQ(profiler_name, profiler.GetName());
- }
- TEST(CoreProcessProfiler, SetModuleName) {
- PipelineTracer tracer;
- ProfilerConfig config;
- config.enable_profiling = true;
- config.enable_tracing = true;
- const std::string profiler_name = "profiler";
- ProcessProfiler profiler(config, profiler_name, &tracer);
- const std::string module_name = "module";
- profiler.SetModuleName(module_name).SetTraceLevel(TraceEvent::Level::MODULE);
- profiler.RecordStart(std::make_pair("stream0", 100));
- PipelineTrace trace = tracer.GetTrace(Time::min(), Time::max());
- EXPECT_NE(trace.module_traces.find(module_name), trace.module_traces.end());
- EXPECT_EQ(trace.module_traces[module_name].size(), 1);
- EXPECT_NE(trace.module_traces[module_name].find(profiler_name), trace.module_traces[module_name].end());
- EXPECT_EQ(trace.module_traces[module_name][profiler_name].size(), 1);
- }
- TEST(CoreProcessProfiler, SetTraceLevel) {
- PipelineTracer tracer;
- ProfilerConfig config;
- config.enable_profiling = true;
- config.enable_tracing = true;
- const std::string profiler_name = "profiler";
- ProcessProfiler profiler(config, profiler_name, &tracer);
- const std::string module_name = "module";
- profiler.SetModuleName(module_name).SetTraceLevel(TraceEvent::Level::PIPELINE);
- profiler.RecordStart(std::make_pair("stream0", 100));
- PipelineTrace trace = tracer.GetTrace(Time::min(), Time::max());
- EXPECT_EQ(trace.module_traces.find(module_name), trace.module_traces.end());
- EXPECT_NE(trace.process_traces.find(profiler_name), trace.process_traces.end());
- EXPECT_EQ(trace.process_traces[profiler_name].size(), 1);
- }
- TEST(CoreProcessProfiler, RecordStartModule) {
- PipelineTracer tracer;
- ProfilerConfig config;
- config.enable_profiling = true;
- config.enable_tracing = true;
- const std::string profiler_name = "profiler";
- ProcessProfiler profiler(config, profiler_name, &tracer);
- const std::string module_name = "module";
- profiler.SetModuleName(module_name).SetTraceLevel(TraceEvent::Level::MODULE);
- RecordKey key = std::make_pair("stream0", 100);
- profiler.RecordStart(key);
- PipelineTrace trace = tracer.GetTrace(Time::min(), Time::max());
- EXPECT_NE(trace.module_traces.find(module_name), trace.module_traces.end());
- EXPECT_EQ(trace.module_traces[module_name].size(), 1);
- EXPECT_NE(trace.module_traces[module_name].find(profiler_name), trace.module_traces[module_name].end());
- EXPECT_EQ(trace.module_traces[module_name][profiler_name].size(), 1);
- TraceElem elem = trace.module_traces[module_name][profiler_name][0];
- EXPECT_EQ(elem.type, TraceEvent::Type::START);
- EXPECT_EQ(elem.key, key);
- }
- TEST(CoreProcessProfiler, RecordEndModule) {
- PipelineTracer tracer;
- ProfilerConfig config;
- config.enable_profiling = true;
- config.enable_tracing = true;
- const std::string profiler_name = "profiler";
- ProcessProfiler profiler(config, profiler_name, &tracer);
- const std::string module_name = "module";
- profiler.SetModuleName(module_name).SetTraceLevel(TraceEvent::Level::MODULE);
- RecordKey key = std::make_pair("stream0", 100);
- profiler.RecordEnd(key);
- PipelineTrace trace = tracer.GetTrace(Time::min(), Time::max());
- ASSERT_NE(trace.module_traces.find(module_name), trace.module_traces.end());
- ASSERT_EQ(trace.module_traces[module_name].size(), 1);
- ASSERT_NE(trace.module_traces[module_name].find(profiler_name), trace.module_traces[module_name].end());
- ASSERT_EQ(trace.module_traces[module_name][profiler_name].size(), 1);
- TraceElem elem = trace.module_traces[module_name][profiler_name][0];
- EXPECT_EQ(elem.type, TraceEvent::Type::END);
- EXPECT_EQ(elem.key, key);
- }
- TEST(CoreProcessProfiler, RecordStartPipeline) {
- PipelineTracer tracer;
- ProfilerConfig config;
- config.enable_profiling = true;
- config.enable_tracing = true;
- const std::string profiler_name = "profiler";
- ProcessProfiler profiler(config, profiler_name, &tracer);
- profiler.SetTraceLevel(TraceEvent::Level::PIPELINE);
- RecordKey key = std::make_pair("stream0", 100);
- profiler.RecordStart(key);
- PipelineTrace trace = tracer.GetTrace(Time::min(), Time::max());
- ASSERT_NE(trace.process_traces.find(profiler_name), trace.process_traces.end());
- ASSERT_EQ(trace.process_traces[profiler_name].size(), 1);
- TraceElem elem = trace.process_traces[profiler_name][0];
- EXPECT_EQ(elem.type, TraceEvent::Type::START);
- EXPECT_EQ(elem.key, key);
- }
- TEST(CoreProcessProfiler, RecordEndPipeline) {
- PipelineTracer tracer;
- ProfilerConfig config;
- config.enable_profiling = true;
- config.enable_tracing = true;
- const std::string profiler_name = "profiler";
- ProcessProfiler profiler(config, profiler_name, &tracer);
- profiler.SetTraceLevel(TraceEvent::Level::PIPELINE);
- RecordKey key = std::make_pair("stream0", 100);
- profiler.RecordEnd(key);
- PipelineTrace trace = tracer.GetTrace(Time::min(), Time::max());
- ASSERT_NE(trace.process_traces.find(profiler_name), trace.process_traces.end());
- ASSERT_EQ(trace.process_traces[profiler_name].size(), 1);
- TraceElem elem = trace.process_traces[profiler_name][0];
- EXPECT_EQ(elem.type, TraceEvent::Type::END);
- EXPECT_EQ(elem.key, key);
- }
- TEST(CoreProcessProfiler, RecordEndRecordEnd) {
- PipelineTracer tracer;
- ProfilerConfig config;
- config.enable_profiling = true;
- config.enable_tracing = true;
- const std::string profiler_name = "profiler";
- ProcessProfiler profiler(config, profiler_name, &tracer);
- profiler.SetTraceLevel(TraceEvent::Level::PIPELINE);
- const std::string stream_name = "stream0";
- RecordKey key = std::make_pair("stream0", 100);
- profiler.RecordEnd(key);
- profiler.RecordEnd(key);
- ProcessProfile profile = profiler.GetProfile();
- EXPECT_EQ(profile.completed, 2);
- }
- TEST(CoreProcessProfiler, DropData) {
- static constexpr uint64_t kDEFAULT_MAX_DPB_SIZE = 16;
- PipelineTracer tracer;
- ProfilerConfig config;
- config.enable_profiling = true;
- config.enable_tracing = true;
- const std::string profiler_name = "profiler";
- ProcessProfiler profiler(config, profiler_name, &tracer);
- profiler.SetTraceLevel(TraceEvent::Level::PIPELINE);
- const std::string stream_name = "stream0";
- uint64_t dropped = 2;
- uint64_t ts = 0;
- for (; ts < dropped; ++ts) {
- RecordKey key = std::make_pair(stream_name, ts);
- profiler.RecordStart(key);
- }
- for (; ts < kDEFAULT_MAX_DPB_SIZE + dropped + 1; ++ts) {
- RecordKey key = std::make_pair(stream_name, ts);
- profiler.RecordStart(key);
- profiler.RecordEnd(key);
- }
- ProcessProfile profile = profiler.GetProfile();
- EXPECT_EQ(profile.dropped, dropped);
- EXPECT_EQ(profile.completed, kDEFAULT_MAX_DPB_SIZE + 1);
- EXPECT_EQ(profile.ongoing, 0);
- EXPECT_EQ(profile.stream_profiles.size(), 1);
- EXPECT_EQ(profile.stream_profiles[0].stream_name, stream_name);
- EXPECT_EQ(profile.stream_profiles[0].dropped, dropped);
- EXPECT_EQ(profile.stream_profiles[0].completed, kDEFAULT_MAX_DPB_SIZE + 1);
- }
- TEST(CoreProcessProfiler, GetProfile0) {
- PipelineTracer tracer;
- ProfilerConfig config;
- config.enable_profiling = true;
- config.enable_tracing = true;
- const std::string profiler_name = "profiler";
- ProcessProfiler profiler(config, profiler_name, &tracer);
- profiler.SetTraceLevel(TraceEvent::Level::PIPELINE);
- const std::string stream_name = "stream0";
- RecordKey key = std::make_pair("stream0", 100);
- profiler.RecordEnd(key);
- ProcessProfile profile = profiler.GetProfile();
- EXPECT_EQ(profile.dropped, 0);
- EXPECT_EQ(profile.completed, 1);
- EXPECT_EQ(profile.ongoing, 0);
- EXPECT_EQ(profile.stream_profiles.size(), 1);
- EXPECT_EQ(profile.stream_profiles[0].completed, 1);
- EXPECT_EQ(profile.stream_profiles[0].dropped, 0);
- }
- TEST(CoreProcessProfiler, GetProfile1) {
- PipelineTracer tracer;
- ProfilerConfig config;
- config.enable_profiling = true;
- config.enable_tracing = true;
- const std::string profiler_name = "profiler";
- ProcessProfiler profiler(config, profiler_name, &tracer);
- profiler.SetTraceLevel(TraceEvent::Level::PIPELINE);
- const std::string stream_name = "stream0";
- RecordKey key1 = std::make_pair(stream_name, 100);
- RecordKey key2 = std::make_pair(stream_name, 200);
- ProcessTrace trace;
- TraceElem elem_start1;
- elem_start1.key = key1;
- elem_start1.time = Time(std::chrono::duration_cast<Time::duration>(std::chrono::duration<double, std::milli>(50)));
- elem_start1.type = TraceEvent::Type::START;
- TraceElem elem_start2;
- elem_start2.key = key2;
- elem_start2.time = Time(std::chrono::duration_cast<Time::duration>(std::chrono::duration<double, std::milli>(100)));
- elem_start2.type = TraceEvent::Type::START;
- TraceElem elem_end1;
- elem_end1.key = key1;
- elem_end1.time = Time(std::chrono::duration_cast<Time::duration>(std::chrono::duration<double, std::milli>(200)));
- elem_end1.type = TraceEvent::Type::END;
- TraceElem elem_end2;
- elem_end2.key = key2;
- elem_end2.time = Time(std::chrono::duration_cast<Time::duration>(std::chrono::duration<double, std::milli>(300)));
- elem_end2.type = TraceEvent::Type::END;
- trace.push_back(elem_start1);
- trace.push_back(elem_start2);
- trace.push_back(elem_end1);
- trace.push_back(elem_end2);
- ProcessProfile profile = profiler.GetProfile(trace);
- EXPECT_EQ(profile.completed, 2);
- EXPECT_EQ(profile.fps, 1e3 / 250 * 2);
- EXPECT_EQ(profile.dropped, 0);
- EXPECT_EQ(profile.latency, 175);
- EXPECT_EQ(profile.minimum_latency, 150);
- EXPECT_EQ(profile.maximum_latency, 200);
- EXPECT_EQ(profile.ongoing, 0);
- EXPECT_EQ(profile.process_name, profiler_name);
- EXPECT_EQ(profile.stream_profiles.size(), 1);
- EXPECT_EQ(profile.stream_profiles[0].stream_name, stream_name);
- EXPECT_EQ(profile.stream_profiles[0].completed, 2);
- EXPECT_EQ(profile.stream_profiles[0].dropped, 0);
- EXPECT_EQ(profile.stream_profiles[0].fps, 1e3 / 250 * 2);
- EXPECT_EQ(profile.stream_profiles[0].minimum_latency, 150);
- EXPECT_EQ(profile.stream_profiles[0].maximum_latency, 200);
- }
- TEST(CoreProcessProfiler, OnStreamEos) {
- PipelineTracer tracer;
- ProfilerConfig config;
- config.enable_profiling = true;
- config.enable_tracing = true;
- const std::string profiler_name = "profiler";
- ProcessProfiler profiler(config, profiler_name, &tracer);
- profiler.SetTraceLevel(TraceEvent::Level::PIPELINE);
- const std::string stream_name = "stream0";
- RecordKey key = std::make_pair(stream_name, 100);
- profiler.RecordStart(key);
- profiler.RecordEnd(key);
- EXPECT_EQ(profiler.GetProfile().stream_profiles.size(), 1);
- profiler.OnStreamEos(stream_name);
- EXPECT_EQ(profiler.GetProfile().stream_profiles.size(), 0);
- }
- TEST(CoreProcessProfiler, OnStreamEosBorderCase) {
- PipelineTracer tracer;
- ProfilerConfig config;
- config.enable_profiling = true;
- config.enable_tracing = true;
- const std::string profiler_name = "profiler";
- ProcessProfiler profiler(config, profiler_name, &tracer);
- profiler.SetTraceLevel(TraceEvent::Level::PIPELINE);
- const std::string stream_name = "stream0";
- EXPECT_EQ(profiler.GetProfile().stream_profiles.size(), 0);
- profiler.OnStreamEos(stream_name);
- EXPECT_EQ(profiler.GetProfile().stream_profiles.size(), 0);
- }
- TEST(CoreProcessProfiler, NullTracer) {
- ProfilerConfig config;
- config.enable_profiling = true;
- config.enable_tracing = true;
- const std::string profiler_name = "profiler";
- ProcessProfiler profiler(config, profiler_name, nullptr);
- RecordKey key = std::make_pair("stream0", 100);
- profiler.RecordStart(key);
- }
- } // namespace cnstream
|