TestPerformanceRecorder.cpp (3256B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 #include <chrono> 5 #include <thread> 6 7 #include "PerformanceRecorder.h" 8 #include "gtest/gtest.h" 9 #include "nsString.h" 10 11 using namespace mozilla; 12 13 class PerformanceRecorderWrapper : public PerformanceRecorder<PlaybackStage> { 14 public: 15 PerformanceRecorderWrapper(MediaStage aStage, int32_t aHeight) 16 : PerformanceRecorder(aStage, aHeight) {} 17 18 static void EnableMeasurementOnNonMarkerSituation() { 19 sEnableMeasurementForTesting = true; 20 } 21 }; 22 23 TEST(PerformanceRecorder, TestResolution) 24 { 25 PerformanceRecorderWrapper::EnableMeasurementOnNonMarkerSituation(); 26 27 static const struct { 28 const int32_t mH; 29 const char* mRes; 30 } resolutions[] = {{0, "A:0"}, 31 {240, "V:0<h<=240"}, 32 {480, "V:240<h<=480"}, 33 {576, "V:480<h<=576"}, 34 {720, "V:576<h<=720"}, 35 {1080, "V:720<h<=1080"}, 36 {1440, "V:1080<h<=1440"}, 37 {2160, "V:1440<h<=2160"}, 38 {4320, "V:h>2160"}}; 39 40 const MediaStage stage = MediaStage::RequestDecode; 41 for (auto&& res : resolutions) { 42 PerformanceRecorderWrapper w(stage, res.mH); 43 nsCString name; 44 w.Record([&](auto& aStage) { name = nsCString(aStage.Name()); }); 45 ASSERT_NE(name.Find(res.mRes), kNotFound); 46 } 47 } 48 49 TEST(PerformanceRecorder, TestMoveOperation) 50 { 51 PerformanceRecorderWrapper::EnableMeasurementOnNonMarkerSituation(); 52 53 const MediaStage stage = MediaStage::RequestDecode; 54 const uint32_t resolution = 1080; 55 PerformanceRecorderWrapper w1(stage, resolution); 56 std::this_thread::sleep_for(std::chrono::milliseconds(1)); 57 58 // w1 has been moved which won't continue measuring data. 59 PerformanceRecorderWrapper w2(std::move(w1)); 60 ASSERT_DOUBLE_EQ(w1.Record(), 0.0); 61 ASSERT_TRUE(w2.Record() > 0.0); 62 } 63 64 TEST(PerformanceRecorder, TestRecordInvalidation) 65 { 66 PerformanceRecorderWrapper::EnableMeasurementOnNonMarkerSituation(); 67 68 const MediaStage stage = MediaStage::RequestDecode; 69 const uint32_t resolution = 1080; 70 PerformanceRecorderWrapper w(stage, resolution); 71 std::this_thread::sleep_for(std::chrono::milliseconds(1)); 72 73 ASSERT_TRUE(w.Record() > 0.0); 74 75 w.Record(); 76 // w has been recorded and won't continue measuring data. 77 ASSERT_DOUBLE_EQ(w.Record(), 0.0); 78 } 79 80 TEST(PerformanceRecorder, TestMultipleRecords) 81 { 82 PerformanceRecorderWrapper::EnableMeasurementOnNonMarkerSituation(); 83 84 const MediaStage stage = MediaStage::RequestDecode; 85 PerformanceRecorderMulti<PlaybackStage> r; 86 87 r.Start(1, stage, 1); 88 r.Start(2, stage, 2); 89 r.Start(3, stage, 3); 90 91 std::this_thread::sleep_for(std::chrono::milliseconds(1)); 92 93 // id 0 wasn't started 94 EXPECT_DOUBLE_EQ(r.Record(0), 0.0); 95 96 // id 1 gets recorded normally 97 EXPECT_TRUE(r.Record(1) > 0.0); 98 99 // id 1 was already recorded 100 EXPECT_DOUBLE_EQ(r.Record(1), 0.0); 101 102 // id 2 gets recorded normally 103 EXPECT_TRUE(r.Record(2) > 0.0); 104 105 // id 4 wasn't started 106 EXPECT_DOUBLE_EQ(r.Record(4), 0.0); 107 108 // All lower ids got discarded 109 EXPECT_DOUBLE_EQ(r.Record(3), 0.0); 110 }