From 726674ac63262b5534536b79cd6779590f6eb447 Mon Sep 17 00:00:00 2001 From: alwu Date: Mon, 4 Oct 2021 22:30:10 +0000 Subject: [PATCH] Bug 1730020 - part3 : add gtest. r=padenot Differential Revision: https://phabricator.services.mozilla.com/D126423 --- dom/media/utils/PerformanceRecorder.cpp | 24 +++++--- dom/media/utils/PerformanceRecorder.h | 16 ++++- .../utils/gtest/TestPerformanceRecorder.cpp | 61 +++++++++++++++++++ dom/media/utils/gtest/moz.build | 18 ++++++ dom/media/utils/moz.build | 3 + 5 files changed, 113 insertions(+), 9 deletions(-) create mode 100644 dom/media/utils/gtest/TestPerformanceRecorder.cpp create mode 100644 dom/media/utils/gtest/moz.build diff --git a/dom/media/utils/PerformanceRecorder.cpp b/dom/media/utils/PerformanceRecorder.cpp index 1ce470921759..162d22cc3f36 100644 --- a/dom/media/utils/PerformanceRecorder.cpp +++ b/dom/media/utils/PerformanceRecorder.cpp @@ -29,7 +29,8 @@ const char* StageToStr(PerformanceRecorder::Stage aStage) { } } -const char* FindMediaResolution(int32_t aHeight) { +/* static */ +const char* PerformanceRecorder::FindMediaResolution(int32_t aHeight) { static const struct { const int32_t mH; const nsCString mRes; @@ -52,11 +53,18 @@ const char* FindMediaResolution(int32_t aHeight) { return resolution; } -static TimeStamp GetCurrentTimeForMeasurement() { +/* static */ +bool PerformanceRecorder::IsMeasurementEnabled() { + return profiler_can_accept_markers() || + PerformanceRecorder::sEnableMeasurementForTesting; +} + +/* static */ +TimeStamp PerformanceRecorder::GetCurrentTimeForMeasurement() { // The system call to get the clock is rather expensive on Windows. As we // only report the measurement report via markers, if the marker isn't enabled // then we won't do any measurement in order to save CPU time. - return profiler_can_accept_markers() ? TimeStamp::Now() : TimeStamp(); + return IsMeasurementEnabled() ? TimeStamp::Now() : TimeStamp(); } void PerformanceRecorder::Start() { @@ -92,13 +100,14 @@ void AppendMediaInfoFlagToName(nsCString& aName, MediaInfoFlag aFlag) { } } -void PerformanceRecorder::End() { +float PerformanceRecorder::End() { + double elapsedTimeUs = 0.0; if (mStartTime && !mStartTime->IsNull()) { MOZ_ASSERT(mStage != Stage::Invalid); - if (profiler_can_accept_markers()) { + if (IsMeasurementEnabled()) { const auto now = TimeStamp::Now(); - const double passedTimeUs = (now - *mStartTime).ToMicroseconds(); - MOZ_ASSERT(passedTimeUs > 0, "Passed time can't be less than 0!"); + elapsedTimeUs = (now - *mStartTime).ToMicroseconds(); + MOZ_ASSERT(elapsedTimeUs >= 0, "Elapsed time can't be less than 0!"); nsAutoCString name(StageToStr(mStage)); name.Append(":"); name.Append(FindMediaResolution(mHeight)); @@ -110,6 +119,7 @@ void PerformanceRecorder::End() { } Reset(); } + return elapsedTimeUs; } } // namespace mozilla diff --git a/dom/media/utils/PerformanceRecorder.h b/dom/media/utils/PerformanceRecorder.h index 8ee75b630dd9..d6495dffbba3 100644 --- a/dom/media/utils/PerformanceRecorder.h +++ b/dom/media/utils/PerformanceRecorder.h @@ -106,15 +106,27 @@ class PerformanceRecorder { PerformanceRecorder& operator=(const PerformanceRecorder&) = delete; void Start(); - void End(); - private: + // Return the passed time if it has started and still valid. Otherwise, + // return 0. + float End(); + + protected: void Reset(); + static bool IsMeasurementEnabled(); + static TimeStamp GetCurrentTimeForMeasurement(); + + // Return the resolution range for the given height. Eg. V:1080 mStartTime; + + // We would enable the measurement on testing. + static inline bool sEnableMeasurementForTesting = false; }; } // namespace mozilla diff --git a/dom/media/utils/gtest/TestPerformanceRecorder.cpp b/dom/media/utils/gtest/TestPerformanceRecorder.cpp new file mode 100644 index 000000000000..943d62e702e4 --- /dev/null +++ b/dom/media/utils/gtest/TestPerformanceRecorder.cpp @@ -0,0 +1,61 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include +#include + +#include "PerformanceRecorder.h" +#include "gtest/gtest.h" +#include "nsString.h" + +using Stage = mozilla::PerformanceRecorder::Stage; + +class PerformanceRecorderWrapper : public mozilla::PerformanceRecorder { + public: + PerformanceRecorderWrapper(Stage aStage, int32_t aHeight) + : PerformanceRecorder(aStage, aHeight) {} + + const char* Resolution() const { return FindMediaResolution(mHeight); } + + static void EnableMeasurementOnNonMarkerSituation() { + sEnableMeasurementForTesting = true; + } +}; + +TEST(PerformanceRecorder, TestResolution) +{ + static const struct { + const int32_t mH; + const char* mRes; + } resolutions[] = {{0, "A:0"}, + {240, "V:02160"}}; + + const Stage stage = Stage::RequestDecode; + for (auto&& res : resolutions) { + PerformanceRecorderWrapper w(stage, res.mH); + ASSERT_STREQ(w.Resolution(), res.mRes); + } +} + +TEST(PerformanceRecorder, TestMoveOperation) +{ + PerformanceRecorderWrapper::EnableMeasurementOnNonMarkerSituation(); + + const Stage stage = Stage::RequestDecode; + const uint32_t resolution = 1080; + PerformanceRecorderWrapper w1(stage, resolution); + w1.Start(); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + + // w1 has been moved which won't continue measuring data. + PerformanceRecorderWrapper w2(std::move(w1)); + ASSERT_DOUBLE_EQ(w1.End(), 0.0); + ASSERT_TRUE(w2.End() > 0.0); +} diff --git a/dom/media/utils/gtest/moz.build b/dom/media/utils/gtest/moz.build new file mode 100644 index 000000000000..8486f52f738a --- /dev/null +++ b/dom/media/utils/gtest/moz.build @@ -0,0 +1,18 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +UNIFIED_SOURCES += [ + "TestPerformanceRecorder.cpp", +] + +LOCAL_INCLUDES += [ + "/dom/media/utils", +] + +FINAL_LIBRARY = "xul-gtest" + +if CONFIG["CC_TYPE"] in ("clang", "gcc"): + CXXFLAGS += ["-Wno-error=shadow"] diff --git a/dom/media/utils/moz.build b/dom/media/utils/moz.build index f9aa10de654c..cc91c79c6477 100644 --- a/dom/media/utils/moz.build +++ b/dom/media/utils/moz.build @@ -18,4 +18,7 @@ UNIFIED_SOURCES += [ "TelemetryProbesReporter.cpp", ] +if CONFIG["ENABLE_TESTS"]: + DIRS += ["gtest"] + FINAL_LIBRARY = "xul"