Skip to content

Commit c04cb3b

Browse files
lukaspiatkowskiMorgan Pretty
authored and
Morgan Pretty
committed
facebook#15.1 Extend packager server to receive signals from Sampling Profiler
Reviewed By: bestander Differential Revision: D3606098 fbshipit-source-id: ec55030dd1b3a27f0595650da1ce01fe1ac9116c
1 parent 6ed6a7b commit c04cb3b

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerImpl.java

+46
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import android.content.pm.PackageInfo;
3232
import android.content.pm.PackageManager;
3333
import android.hardware.SensorManager;
34+
import android.net.Uri;
35+
import android.os.AsyncTask;
3436
import android.view.WindowManager;
3537
import android.widget.Toast;
3638

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

55+
import okhttp3.MediaType;
56+
import okhttp3.OkHttpClient;
57+
import okhttp3.Request;
58+
import okhttp3.RequestBody;
59+
5360
/**
5461
* Interface for accessing and interacting with development features. Following features
5562
* are supported through this manager class:
@@ -114,6 +121,40 @@ private static enum ErrorType {
114121
private int mLastErrorCookie = 0;
115122
private @Nullable ErrorType mLastErrorType;
116123

124+
125+
private static class JscProfileTask extends AsyncTask<String, Void, Void> {
126+
private static final MediaType JSON =
127+
MediaType.parse("application/json; charset=utf-8");
128+
129+
private final String mSourceUrl;
130+
131+
private JscProfileTask(String sourceUrl) {
132+
mSourceUrl = sourceUrl;
133+
}
134+
135+
@Override
136+
protected Void doInBackground(String... jsonData) {
137+
try {
138+
String jscProfileUrl =
139+
Uri.parse(mSourceUrl).buildUpon()
140+
.path("/jsc-profile")
141+
.query(null)
142+
.build()
143+
.toString();
144+
OkHttpClient client = new OkHttpClient();
145+
for (String json: jsonData) {
146+
RequestBody body = RequestBody.create(JSON, json);
147+
Request request =
148+
new Request.Builder().url(jscProfileUrl).post(body).build();
149+
client.newCall(request).execute();
150+
}
151+
} catch (IOException e) {
152+
FLog.e(ReactConstants.TAG, "Failed not talk to server", e);
153+
}
154+
return null;
155+
}
156+
}
157+
117158
public DevSupportManagerImpl(
118159
Context applicationContext,
119160
ReactInstanceDevCommandsHandler reactInstanceCommandsHandler,
@@ -389,6 +430,11 @@ public void onOptionSelected() {
389430
? "Started JSC Sampling Profiler"
390431
: "Stopped JSC Sampling Profiler",
391432
Toast.LENGTH_LONG).show();
433+
if (result != null) {
434+
new JscProfileTask(getSourceUrl()).executeOnExecutor(
435+
AsyncTask.THREAD_POOL_EXECUTOR,
436+
result);
437+
}
392438
}
393439
} catch (JSCSamplingProfiler.ProfilerException e) {
394440
showNewJavaError(e.getMessage(), e);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
'use strict';
10+
11+
const fs = require('fs');
12+
13+
module.exports = function(req, res, next) {
14+
if (req.url !== '/jsc-profile') {
15+
next();
16+
return;
17+
}
18+
19+
console.log('Dumping JSC profile information...');
20+
const dumpName = '/tmp/jsc-profile_' + Date.now() + '.cpuprofile';
21+
fs.writeFile(dumpName, req.rawBody, (err) => {
22+
var response = '';
23+
if (err) {
24+
response =
25+
'An error occured when trying to save the profile at ' + dumpName;
26+
console.error(response, err);
27+
} else {
28+
response =
29+
'Your profile was generated at\n\n' + dumpName + '\n\n' +
30+
'Open `Chrome Dev Tools > Profiles > Load` '
31+
+ 'and select the profile to visualize it.';
32+
console.log(response);
33+
}
34+
res.end(response);
35+
});
36+
};

local-cli/server/runServer.js

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const connect = require('connect');
1313
const cpuProfilerMiddleware = require('./middleware/cpuProfilerMiddleware');
1414
const getDevToolsMiddleware = require('./middleware/getDevToolsMiddleware');
1515
const http = require('http');
16+
const jscProfilerMiddleware = require('./middleware/jscProfilerMiddleware');
1617
const loadRawBodyMiddleware = require('./middleware/loadRawBodyMiddleware');
1718
const messageSocket = require('./util/messageSocket.js');
1819
const openStackFrameInEditorMiddleware = require('./middleware/openStackFrameInEditorMiddleware');
@@ -41,6 +42,7 @@ function runServer(args, config, readyCallback) {
4142
.use(systraceProfileMiddleware)
4243
.use(heapCaptureMiddleware)
4344
.use(cpuProfilerMiddleware)
45+
.use(jscProfilerMiddleware)
4446
.use(indexPageMiddleware)
4547
.use(packagerServer.processRequest.bind(packagerServer));
4648

0 commit comments

Comments
 (0)