Bug 1730020 - part3 : add gtest. r=padenot

Differential Revision: https://phabricator.services.mozilla.com/D126423
This commit is contained in:
alwu 2021-10-04 22:30:10 +00:00
Родитель 929314f612
Коммит 726674ac63
5 изменённых файлов: 113 добавлений и 9 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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<h<=1440.
static const char* FindMediaResolution(int32_t aHeight);
Stage mStage = Stage::Invalid;
int32_t mHeight;
MediaInfoFlag mFlag = MediaInfoFlag::None;
Maybe<TimeStamp> mStartTime;
// We would enable the measurement on testing.
static inline bool sEnableMeasurementForTesting = false;
};
} // namespace mozilla

Просмотреть файл

@ -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 <chrono>
#include <thread>
#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:0<h<=240"},
{480, "V:240<h<=480"},
{576, "V:480<h<=576"},
{720, "V:576<h<=720"},
{1080, "V:720<h<=1080"},
{1440, "V:1080<h<=1440"},
{2160, "V:1440<h<=2160"},
{4320, "V:h>2160"}};
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);
}

Просмотреть файл

@ -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"]

Просмотреть файл

@ -18,4 +18,7 @@ UNIFIED_SOURCES += [
"TelemetryProbesReporter.cpp",
]
if CONFIG["ENABLE_TESTS"]:
DIRS += ["gtest"]
FINAL_LIBRARY = "xul"