From a9f60bea729b0ec41ec0cffe158d3f7a1c522e36 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Thu, 19 Dec 2024 14:51:26 -0800 Subject: [PATCH] Remove unused perftests JNI code (#48344) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48344 Matching Java code was removed in D60581611 Changelog: [Internal] Reviewed By: rshest Differential Revision: D67460124 fbshipit-source-id: 1c7e986fbb0c02af51e0c4035b2f131fcc2c64ec --- .../src/main/jni/react/perftests/OnLoad.cpp | 243 ------------------ 1 file changed, 243 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/jni/react/perftests/OnLoad.cpp diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/perftests/OnLoad.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/perftests/OnLoad.cpp deleted file mode 100644 index bd5341c1e1d..00000000000 --- a/packages/react-native/ReactAndroid/src/main/jni/react/perftests/OnLoad.cpp +++ /dev/null @@ -1,243 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include -#include -#include -#include - -#include -#include - -namespace facebook::react { - -using facebook::jni::alias_ref; - -namespace { - -// This is a wrapper around the Java proxy to the javascript module. This -// allows us to call functions on the js module from c++. Are you seeing -// crashes in this class? Android 6+ crashes when you try to call a -// method on a Proxy. Switch to an older version of Android. If you're -// really desperate, you can fix this by using ToReflectedMethod on the -// underlying jmethodid and invoking that. -class JavaJSModule : public jni::JavaClass { - public: - static constexpr auto kJavaDescriptor = - "Lcom/facebook/react/CatalystBridgeBenchmarks$BridgeBenchmarkModule;"; - - static void bounceCxx(alias_ref obj, int iters) { - static auto method = javaClassLocal()->getMethod("bounceCxx"); - method(obj, iters); - } - - static void bounceArgsCxx( - alias_ref obj, - int iters, - int a, - int b, - double x, - double y, - const std::string& s, - const std::string& t) { - static auto method = - javaClassLocal() - ->getMethod( - "bounceArgsCxx"); - method( - obj, - iters, - a, - b, - x, - y, - jni::make_jstring(s).get(), - jni::make_jstring(t).get()); - } -}; - -// This is just the test instance itself. Used only to countdown the latch. -class CatalystBridgeBenchmarks - : public jni::JavaClass { - public: - static constexpr auto kJavaDescriptor = - "Lcom/facebook/react/CatalystBridgeBenchmarks;"; - - static void countDown(alias_ref obj) { - static auto method = javaClassLocal()->getMethod("countDown"); - method(obj); - } -}; - -// This is the shared data for two cxx bounce threads. -struct Data { - std::mutex m; - std::condition_variable cv; - bool leftActive; - Data() : leftActive(true) {} -}; -Data data; - -void runBounce(jni::alias_ref, bool isLeft, int iters) { - for (int i = 0; i < iters; i++) { - std::unique_lock lk(data.m); - data.cv.wait(lk, [&] { return data.leftActive == isLeft; }); - data.leftActive = !isLeft; - data.cv.notify_one(); - } -} - -static jni::global_ref jsModule; -static jni::global_ref javaTestInstance; - -class CxxBenchmarkModule : public xplat::module::CxxModule { - public: - virtual std::string getName() override { - return "CxxBenchmarkModule"; - } - - virtual auto getConstants() - -> std::map override { - return std::map(); - } - - virtual auto getMethods() -> std::vector override { - return std::vector{ - Method( - "bounce", - [this](folly::dynamic args) { - this->bounce(xplat::jsArgAsInt(args, 0)); - }), - Method( - "bounceArgs", - [this](folly::dynamic args) { - this->bounceArgs( - xplat::jsArgAsInt(args, 0), - xplat::jsArgAsInt(args, 1), - xplat::jsArgAsInt(args, 2), - xplat::jsArgAsDouble(args, 3), - xplat::jsArgAsDouble(args, 4), - xplat::jsArgAsString(args, 5), - xplat::jsArgAsString(args, 6)); - }), - }; - } - - void bounce(int iters) { - if (iters == 0) { - CatalystBridgeBenchmarks::countDown(javaTestInstance); - } else { - JavaJSModule::bounceCxx(jsModule, iters - 1); - } - } - - void bounceArgs( - int iters, - int a, - int b, - double x, - double y, - const std::string& s, - const std::string& t) { - if (iters == 0) { - CatalystBridgeBenchmarks::countDown(javaTestInstance); - } else { - JavaJSModule::bounceArgsCxx(jsModule, iters - 1, a, b, x, y, s, t); - } - } -}; - -void setUp( - alias_ref obj, - alias_ref mod) { - javaTestInstance = jni::make_global(obj); - jsModule = jni::make_global(mod); -} - -void tearDown(alias_ref) { - javaTestInstance.reset(); - jsModule.reset(); -} - -namespace logwatcher { - -static std::string gMessageToLookFor; -static int gMessagePriorityToLookFor; -static bool gHasSeenMessage = false; - -/** - * NB: Don't put JNI logic (or anything else that could trigger a log) here! - */ -static void stubLogHandler(int pri, const char* tag, const char* msg) { - if (gMessageToLookFor.empty()) { - return; - } - - bool priorityMatches = pri == gMessagePriorityToLookFor; - bool substringFound = strstr(msg, gMessageToLookFor.c_str()) != NULL; - gHasSeenMessage |= priorityMatches && substringFound; -} - -static jboolean hasSeenExpectedLogMessage(JNIEnv*, jclass) { - return gHasSeenMessage ? JNI_TRUE : JNI_FALSE; -} - -static void stopWatchingLogMessages(JNIEnv*, jclass) { - gMessageToLookFor = ""; - gHasSeenMessage = false; - setLogHandler(NULL); -} - -static void startWatchingForLogMessage( - JNIEnv* env, - jclass loggerClass, - jstring jmsg, - jint priority) { - stopWatchingLogMessages(env, loggerClass); - gMessageToLookFor = jni::wrap_alias(jmsg)->toStdString(); - gMessagePriorityToLookFor = priority; - setLogHandler(&stubLogHandler); -} - -} // namespace logwatcher -} // namespace -} // namespace facebook::react - -using namespace facebook::react; - -extern "C" facebook::xplat::module::CxxModule* CxxBenchmarkModule() { - return new facebook::react::CxxBenchmarkModule(); -} - -extern "C" jint JNI_OnLoad(JavaVM* vm, void*) { - return facebook::jni::initialize(vm, [] { - facebook::jni::registerNatives( - "com/facebook/catalyst/testing/LogWatcher", - { - makeNativeMethod( - "startWatchingForLogMessage", - "(Ljava/lang/String;I)V", - logwatcher::startWatchingForLogMessage), - makeNativeMethod( - "stopWatchingLogMessages", - "()V", - logwatcher::stopWatchingLogMessages), - makeNativeMethod( - "hasSeenExpectedLogMessage", - "()Z", - logwatcher::hasSeenExpectedLogMessage), - }); - facebook::jni::registerNatives( - "com/facebook/react/CatalystBridgeBenchmarks", - { - makeNativeMethod("runNativeBounce", runBounce), - makeNativeMethod("nativeSetUp", setUp), - makeNativeMethod("nativeTearDown", tearDown), - }); - }); -}