diff --git a/cpuProfilerMiddleware.js b/cpuProfilerMiddleware.js new file mode 100644 index 00000000..2e31cc50 --- /dev/null +++ b/cpuProfilerMiddleware.js @@ -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); + } + } + ); +}; diff --git a/packager.js b/packager.js index bd9ae8ee..77bef9e4 100644 --- a/packager.js +++ b/packager.js @@ -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));