From 0018a695da5618647c87920dd8322a04c0579519 Mon Sep 17 00:00:00 2001 From: Xin Chen Date: Thu, 9 Mar 2023 08:34:58 -0800 Subject: [PATCH] Refactor startup performance API implementation Summary: Refactor the startup performance API so that we are not using a vector to store only a few startup metrics values. This makes the metrics more strictly typed and concise. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D43860049 fbshipit-source-id: 2c2102de2b64e2c5cda509ac26f0d1d072287083 --- .../WebPerformance/NativePerformance.cpp | 27 +++------ ReactCommon/cxxreact/ReactMarker.cpp | 55 +++++++++++-------- ReactCommon/cxxreact/ReactMarker.h | 12 ++-- 3 files changed, 46 insertions(+), 48 deletions(-) diff --git a/Libraries/WebPerformance/NativePerformance.cpp b/Libraries/WebPerformance/NativePerformance.cpp index c97020d8914..d08810da47c 100644 --- a/Libraries/WebPerformance/NativePerformance.cpp +++ b/Libraries/WebPerformance/NativePerformance.cpp @@ -58,26 +58,15 @@ std::unordered_map NativePerformance::getSimpleMemoryInfo( ReactNativeStartupTiming NativePerformance::getReactNativeStartupTiming( jsi::Runtime &rt) { ReactNativeStartupTiming result = {0, 0, 0, 0}; - result.startTime = ReactMarker::getAppStartTime(); - auto startupReactMarkers = - ReactMarker::StartupLogger::getInstance().getStartupReactMarkers(); - for (const auto &startupReactMarker : startupReactMarkers) { - auto time = startupReactMarker.time; - switch (startupReactMarker.markerId) { - case ReactMarker::ReactMarkerId::RUN_JS_BUNDLE_START: - result.executeJavaScriptBundleEntryPointStart = time; - break; - - case ReactMarker::ReactMarkerId::RUN_JS_BUNDLE_STOP: - result.executeJavaScriptBundleEntryPointEnd = time; - result.endTime = time; - break; - - default: - break; - } - } + ReactMarker::StartupLogger &startupLogger = + ReactMarker::StartupLogger::getInstance(); + result.startTime = startupLogger.getAppStartTime(); + result.executeJavaScriptBundleEntryPointStart = + startupLogger.getRunJSBundleStartTime(); + result.executeJavaScriptBundleEntryPointEnd = + startupLogger.getRunJSBundleEndTime(); + result.endTime = startupLogger.getRunJSBundleEndTime(); return result; } diff --git a/ReactCommon/cxxreact/ReactMarker.cpp b/ReactCommon/cxxreact/ReactMarker.cpp index a37d64020d7..c154fccc1cb 100644 --- a/ReactCommon/cxxreact/ReactMarker.cpp +++ b/ReactCommon/cxxreact/ReactMarker.cpp @@ -30,7 +30,7 @@ void logMarker(const ReactMarkerId markerId) { } void logTaggedMarker(const ReactMarkerId markerId, const char *tag) { - StartupLogger::getInstance().logStartupEvent(markerId, tag); + StartupLogger::getInstance().logStartupEvent(markerId); logTaggedMarkerImpl(markerId, tag); } @@ -39,40 +39,49 @@ void logMarkerBridgeless(const ReactMarkerId markerId) { } void logTaggedMarkerBridgeless(const ReactMarkerId markerId, const char *tag) { - StartupLogger::getInstance().logStartupEvent(markerId, tag); + StartupLogger::getInstance().logStartupEvent(markerId); logTaggedMarkerBridgelessImpl(markerId, tag); } -double getAppStartTime() { - if (getAppStartTimeImpl == nullptr) { - return 0; - } - - return getAppStartTimeImpl(); -} - StartupLogger &StartupLogger::getInstance() { static StartupLogger instance; return instance; } -void StartupLogger::logStartupEvent( - const ReactMarkerId markerId, - const char *tag) { - if (startupStopped) { - return; - } +void StartupLogger::logStartupEvent(const ReactMarkerId markerId) { + auto now = JSExecutor::performanceNow(); + switch (markerId) { + case ReactMarkerId::RUN_JS_BUNDLE_START: + if (runJSBundleStartTime == 0) { + runJSBundleStartTime = now; + } + return; - if (markerId == ReactMarkerId::RUN_JS_BUNDLE_START || - markerId == ReactMarkerId::RUN_JS_BUNDLE_STOP) { - startupReactMarkers.push_back( - {markerId, tag, JSExecutor::performanceNow()}); - startupStopped = markerId == ReactMarkerId::RUN_JS_BUNDLE_STOP; + case ReactMarkerId::RUN_JS_BUNDLE_STOP: + if (runJSBundleEndTime == 0) { + runJSBundleEndTime = now; + } + return; + + default: + return; } } -std::vector StartupLogger::getStartupReactMarkers() { - return startupReactMarkers; +double StartupLogger::getAppStartTime() { + if (getAppStartTimeImpl == nullptr) { + return 0; + } + + return getAppStartTimeImpl(); +} + +double StartupLogger::getRunJSBundleStartTime() { + return runJSBundleStartTime; +} + +double StartupLogger::getRunJSBundleEndTime() { + return runJSBundleEndTime; } } // namespace ReactMarker diff --git a/ReactCommon/cxxreact/ReactMarker.h b/ReactCommon/cxxreact/ReactMarker.h index d30720715aa..e0640ae672c 100644 --- a/ReactCommon/cxxreact/ReactMarker.h +++ b/ReactCommon/cxxreact/ReactMarker.h @@ -7,8 +7,6 @@ #pragma once -#include - #ifdef __APPLE__ #include #endif @@ -74,16 +72,18 @@ class StartupLogger { public: static StartupLogger &getInstance(); - void logStartupEvent(const ReactMarkerId markerId, const char *tag); - std::vector getStartupReactMarkers(); + void logStartupEvent(const ReactMarker::ReactMarkerId markerId); + double getAppStartTime(); + double getRunJSBundleStartTime(); + double getRunJSBundleEndTime(); private: StartupLogger() = default; StartupLogger(const StartupLogger &) = delete; StartupLogger &operator=(const StartupLogger &) = delete; - bool startupStopped; - std::vector startupReactMarkers; + double runJSBundleStartTime; + double runJSBundleEndTime; }; } // namespace ReactMarker