From f5c9ed1ff43dbb28b5b2b9d96967937e2cdb7eed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Tue, 9 Apr 2024 07:51:03 -0700 Subject: [PATCH] Decouple PerformanceEntryReporter from NativePerformanceObserver (#43849) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43849 Changelog: [internal] ## Context This is part of a refactor to decouple the performance entry reporter from the rendering infra and from the native module that uses it. ## Changes This refactors `PerformanceEntryReporter` to make the class not depend on the native module that uses it. Instead of using the `RawPerformanceEntry` type from the native module, we define `PerformanceEntry` in `PerformanceEntryReporter` and use it as the source of truth in the native module instead. Thanks to the bridging template sytem we have, we can convert the raw objects passed from JS to the C++ structs, defining how the enums are converted from and to JS. Reviewed By: sammy-SC Differential Revision: D55646394 fbshipit-source-id: 9cf5a7db6ecb221ca08320d0aaae7e7bc8d91804 --- .../NativePerformanceObserver.cpp | 70 ++++++------ .../NativePerformanceObserver.h | 64 ++++++----- .../PerformanceEntryReporter.cpp | 79 +++++++------- .../webperformance/PerformanceEntryReporter.h | 63 +++++++---- .../tests/PerformanceEntryReporterTest.cpp | 100 ++++++++++++------ 5 files changed, 210 insertions(+), 166 deletions(-) diff --git a/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformanceObserver.cpp b/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformanceObserver.cpp index 5f4a5bf3264..5bd4e8f2aac 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformanceObserver.cpp +++ b/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformanceObserver.cpp @@ -36,14 +36,12 @@ NativePerformanceObserver::~NativePerformanceObserver() { void NativePerformanceObserver::startReporting( jsi::Runtime& rt, - int32_t entryType) { + PerformanceEntryType entryType) { PerformanceEntryReporter& reporter = PerformanceEntryReporter::getInstance(); - PerformanceEntryType entryTypeEnum = - static_cast(entryType); - reporter.startReporting(entryTypeEnum); + reporter.startReporting(entryType); - if (entryTypeEnum == PerformanceEntryType::EVENT && + if (entryType == PerformanceEntryType::EVENT && CoreFeatures::enableReportEventPaintTime) { UIManagerBinding::getBinding(rt)->getUIManager().registerMountHook( reporter); @@ -52,14 +50,12 @@ void NativePerformanceObserver::startReporting( void NativePerformanceObserver::stopReporting( jsi::Runtime& rt, - int32_t entryType) { + PerformanceEntryType entryType) { PerformanceEntryReporter& reporter = PerformanceEntryReporter::getInstance(); - PerformanceEntryType entryTypeEnum = - static_cast(entryType); - reporter.stopReporting(entryTypeEnum); + reporter.stopReporting(entryType); - if (entryTypeEnum == PerformanceEntryType::EVENT && + if (entryType == PerformanceEntryType::EVENT && CoreFeatures::enableReportEventPaintTime) { UIManagerBinding::getBinding(rt)->getUIManager().unregisterMountHook( reporter); @@ -67,34 +63,34 @@ void NativePerformanceObserver::stopReporting( } void NativePerformanceObserver::setIsBuffered( - jsi::Runtime& rt, - std::vector entryTypes, + jsi::Runtime& /*rt*/, + const std::vector entryTypes, bool isBuffered) { - for (const int32_t entryType : entryTypes) { + for (const PerformanceEntryType entryType : entryTypes) { PerformanceEntryReporter::getInstance().setAlwaysLogged( - static_cast(entryType), isBuffered); + entryType, isBuffered); } } -GetPendingEntriesResult NativePerformanceObserver::popPendingEntries( - jsi::Runtime& rt) { +PerformanceEntryReporter::PopPendingEntriesResult +NativePerformanceObserver::popPendingEntries(jsi::Runtime& /*rt*/) { return PerformanceEntryReporter::getInstance().popPendingEntries(); } void NativePerformanceObserver::setOnPerformanceEntryCallback( - jsi::Runtime& rt, + jsi::Runtime& /*rt*/, std::optional> callback) { PerformanceEntryReporter::getInstance().setReportingCallback(callback); } void NativePerformanceObserver::logRawEntry( - jsi::Runtime& rt, - RawPerformanceEntry entry) { + jsi::Runtime& /*rt*/, + const PerformanceEntry entry) { PerformanceEntryReporter::getInstance().logEntry(entry); } std::vector> -NativePerformanceObserver::getEventCounts(jsi::Runtime& rt) { +NativePerformanceObserver::getEventCounts(jsi::Runtime& /*rt*/) { const auto& eventCounts = PerformanceEntryReporter::getInstance().getEventCounts(); return std::vector>( @@ -102,38 +98,36 @@ NativePerformanceObserver::getEventCounts(jsi::Runtime& rt) { } void NativePerformanceObserver::setDurationThreshold( - jsi::Runtime& rt, - int32_t entryType, + jsi::Runtime& /*rt*/, + PerformanceEntryType entryType, double durationThreshold) { PerformanceEntryReporter::getInstance().setDurationThreshold( - static_cast(entryType), durationThreshold); + entryType, durationThreshold); } void NativePerformanceObserver::clearEntries( - jsi::Runtime& rt, - int32_t entryType, + jsi::Runtime& /*rt*/, + PerformanceEntryType entryType, std::optional entryName) { PerformanceEntryReporter::getInstance().clearEntries( - static_cast(entryType), - entryName ? entryName->c_str() : nullptr); + entryType, entryName ? entryName->c_str() : std::string_view{}); } -std::vector NativePerformanceObserver::getEntries( - jsi::Runtime& rt, - std::optional entryType, +std::vector NativePerformanceObserver::getEntries( + jsi::Runtime& /*rt*/, + std::optional entryType, std::optional entryName) { return PerformanceEntryReporter::getInstance().getEntries( - entryType ? std::optional{static_cast(*entryType)} - : std::nullopt, - entryName ? entryName->c_str() : nullptr); + entryType, entryName ? entryName->c_str() : std::string_view{}); } -std::vector -NativePerformanceObserver::getSupportedPerformanceEntryTypes(jsi::Runtime& rt) { +std::vector +NativePerformanceObserver::getSupportedPerformanceEntryTypes( + jsi::Runtime& /*rt*/) { return { - static_cast(PerformanceEntryType::MARK), - static_cast(PerformanceEntryType::MEASURE), - static_cast(PerformanceEntryType::EVENT), + PerformanceEntryType::MARK, + PerformanceEntryType::MEASURE, + PerformanceEntryType::EVENT, }; } diff --git a/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformanceObserver.h b/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformanceObserver.h index 8271bde666e..639e31e77f3 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformanceObserver.h +++ b/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformanceObserver.h @@ -8,43 +8,40 @@ #pragma once #include +#include #include #include #include #include namespace facebook::react { -class PerformanceEntryReporter; #pragma mark - Structs -using RawPerformanceEntryType = int32_t; +template <> +struct Bridging { + static PerformanceEntryType fromJs( + jsi::Runtime& /*rt*/, + const jsi::Value& value) { + return static_cast(value.asNumber()); + } -using RawPerformanceEntry = NativePerformanceObserverCxxRawPerformanceEntry< - /* name */ std::string, - /* type */ RawPerformanceEntryType, - /* startTime */ double, - /* duration */ double, - - // For "event" entries only: - /* processingStart */ std::optional, - /* processingEnd */ std::optional, - /* interactionId */ std::optional>; + static jsi::Value toJs( + jsi::Runtime& /*rt*/, + const PerformanceEntryType& value) { + return {static_cast(value)}; + } +}; template <> -struct Bridging +struct Bridging : NativePerformanceObserverCxxRawPerformanceEntryBridging< - RawPerformanceEntry> {}; - -using GetPendingEntriesResult = - NativePerformanceObserverCxxGetPendingEntriesResult< - std::vector, - uint32_t>; + PerformanceEntry> {}; template <> -struct Bridging +struct Bridging : NativePerformanceObserverCxxGetPendingEntriesResultBridging< - GetPendingEntriesResult> {}; + PerformanceEntryReporter::PopPendingEntriesResult> {}; #pragma mark - implementation @@ -54,45 +51,44 @@ class NativePerformanceObserver NativePerformanceObserver(std::shared_ptr jsInvoker); ~NativePerformanceObserver(); - void startReporting(jsi::Runtime& rt, int32_t entryType); + void startReporting(jsi::Runtime& rt, PerformanceEntryType entryType); - void stopReporting(jsi::Runtime& rt, int32_t entryType); + void stopReporting(jsi::Runtime& rt, PerformanceEntryType entryType); void setIsBuffered( jsi::Runtime& rt, - std::vector entryTypes, + const std::vector entryTypes, bool isBuffered); - GetPendingEntriesResult popPendingEntries(jsi::Runtime& rt); + PerformanceEntryReporter::PopPendingEntriesResult popPendingEntries( + jsi::Runtime& rt); void setOnPerformanceEntryCallback( jsi::Runtime& rt, std::optional> callback); - void logRawEntry(jsi::Runtime& rt, RawPerformanceEntry entry); + void logRawEntry(jsi::Runtime& rt, const PerformanceEntry entry); std::vector> getEventCounts( jsi::Runtime& rt); void setDurationThreshold( jsi::Runtime& rt, - int32_t entryType, - double durationThreshold); + PerformanceEntryType entryType, + DOMHighResTimeStamp durationThreshold); void clearEntries( jsi::Runtime& rt, - int32_t entryType, + PerformanceEntryType entryType, std::optional entryName); - std::vector getEntries( + std::vector getEntries( jsi::Runtime& rt, - std::optional entryType, + std::optional entryType, std::optional entryName); - std::vector getSupportedPerformanceEntryTypes( + std::vector getSupportedPerformanceEntryTypes( jsi::Runtime& rt); - - private: }; } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/nativemodule/webperformance/PerformanceEntryReporter.cpp b/packages/react-native/ReactCommon/react/nativemodule/webperformance/PerformanceEntryReporter.cpp index 743ca4b5aec..60669bde4d1 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/webperformance/PerformanceEntryReporter.cpp +++ b/packages/react-native/ReactCommon/react/nativemodule/webperformance/PerformanceEntryReporter.cpp @@ -9,7 +9,6 @@ #include #include #include -#include "NativePerformanceObserver.h" #include #include @@ -35,7 +34,7 @@ void PerformanceEntryReporter::setReportingCallback( callback_ = callback; } -double PerformanceEntryReporter::getCurrentTimeStamp() const { +DOMHighResTimeStamp PerformanceEntryReporter::getCurrentTimeStamp() const { return timeStampProvider_ != nullptr ? timeStampProvider_() : JSExecutor::performanceNow(); } @@ -55,7 +54,7 @@ void PerformanceEntryReporter::setAlwaysLogged( void PerformanceEntryReporter::setDurationThreshold( PerformanceEntryType entryType, - double durationThreshold) { + DOMHighResTimeStamp durationThreshold) { getBuffer(entryType).durationThreshold = durationThreshold; } @@ -69,10 +68,12 @@ void PerformanceEntryReporter::stopReporting() { } } -GetPendingEntriesResult PerformanceEntryReporter::popPendingEntries() { +PerformanceEntryReporter::PopPendingEntriesResult +PerformanceEntryReporter::popPendingEntries() { std::lock_guard lock(entriesMutex_); - GetPendingEntriesResult res = { - std::vector(), droppedEntryCount_}; + PopPendingEntriesResult res = { + .entries = std::vector(), + .droppedEntriesCount = droppedEntriesCount_}; for (auto& buffer : buffers_) { buffer.entries.consume(res.entries); } @@ -81,7 +82,7 @@ GetPendingEntriesResult PerformanceEntryReporter::popPendingEntries() { std::stable_sort( res.entries.begin(), res.entries.end(), - [](const RawPerformanceEntry& lhs, const RawPerformanceEntry& rhs) { + [](const PerformanceEntry& lhs, const PerformanceEntry& rhs) { if (lhs.startTime != rhs.startTime) { return lhs.startTime < rhs.startTime; } else { @@ -89,23 +90,22 @@ GetPendingEntriesResult PerformanceEntryReporter::popPendingEntries() { } }); - droppedEntryCount_ = 0; + droppedEntriesCount_ = 0; return res; } -void PerformanceEntryReporter::logEntry(const RawPerformanceEntry& entry) { - const auto entryType = static_cast(entry.entryType); - if (entryType == PerformanceEntryType::EVENT) { +void PerformanceEntryReporter::logEntry(const PerformanceEntry& entry) { + if (entry.entryType == PerformanceEntryType::EVENT) { eventCounts_[entry.name]++; } - if (!isReporting(entryType) && !isAlwaysLogged(entryType)) { + if (!isReporting(entry.entryType) && !isAlwaysLogged(entry.entryType)) { return; } std::lock_guard lock(entriesMutex_); - auto& buffer = getBuffer(entryType); + auto& buffer = getBuffer(entry.entryType); if (entry.duration < buffer.durationThreshold) { // The entries duration is lower than the desired reporting threshold, skip @@ -127,11 +127,11 @@ void PerformanceEntryReporter::logEntry(const RawPerformanceEntry& entry) { auto pushResult = buffer.entries.add(std::move(entry)); if (pushResult == - BoundedConsumableBuffer::PushStatus::DROP) { + BoundedConsumableBuffer::PushStatus::DROP) { // Start dropping entries once reached maximum buffer size. // The number of dropped entries will be reported back to the corresponding // PerformanceObserver callback. - droppedEntryCount_ += 1; + droppedEntriesCount_ += 1; } if (buffer.hasNameLookup) { @@ -153,10 +153,10 @@ void PerformanceEntryReporter::logEntry(const RawPerformanceEntry& entry) { void PerformanceEntryReporter::mark( const std::string& name, - const std::optional& startTime) { - logEntry(RawPerformanceEntry{ + const std::optional& startTime) { + logEntry(PerformanceEntry{ .name = name, - .entryType = static_cast(PerformanceEntryType::MARK), + .entryType = PerformanceEntryType::MARK, .startTime = startTime ? *startTime : getCurrentTimeStamp()}); } @@ -177,7 +177,7 @@ void PerformanceEntryReporter::clearEntries( } std::lock_guard lock(entriesMutex_); - buffer.entries.clear([entryName](const RawPerformanceEntry& entry) { + buffer.entries.clear([entryName](const PerformanceEntry& entry) { return entry.name == entryName; }); @@ -207,22 +207,22 @@ void PerformanceEntryReporter::clearEntries( void PerformanceEntryReporter::getEntries( PerformanceEntryType entryType, std::string_view entryName, - std::vector& res) const { + std::vector& res) const { std::lock_guard lock(entriesMutex_); const auto& entries = getBuffer(entryType).entries; if (entryName.empty()) { entries.getEntries(res); } else { - entries.getEntries(res, [entryName](const RawPerformanceEntry& entry) { + entries.getEntries(res, [entryName](const PerformanceEntry& entry) { return entry.name == entryName; }); } } -std::vector PerformanceEntryReporter::getEntries( +std::vector PerformanceEntryReporter::getEntries( std::optional entryType, std::string_view entryName) const { - std::vector res; + std::vector res; if (!entryType) { // Collect all entry types for (int i = 1; i < NUM_PERFORMANCE_ENTRY_TYPES; i++) { @@ -236,13 +236,14 @@ std::vector PerformanceEntryReporter::getEntries( void PerformanceEntryReporter::measure( const std::string& name, - double startTime, - double endTime, - const std::optional& duration, + DOMHighResTimeStamp startTime, + DOMHighResTimeStamp endTime, + const std::optional& duration, const std::optional& startMark, const std::optional& endMark) { - double startTimeVal = startMark ? getMarkTime(*startMark) : startTime; - double endTimeVal = endMark ? getMarkTime(*endMark) : endTime; + DOMHighResTimeStamp startTimeVal = + startMark ? getMarkTime(*startMark) : startTime; + DOMHighResTimeStamp endTimeVal = endMark ? getMarkTime(*endMark) : endTime; if (!endMark && endTime < startTimeVal) { // The end time is not specified, take the current time, according to the @@ -250,20 +251,20 @@ void PerformanceEntryReporter::measure( endTimeVal = getCurrentTimeStamp(); } - double durationVal = duration ? *duration : endTimeVal - startTimeVal; + DOMHighResTimeStamp durationVal = + duration ? *duration : endTimeVal - startTimeVal; logEntry( {.name = name, - .entryType = static_cast(PerformanceEntryType::MEASURE), + .entryType = PerformanceEntryType::MEASURE, .startTime = startTimeVal, .duration = durationVal}); } -double PerformanceEntryReporter::getMarkTime( +DOMHighResTimeStamp PerformanceEntryReporter::getMarkTime( const std::string& markName) const { - RawPerformanceEntry mark{ - .name = markName, - .entryType = static_cast(PerformanceEntryType::MARK)}; + PerformanceEntry mark{ + .name = markName, .entryType = PerformanceEntryType::MARK}; std::lock_guard lock(nameLookupMutex_); const auto& marksBuffer = getBuffer(PerformanceEntryType::MARK); @@ -277,14 +278,14 @@ double PerformanceEntryReporter::getMarkTime( void PerformanceEntryReporter::logEventEntry( std::string name, - double startTime, - double duration, - double processingStart, - double processingEnd, + DOMHighResTimeStamp startTime, + DOMHighResTimeStamp duration, + DOMHighResTimeStamp processingStart, + DOMHighResTimeStamp processingEnd, uint32_t interactionId) { logEntry( {.name = std::move(name), - .entryType = static_cast(PerformanceEntryType::EVENT), + .entryType = PerformanceEntryType::EVENT, .startTime = startTime, .duration = duration, .processingStart = processingStart, diff --git a/packages/react-native/ReactCommon/react/nativemodule/webperformance/PerformanceEntryReporter.h b/packages/react-native/ReactCommon/react/nativemodule/webperformance/PerformanceEntryReporter.h index 50e16019cc4..287d05bdf6a 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/webperformance/PerformanceEntryReporter.h +++ b/packages/react-native/ReactCommon/react/nativemodule/webperformance/PerformanceEntryReporter.h @@ -17,28 +17,50 @@ #include #include #include "BoundedConsumableBuffer.h" -#include "NativePerformanceObserver.h" #include namespace facebook::react { +using DOMHighResTimeStamp = double; + +using PerformanceEntryInteractionId = uint32_t; + +enum class PerformanceEntryType { + // We need to preserve these values for backwards compatibility. + MARK = 1, + MEASURE = 2, + EVENT = 3, + _NEXT = 4, +}; + +struct PerformanceEntry { + std::string name; + PerformanceEntryType entryType; + DOMHighResTimeStamp startTime; + DOMHighResTimeStamp duration = 0; + + // For "event" entries only: + std::optional processingStart; + std::optional processingEnd; + std::optional interactionId; +}; + struct PerformanceEntryHash { - size_t operator()(const RawPerformanceEntry* entry) const { + size_t operator()(const PerformanceEntry* entry) const { return std::hash()(entry->name); } }; struct PerformanceEntryEqual { - bool operator()( - const RawPerformanceEntry* lhs, - const RawPerformanceEntry* rhs) const { + bool operator()(const PerformanceEntry* lhs, const PerformanceEntry* rhs) + const { return lhs->name == rhs->name; } }; using PerformanceEntryRegistryType = std::unordered_set< - const RawPerformanceEntry*, + const PerformanceEntry*, PerformanceEntryHash, PerformanceEntryEqual>; @@ -50,7 +72,7 @@ constexpr double DEFAULT_DURATION_THRESHOLD = 0.0; constexpr size_t DEFAULT_MAX_BUFFER_SIZE = 1024; struct PerformanceEntryBuffer { - BoundedConsumableBuffer entries{DEFAULT_MAX_BUFFER_SIZE}; + BoundedConsumableBuffer entries{DEFAULT_MAX_BUFFER_SIZE}; bool isReporting{false}; bool isAlwaysLogged{false}; double durationThreshold{DEFAULT_DURATION_THRESHOLD}; @@ -58,14 +80,6 @@ struct PerformanceEntryBuffer { PerformanceEntryRegistryType nameLookup; }; -enum class PerformanceEntryType { - // We need to preserve these values for backwards compatibility. - MARK = 1, - MEASURE = 2, - EVENT = 3, - _NEXT = 4, -}; - constexpr size_t NUM_PERFORMANCE_ENTRY_TYPES = (size_t)PerformanceEntryType::_NEXT - 1; // Valid types start from 1. @@ -80,6 +94,11 @@ class PerformanceEntryReporter : public EventLogger, public UIManagerMountHook { // creation time instead of having the singleton. static PerformanceEntryReporter& getInstance(); + struct PopPendingEntriesResult { + std::vector entries; + uint32_t droppedEntriesCount; + }; + void setReportingCallback(std::optional> callback); void startReporting(PerformanceEntryType entryType); void stopReporting(PerformanceEntryType entryType); @@ -89,9 +108,9 @@ class PerformanceEntryReporter : public EventLogger, public UIManagerMountHook { PerformanceEntryType entryType, double durationThreshold); - GetPendingEntriesResult popPendingEntries(); + PopPendingEntriesResult popPendingEntries(); - void logEntry(const RawPerformanceEntry& entry); + void logEntry(const PerformanceEntry& entry); PerformanceEntryBuffer& getBuffer(PerformanceEntryType entryType) { return buffers_[static_cast(entryType) - 1]; @@ -110,8 +129,8 @@ class PerformanceEntryReporter : public EventLogger, public UIManagerMountHook { return getBuffer(entryType).isAlwaysLogged; } - uint32_t getDroppedEntryCount() const { - return droppedEntryCount_; + uint32_t getDroppedEntriesCount() const { + return droppedEntriesCount_; } void mark( @@ -130,7 +149,7 @@ class PerformanceEntryReporter : public EventLogger, public UIManagerMountHook { std::optional entryType = std::nullopt, std::string_view entryName = {}); - std::vector getEntries( + std::vector getEntries( std::optional entryType = std::nullopt, std::string_view entryName = {}) const; @@ -165,7 +184,7 @@ class PerformanceEntryReporter : public EventLogger, public UIManagerMountHook { std::array buffers_; std::unordered_map eventCounts_; - uint32_t droppedEntryCount_{0}; + uint32_t droppedEntriesCount_{0}; struct EventEntry { std::string_view name; @@ -199,7 +218,7 @@ class PerformanceEntryReporter : public EventLogger, public UIManagerMountHook { void getEntries( PerformanceEntryType entryType, std::string_view entryName, - std::vector& res) const; + std::vector& res) const; double getCurrentTimeStamp() const; }; diff --git a/packages/react-native/ReactCommon/react/nativemodule/webperformance/tests/PerformanceEntryReporterTest.cpp b/packages/react-native/ReactCommon/react/nativemodule/webperformance/tests/PerformanceEntryReporterTest.cpp index b1b0e50be51..caee605ab45 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/webperformance/tests/PerformanceEntryReporterTest.cpp +++ b/packages/react-native/ReactCommon/react/nativemodule/webperformance/tests/PerformanceEntryReporterTest.cpp @@ -13,9 +13,19 @@ namespace facebook::react { -static std::ostream& operator<<( +[[maybe_unused]] static bool operator==( + const PerformanceEntry& lhs, + const PerformanceEntry& rhs) { + return lhs.name == rhs.name && lhs.entryType == rhs.entryType && + lhs.startTime == rhs.startTime && lhs.duration == rhs.duration && + lhs.processingStart == rhs.processingStart && + lhs.processingEnd == rhs.processingEnd && + lhs.interactionId == rhs.interactionId; +} + +[[maybe_unused]] static std::ostream& operator<<( std::ostream& os, - const RawPerformanceEntry& entry) { + const PerformanceEntry& entry) { static constexpr const char* entryTypeNames[] = { "UNDEFINED", "MARK", @@ -23,7 +33,7 @@ static std::ostream& operator<<( "EVENT", }; return os << "{ name: " << entry.name - << ", type: " << entryTypeNames[entry.entryType] + << ", type: " << entryTypeNames[static_cast(entry.entryType)] << ", startTime: " << entry.startTime << ", duration: " << entry.duration << " }"; } @@ -103,18 +113,18 @@ TEST(PerformanceEntryReporter, PerformanceEntryReporterTestReportMarks) { ASSERT_EQ(0, res.droppedEntriesCount); ASSERT_EQ(4, entries.size()); - const std::vector expected = { + const std::vector expected = { {.name = "mark0", - .entryType = static_cast(PerformanceEntryType::MARK), + .entryType = PerformanceEntryType::MARK, .startTime = 0.0}, {.name = "mark1", - .entryType = static_cast(PerformanceEntryType::MARK), + .entryType = PerformanceEntryType::MARK, .startTime = 1.0}, {.name = "mark2", - .entryType = static_cast(PerformanceEntryType::MARK), + .entryType = PerformanceEntryType::MARK, .startTime = 2.0}, {.name = "mark0", - .entryType = static_cast(PerformanceEntryType::MARK), + .entryType = PerformanceEntryType::MARK, .startTime = 3.0}, }; @@ -155,80 +165,80 @@ TEST(PerformanceEntryReporter, PerformanceEntryReporterTestReportMeasures) { ASSERT_EQ(0, res.droppedEntriesCount); - const std::vector expected = { + const std::vector expected = { {.name = "mark0", - .entryType = static_cast(PerformanceEntryType::MARK), + .entryType = PerformanceEntryType::MARK, .startTime = 0.0}, {.name = "measure0", - .entryType = static_cast(PerformanceEntryType::MEASURE), + .entryType = PerformanceEntryType::MEASURE, .startTime = 0.0, .duration = 2.0}, {.name = "measure1", - .entryType = static_cast(PerformanceEntryType::MEASURE), + .entryType = PerformanceEntryType::MEASURE, .startTime = 0.0, .duration = 4.0}, {.name = "mark1", - .entryType = static_cast(PerformanceEntryType::MARK), + .entryType = PerformanceEntryType::MARK, .startTime = 1.0}, {.name = "measure2", - .entryType = static_cast(PerformanceEntryType::MEASURE), + .entryType = PerformanceEntryType::MEASURE, .startTime = 1.0, .duration = 1.0}, {.name = "measure7", - .entryType = static_cast(PerformanceEntryType::MEASURE), + .entryType = PerformanceEntryType::MEASURE, .startTime = 1.0, .duration = 2.0}, {.name = "measure3", - .entryType = static_cast(PerformanceEntryType::MEASURE), + .entryType = PerformanceEntryType::MEASURE, .startTime = 1.0, .duration = 5.0}, {.name = "measure4", - .entryType = static_cast(PerformanceEntryType::MEASURE), + .entryType = PerformanceEntryType::MEASURE, .startTime = 1.5, .duration = 0.5}, {.name = "mark2", - .entryType = static_cast(PerformanceEntryType::MARK), + .entryType = PerformanceEntryType::MARK, .startTime = 2.0}, {.name = "mark3", - .entryType = static_cast(PerformanceEntryType::MARK), + .entryType = PerformanceEntryType::MARK, .startTime = 2.0}, {.name = "mark4", - .entryType = static_cast(PerformanceEntryType::MARK), + .entryType = PerformanceEntryType::MARK, .startTime = 2.0}, {.name = "measure6", - .entryType = static_cast(PerformanceEntryType::MEASURE), + .entryType = PerformanceEntryType::MEASURE, .startTime = 2.0, .duration = 0.0}, {.name = "measure5", - .entryType = static_cast(PerformanceEntryType::MEASURE), + .entryType = PerformanceEntryType::MEASURE, .startTime = 2.0, .duration = 1.5}, {.name = "mark4", - .entryType = static_cast(PerformanceEntryType::MARK), + .entryType = PerformanceEntryType::MARK, .startTime = 3.0}}; ASSERT_EQ(expected, entries); } static std::vector getNames( - const std::vector& entries) { + const std::vector& entries) { std::vector res; std::transform( entries.begin(), entries.end(), std::back_inserter(res), - [](const RawPerformanceEntry& e) { return e.name; }); + [](const PerformanceEntry& e) { return e.name; }); return res; } -static std::vector getTypes( - const std::vector& entries) { - std::vector res; +static std::vector getTypes( + const std::vector& entries) { + std::vector res; std::transform( entries.begin(), entries.end(), std::back_inserter(res), - [](const RawPerformanceEntry& e) { return e.entryType; }); + [](const PerformanceEntry& e) { return e.entryType; }); return res; } @@ -270,10 +280,34 @@ TEST(PerformanceEntryReporter, PerformanceEntryReporterTestGetEntries) { reporter.getEntries(); const auto all = reporter.getEntries(); - ASSERT_EQ(std::vector({2, 2, 2, 2, 2}), getTypes(measures)); - ASSERT_EQ(std::vector({1, 2}), getTypes(common_name)); - ASSERT_EQ(std::vector({1, 1, 1, 2, 2, 2, 2, 2}), getTypes(all)); - ASSERT_EQ(std::vector({1, 1, 1}), getTypes(marks)); + ASSERT_EQ( + std::vector( + {PerformanceEntryType::MEASURE, + PerformanceEntryType::MEASURE, + PerformanceEntryType::MEASURE, + PerformanceEntryType::MEASURE, + PerformanceEntryType::MEASURE}), + getTypes(measures)); + ASSERT_EQ( + std::vector({PerformanceEntryType::MARK, PerformanceEntryType::MEASURE}), + getTypes(common_name)); + ASSERT_EQ( + std::vector( + {PerformanceEntryType::MARK, + PerformanceEntryType::MARK, + PerformanceEntryType::MARK, + PerformanceEntryType::MEASURE, + PerformanceEntryType::MEASURE, + PerformanceEntryType::MEASURE, + PerformanceEntryType::MEASURE, + PerformanceEntryType::MEASURE}), + getTypes(all)); + ASSERT_EQ( + std::vector( + {PerformanceEntryType::MARK, + PerformanceEntryType::MARK, + PerformanceEntryType::MARK}), + getTypes(marks)); ASSERT_EQ( std::vector({"common_name", "mark1", "mark2"}),