test_stream_profiler.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "profiler/stream_profiler.hpp"
  23. namespace cnstream {
  24. TEST(CoreStreamProfiler, GetName) {
  25. const std::string name = "profiler";
  26. StreamProfiler profiler(name);
  27. EXPECT_EQ(name, profiler.GetName());
  28. EXPECT_EQ(name, profiler.GetProfile().stream_name);
  29. }
  30. TEST(CoreStreamProfiler, AddLatency) {
  31. StreamProfiler profiler("profiler");
  32. profiler.AddLatency(std::chrono::duration<double, std::milli>(2.0))
  33. .AddLatency(std::chrono::duration<double, std::milli>(3.0));
  34. StreamProfile profile = profiler.GetProfile();
  35. EXPECT_EQ(profiler.GetProfile().latency, (2.0 + 3.0) / 2);
  36. }
  37. TEST(CoreStreamProfiler, UpdatePhysicalTime) {
  38. StreamProfiler profiler("profiler");
  39. profiler.UpdatePhysicalTime(std::chrono::duration<double, std::milli>(1.0)).AddCompleted()
  40. .UpdatePhysicalTime(std::chrono::duration<double, std::milli>(3.0)).AddCompleted();
  41. EXPECT_EQ(profiler.GetProfile().fps, 1e3 / (3.0) * 2);
  42. }
  43. TEST(CoreStreamProfiler, AddDropped) {
  44. StreamProfiler profiler("profiler");
  45. profiler.AddDropped(2).AddDropped(20);
  46. EXPECT_EQ(profiler.GetProfile().dropped, 2 + 20);
  47. }
  48. TEST(CoreStreamProfiler, AddCompleted) {
  49. StreamProfiler profiler("profiler");
  50. profiler.AddCompleted().AddCompleted();
  51. EXPECT_EQ(profiler.GetProfile().completed, 2);
  52. }
  53. TEST(CoreStreamProfiler, GetProfile_NoDrop) {
  54. StreamProfiler profiler("profiler");
  55. profiler.UpdatePhysicalTime(std::chrono::duration<double, std::milli>(2.0))
  56. .AddLatency(std::chrono::duration<double, std::milli>(2.0))
  57. .AddCompleted();
  58. profiler.UpdatePhysicalTime(std::chrono::duration<double, std::milli>(4.0))
  59. .AddLatency(std::chrono::duration<double, std::milli>(2.0))
  60. .AddCompleted();
  61. const StreamProfile profile = profiler.GetProfile();
  62. EXPECT_EQ(profile.completed, 2);
  63. EXPECT_EQ(profile.counter, 2);
  64. EXPECT_EQ(profile.dropped, 0);
  65. EXPECT_EQ(profile.fps, 1e3 / 4.0 * 2);
  66. EXPECT_EQ(profile.latency, 2.0);
  67. EXPECT_EQ(profile.maximum_latency, 2.0);
  68. EXPECT_EQ(profile.minimum_latency, 2.0);
  69. }
  70. TEST(CoreStreamProfiler, GetProfile_WithDrop) {
  71. StreamProfiler profiler("profiler");
  72. profiler.UpdatePhysicalTime(std::chrono::duration<double, std::milli>(2.0))
  73. .AddLatency(std::chrono::duration<double, std::milli>(2.0))
  74. .AddCompleted();
  75. profiler.UpdatePhysicalTime(std::chrono::duration<double, std::milli>(4.0))
  76. .AddLatency(std::chrono::duration<double, std::milli>(2.0))
  77. .AddCompleted();
  78. profiler.AddDropped(2);
  79. const StreamProfile profile = profiler.GetProfile();
  80. EXPECT_EQ(profile.completed, 2);
  81. EXPECT_EQ(profile.counter, 4);
  82. EXPECT_EQ(profile.dropped, 2);
  83. EXPECT_EQ(profile.fps, 1e3 / 4.0 * 4);
  84. EXPECT_EQ(profile.latency, 2.0);
  85. EXPECT_EQ(profile.maximum_latency, 2.0);
  86. EXPECT_EQ(profile.minimum_latency, 2.0);
  87. }
  88. TEST(CoreStreamProfiler, GetProfile_MinMaxLatency) {
  89. StreamProfiler profiler("profiler");
  90. profiler.AddLatency(std::chrono::duration<double, std::milli>(2.0))
  91. .AddLatency(std::chrono::duration<double, std::milli>(3.0))
  92. .AddLatency(std::chrono::duration<double, std::milli>(3.0))
  93. .AddLatency(std::chrono::duration<double, std::milli>(1.0));
  94. const StreamProfile profile = profiler.GetProfile();
  95. EXPECT_EQ(profile.maximum_latency, 3.0);
  96. EXPECT_EQ(profile.minimum_latency, 1.0);
  97. }
  98. } // namespace cnstream