Skip to content

Commit

Permalink
Enabling Sampling Profiler for all apps via Dev Menu
Browse files Browse the repository at this point in the history
Reviewed By: makovkastar

Differential Revision: D16141959

fbshipit-source-id: 3a9964961a6af4bc7d4650526031db564ec2dd27
  • Loading branch information
axe-fb authored and facebook-github-bot committed Jul 12, 2019
1 parent aa5edca commit 6c362a7
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,18 @@ public void toggleElementInspector() {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ public JavaScriptExecutor create() throws Exception {
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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,17 @@

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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ public Factory(JavaJSExecutor.Factory javaJSExecutorFactory) {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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;
Expand Down Expand Up @@ -553,13 +554,48 @@ public void onOptionSelected() {
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;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;

/**
Expand All @@ -29,4 +30,6 @@ public interface ReactInstanceManagerDevHelper {
/** Get reference to top level #{link Activity} attached to react context */
@Nullable
Activity getCurrentActivity();

JavaScriptExecutorFactory getJavaScriptExecutorFactory();
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ public JavaScriptExecutor create() throws Exception {
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";
Expand Down

0 comments on commit 6c362a7

Please sign in to comment.