Skip to content

Commit

Permalink
#15.1 Extend packager server to receive signals from Sampling Profiler
Browse files Browse the repository at this point in the history
Reviewed By: bestander

Differential Revision: D3606098

fbshipit-source-id: ec55030dd1b3a27f0595650da1ce01fe1ac9116c
  • Loading branch information
lukaspiatkowski authored and Facebook Github Bot 8 committed Aug 2, 2016
1 parent ec0ccf5 commit 2231b21
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.view.WindowManager;
import android.widget.Toast;

Expand All @@ -50,6 +52,11 @@
import com.facebook.react.devsupport.StackTraceHelper.StackFrame;
import com.facebook.react.modules.debug.DeveloperSettings;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
* Interface for accessing and interacting with development features. Following features
* are supported through this manager class:
Expand Down Expand Up @@ -114,6 +121,40 @@ private static enum ErrorType {
private int mLastErrorCookie = 0;
private @Nullable ErrorType mLastErrorType;


private static class JscProfileTask extends AsyncTask<String, Void, Void> {
private static final MediaType JSON =
MediaType.parse("application/json; charset=utf-8");

private final String mSourceUrl;

private JscProfileTask(String sourceUrl) {
mSourceUrl = sourceUrl;
}

@Override
protected Void doInBackground(String... jsonData) {
try {
String jscProfileUrl =
Uri.parse(mSourceUrl).buildUpon()
.path("/jsc-profile")
.query(null)
.build()
.toString();
OkHttpClient client = new OkHttpClient();
for (String json: jsonData) {
RequestBody body = RequestBody.create(JSON, json);
Request request =
new Request.Builder().url(jscProfileUrl).post(body).build();
client.newCall(request).execute();
}
} catch (IOException e) {
FLog.e(ReactConstants.TAG, "Failed not talk to server", e);
}
return null;
}
}

public DevSupportManagerImpl(
Context applicationContext,
ReactInstanceDevCommandsHandler reactInstanceCommandsHandler,
Expand Down Expand Up @@ -389,6 +430,11 @@ public void onOptionSelected() {
? "Started JSC Sampling Profiler"
: "Stopped JSC Sampling Profiler",
Toast.LENGTH_LONG).show();
if (result != null) {
new JscProfileTask(getSourceUrl()).executeOnExecutor(
AsyncTask.THREAD_POOL_EXECUTOR,
result);
}
}
} catch (JSCSamplingProfiler.ProfilerException e) {
showNewJavaError(e.getMessage(), e);
Expand Down
36 changes: 36 additions & 0 deletions local-cli/server/middleware/jscProfilerMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

const fs = require('fs');

module.exports = function(req, res, next) {
if (req.url !== '/jsc-profile') {
next();
return;
}

console.log('Dumping JSC profile information...');
const dumpName = '/tmp/jsc-profile_' + Date.now() + '.cpuprofile';
fs.writeFile(dumpName, req.rawBody, (err) => {
var response = '';
if (err) {
response =
'An error occured when trying to save the profile at ' + dumpName;
console.error(response, err);
} else {
response =
'Your profile was generated at\n\n' + dumpName + '\n\n' +
'Open `Chrome Dev Tools > Profiles > Load` '
+ 'and select the profile to visualize it.';
console.log(response);
}
res.end(response);
});
};
2 changes: 2 additions & 0 deletions local-cli/server/runServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const connect = require('connect');
const cpuProfilerMiddleware = require('./middleware/cpuProfilerMiddleware');
const getDevToolsMiddleware = require('./middleware/getDevToolsMiddleware');
const http = require('http');
const jscProfilerMiddleware = require('./middleware/jscProfilerMiddleware');
const loadRawBodyMiddleware = require('./middleware/loadRawBodyMiddleware');
const messageSocket = require('./util/messageSocket.js');
const openStackFrameInEditorMiddleware = require('./middleware/openStackFrameInEditorMiddleware');
Expand Down Expand Up @@ -41,6 +42,7 @@ function runServer(args, config, readyCallback) {
.use(systraceProfileMiddleware)
.use(heapCaptureMiddleware)
.use(cpuProfilerMiddleware)
.use(jscProfilerMiddleware)
.use(indexPageMiddleware)
.use(packagerServer.processRequest.bind(packagerServer));

Expand Down

0 comments on commit 2231b21

Please sign in to comment.