react-native/local-cli/server/middleware/cpuProfilerMiddleware.js
Martín Bigio bcf762af60 Move files from packager/ to local-cli
Summary: public

This is not only to put the files on a place where it makes more sense but also to allow to use ES6 features on them as `/packager` is not whitelisted on `babel`.

Reviewed By: mkonicek

Differential Revision: D2577267

fb-gh-sync-id: b69a17c0aad349a3eda987e33d1778d97a8e1549
2015-10-26 15:38:14 -07:00

48 lines
1.3 KiB
JavaScript

/**
* 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);
}
}
);
};