Enabling Sampling Profiler for all apps via Dev Menu

Reviewed By: makovkastar

Differential Revision: D16141959

fbshipit-source-id: 3a9964961a6af4bc7d4650526031db564ec2dd27
This commit is contained in:
Ram N
2019-07-11 23:32:25 -07:00
committed by Facebook Github Bot
parent aa5edca0e2
commit 6c362a7b19
7 changed files with 104 additions and 7 deletions
@@ -292,9 +292,18 @@ public class ReactInstanceManager {
public @Nullable Activity getCurrentActivity() {
return ReactInstanceManager.this.mCurrentActivity;
}
@Override
public JavaScriptExecutorFactory getJavaScriptExecutorFactory() {
return ReactInstanceManager.this.getJSExecutorFactory();
}
};
}
private JavaScriptExecutorFactory getJSExecutorFactory() {
return mJavaScriptExecutorFactory;
}
public DevSupportManager getDevSupportManager() {
return mDevSupportManager;
}
@@ -24,6 +24,18 @@ public class JSCJavaScriptExecutorFactory implements JavaScriptExecutorFactory {
return new JSCJavaScriptExecutor(jscConfig);
}
@Override
public void startSamplingProfiler() {
throw new UnsupportedOperationException(
"Starting sampling profiler not supported on " + toString());
}
@Override
public void stopSamplingProfiler(String filename) {
throw new UnsupportedOperationException(
"Stopping sampling profiler not supported on " + toString());
}
@Override
public String toString() {
return "JSCExecutor";
@@ -8,4 +8,17 @@ package com.facebook.react.bridge;
public interface JavaScriptExecutorFactory {
JavaScriptExecutor create() throws Exception;
/**
* Starts the sampling profiler for this specific JavaScriptExecutor Sampling profiler is usually
* a singleton on the runtime, hence the method exists here and not in {@link JavaScriptExecutor}
*/
void startSamplingProfiler();
/**
* Stops the Sampling profile
*
* @param filename The filename where the results of the sampling profiler are dumped to
*/
void stopSamplingProfiler(String filename);
}
@@ -31,6 +31,18 @@ public class ProxyJavaScriptExecutor extends JavaScriptExecutor {
public JavaScriptExecutor create() throws Exception {
return new ProxyJavaScriptExecutor(mJavaJSExecutorFactory.create());
}
@Override
public void startSamplingProfiler() {
throw new UnsupportedOperationException(
"Starting sampling profiler not supported on " + toString());
}
@Override
public void stopSamplingProfiler(String filename) {
throw new UnsupportedOperationException(
"Stopping sampling profiler not supported on " + toString());
}
}
static {
@@ -29,6 +29,7 @@ import com.facebook.react.R;
import com.facebook.react.bridge.CatalystInstance;
import com.facebook.react.bridge.DefaultNativeModuleCallExceptionHandler;
import com.facebook.react.bridge.JavaJSExecutor;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
import com.facebook.react.bridge.NativeDeltaClient;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactMarker;
@@ -553,13 +554,48 @@ public class DevSupportManagerImpl
new DevOptionHandler() {
@Override
public void onOptionSelected() {
Intent intent =
new Intent(
mApplicationContext.getPackageName()
+ (mIsSamplingProfilerEnabled
? DISABLE_SAMPLING_PROFILER
: ENABLE_SAMPLING_PROFILER));
mApplicationContext.sendBroadcast(intent);
JavaScriptExecutorFactory javaScriptExecutorFactory =
mReactInstanceManagerHelper.getJavaScriptExecutorFactory();
if (!mIsSamplingProfilerEnabled) {
try {
javaScriptExecutorFactory.startSamplingProfiler();
Toast.makeText(
mApplicationContext, "Starting Sampling Profiler", Toast.LENGTH_SHORT)
.show();
} catch (UnsupportedOperationException e) {
Toast.makeText(
mApplicationContext,
javaScriptExecutorFactory.toString()
+ " does not support Sampling Profiler",
Toast.LENGTH_LONG)
.show();
}
} else {
try {
final String outputPath =
File.createTempFile(
"sampling-profiler-trace",
".cpuprofile",
mApplicationContext.getCacheDir())
.getPath();
javaScriptExecutorFactory.stopSamplingProfiler(outputPath);
Toast.makeText(
mApplicationContext,
"Saved results from Profiler to " + outputPath,
Toast.LENGTH_LONG)
.show();
} catch (IOException e) {
FLog.e(
ReactConstants.TAG,
"Could not create temporary file for saving results from Sampling Profiler");
} catch (UnsupportedOperationException e) {
Toast.makeText(
mApplicationContext,
javaScriptExecutorFactory.toString() + "does not support Sampling Profiler",
Toast.LENGTH_LONG)
.show();
}
}
mIsSamplingProfilerEnabled = !mIsSamplingProfilerEnabled;
}
});
@@ -9,6 +9,7 @@ package com.facebook.react.devsupport;
import android.app.Activity;
import androidx.annotation.Nullable;
import com.facebook.react.bridge.JavaJSExecutor;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
import com.facebook.react.bridge.NativeDeltaClient;
/**
@@ -29,4 +30,6 @@ public interface ReactInstanceManagerDevHelper {
/** Get reference to top level #{link Activity} attached to react context */
@Nullable
Activity getCurrentActivity();
JavaScriptExecutorFactory getJavaScriptExecutorFactory();
}
@@ -29,6 +29,18 @@ public class JSCExecutorFactory implements JavaScriptExecutorFactory {
return new JSCExecutor(jscConfig);
}
@Override
public void startSamplingProfiler() {
throw new UnsupportedOperationException(
"Starting sampling profiler not supported on " + toString());
}
@Override
public void stopSamplingProfiler(String filename) {
throw new UnsupportedOperationException(
"Stopping sampling profiler not supported on " + toString());
}
@Override
public String toString() {
return "JSIExecutor+JSCRuntime";