test_pipeline_tracer.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/pipeline_tracer.hpp"
  24. namespace cnstream {
  25. TEST(CorePipelineTracer, Capacity) {
  26. size_t capacity = 100;
  27. PipelineTracer tracer(capacity);
  28. const std::string stream_name = "stream0";
  29. const std::string process_name = "process";
  30. RecordKey key = std::make_pair(stream_name, 0);
  31. TraceEvent event(key);
  32. event.SetKey(key).SetLevel(TraceEvent::Level::PIPELINE)
  33. .SetProcessName(process_name).SetTime(Clock::now())
  34. .SetType(TraceEvent::Type::START);
  35. for (size_t i = 0; i < capacity * 2; ++i)
  36. tracer.RecordEvent(event);
  37. PipelineTrace trace = tracer.GetTrace(Time::min(), Time::max());
  38. ASSERT_EQ(trace.process_traces.size(), 1);
  39. ASSERT_NE(trace.process_traces.find(process_name), trace.process_traces.end());
  40. EXPECT_EQ(trace.process_traces[process_name].size(), capacity);
  41. }
  42. TEST(CorePipelineTracer, RecordEvent) {
  43. PipelineTracer tracer;
  44. const std::string stream_name = "stream0";
  45. const std::string process_name = "process";
  46. RecordKey key = std::make_pair(stream_name, 0);
  47. TraceEvent event(key);
  48. event.SetKey(key).SetLevel(TraceEvent::Level::PIPELINE)
  49. .SetProcessName(process_name).SetTime(Clock::now())
  50. .SetType(TraceEvent::Type::START);
  51. tracer.RecordEvent(event);
  52. tracer.RecordEvent(std::move(event));
  53. PipelineTrace trace = tracer.GetTrace(Time::min(), Time::max());
  54. ASSERT_EQ(trace.process_traces.size(), 1);
  55. ASSERT_NE(trace.process_traces.find(process_name), trace.process_traces.end());
  56. EXPECT_EQ(trace.process_traces[process_name].size(), 2);
  57. }
  58. TEST(CorePipelineTracer, GetTrace) {
  59. PipelineTracer tracer;
  60. const std::string stream_name = "stream0";
  61. const std::string process_name = "process";
  62. RecordKey key = std::make_pair(stream_name, 0);
  63. TraceEvent event(key);
  64. auto start_time = Clock::now();
  65. event.SetKey(key).SetLevel(TraceEvent::Level::PIPELINE)
  66. .SetProcessName(process_name).SetTime(Clock::now())
  67. .SetType(TraceEvent::Type::START);
  68. auto event_tmp = event;
  69. tracer.RecordEvent(event);
  70. tracer.RecordEvent(std::move(event));
  71. PipelineTrace trace;
  72. trace = tracer.GetTrace(Time::min(), Time::max());
  73. ASSERT_EQ(trace.process_traces.size(), 1);
  74. ASSERT_NE(trace.process_traces.find(process_name), trace.process_traces.end());
  75. EXPECT_EQ(trace.process_traces[process_name].size(), 2);
  76. for (TraceElem it : trace.process_traces[process_name]) {
  77. EXPECT_EQ(it.key, event_tmp.key);
  78. EXPECT_EQ(it.time, event_tmp.time);
  79. EXPECT_EQ(it.type, event_tmp.type);
  80. }
  81. trace = tracer.GetTraceBefore(Clock::now(), Duration(1e10));
  82. ASSERT_EQ(trace.process_traces.size(), 1);
  83. ASSERT_NE(trace.process_traces.find(process_name), trace.process_traces.end());
  84. EXPECT_EQ(trace.process_traces[process_name].size(), 2);
  85. for (TraceElem it : trace.process_traces[process_name]) {
  86. EXPECT_EQ(it.key, event_tmp.key);
  87. EXPECT_EQ(it.time, event_tmp.time);
  88. EXPECT_EQ(it.type, event_tmp.type);
  89. }
  90. trace = tracer.GetTraceAfter(start_time, Duration(1e10));
  91. ASSERT_EQ(trace.process_traces.size(), 1);
  92. ASSERT_NE(trace.process_traces.find(process_name), trace.process_traces.end());
  93. EXPECT_EQ(trace.process_traces[process_name].size(), 2);
  94. for (TraceElem it : trace.process_traces[process_name]) {
  95. EXPECT_EQ(it.key, event_tmp.key);
  96. EXPECT_EQ(it.time, event_tmp.time);
  97. EXPECT_EQ(it.type, event_tmp.type);
  98. }
  99. }
  100. } // namespace cnstream