Factor out cpu profiler middleware into it's own file

Reviewed By: @amasad

Differential Revision: D2519562

fb-gh-sync-id: c2b2ba56072d75d7082740532b5d1959273c84f4
This commit is contained in:
Martín Bigio 2015-10-08 14:15:18 -07:00 committed by facebook-github-bot-4
parent 22e88f2069
commit 0a835b7994
2 changed files with 49 additions and 25 deletions

View File

@ -0,0 +1,47 @@
/**
* 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 execFile = require('child_process').execFile;
const fs = require('fs');
const path = require('path');
module.exports = function(req, res, next) {
if (req.url !== '/cpu-profile') {
next();
return;
}
console.log('Dumping CPU profile information...');
var dumpName = '/tmp/cpu-profile_' + Date.now();
fs.writeFileSync(dumpName + '.json', req.rawBody);
var cmdPath = path.join(
__dirname,
'../react-native-github/JSCLegacyProfiler/json2trace'
);
execFile(
cmdPath,
[
'-cpuprofiler',
dumpName + '.cpuprofile ' + dumpName + '.json'
],
function(error) {
if (error) {
console.error(error);
res.end('Unknown error: ' + error.message);
} else {
var response = 'Your profile was generated at\n\n' + dumpName + '.cpuprofile\n\n' +
'Open `Chrome Dev Tools > Profiles > Load` and select the profile to visualize it.';
console.log(response);
res.end(response);
}
}
);
};

View File

@ -17,6 +17,7 @@ const isAbsolutePath = require('absolute-path');
const blacklist = require('./blacklist.js');
const chalk = require('chalk');
const checkNodeVersion = require('./checkNodeVersion');
const cpuProfilerMiddleware = require('./cpuProfilerMiddleware');
const connect = require('connect');
const formatBanner = require('./formatBanner');
const getDevToolsMiddleware = require('./getDevToolsMiddleware');
@ -169,30 +170,6 @@ function loadRawBody(req, res, next) {
});
}
function cpuProfileMiddleware(req, res, next) {
if (req.url !== '/cpu-profile') {
next();
return;
}
console.log('Dumping CPU profile information...');
var dumpName = '/tmp/cpu-profile_' + Date.now();
fs.writeFileSync(dumpName + '.json', req.rawBody);
var cmd = path.join(__dirname, '..', 'react-native-github', 'JSCLegacyProfiler', 'json2trace') + ' -cpuprofiler ' + dumpName + '.cpuprofile ' + dumpName + '.json';
childProcess.exec(cmd, function(error) {
if (error) {
console.error(error);
res.end('Unknown error: ' + error.message);
} else {
var response = 'Your profile was generated at\n\n' + dumpName + '.cpuprofile\n\n' +
'Open `Chrome Dev Tools > Profiles > Load` and select the profile to visualize it.';
console.log(response);
res.end(response);
}
});
}
function getAppMiddleware(options) {
var transformerPath = options.transformer;
if (!isAbsolutePath(transformerPath)) {
@ -226,7 +203,7 @@ function runServer(
.use(openStackFrameInEditorMiddleware)
.use(statusPageMiddleware)
.use(systraceProfileMiddleware)
.use(cpuProfileMiddleware)
.use(cpuProfilerMiddleware)
// Temporarily disable flow check until it's more stable
//.use(getFlowTypeCheckMiddleware(options))
.use(getAppMiddleware(options));