Unify oss and internal version of systrace profiler middleware

Reviewed By: @amasad

Differential Revision: D2519518

fb-gh-sync-id: 6682f80447e2b62fcf6b137412ca8ddbe03ecb09
This commit is contained in:
Martín Bigio 2015-10-08 14:15:17 -07:00 committed by facebook-github-bot-4
parent f980c33073
commit 22e88f2069
2 changed files with 53 additions and 38 deletions

View File

@ -24,6 +24,7 @@ const openStackFrameInEditorMiddleware = require('./openStackFrameInEditorMiddle
const parseCommandLine = require('./parseCommandLine.js');
const ReactPackager = require('./react-packager');
const statusPageMiddleware = require('./statusPageMiddleware.js');
const systraceProfileMiddleware = require('./systraceProfileMiddleware.js');
const webSocketProxy = require('./webSocketProxy.js');
var options = parseCommandLine([{
@ -168,44 +169,6 @@ function loadRawBody(req, res, next) {
});
}
function systraceProfileMiddleware(req, res, next) {
if (req.url !== '/systrace') {
next();
return;
}
console.log('Dumping profile information...');
var dumpName = '/tmp/dump_' + Date.now() + '.json';
var prefix = process.env.TRACE_VIEWER_PATH || '';
var cmd = path.join(prefix, 'trace2html') + ' ' + dumpName;
fs.writeFileSync(dumpName, req.rawBody);
childProcess.exec(cmd, function(error) {
if (error) {
if (error.code === 127) {
var response = '\n** Failed executing `' + cmd + '` **\n\n' +
'Google trace-viewer is required to visualize the data, You can install it with `brew install trace2html`\n\n' +
'NOTE: Your profile data was kept at:\n' + dumpName
console.log(response);
res.end(response);
} else {
console.error(error);
res.end('Unknown error: ' + error.message);
}
return;
} else {
childProcess.exec('rm ' + dumpName);
childProcess.exec('open ' + dumpName.replace(/json$/, 'html'), function(err) {
if (err) {
console.error(err);
res.end(err.message);
} else {
res.end();
}
});
}
});
}
function cpuProfileMiddleware(req, res, next) {
if (req.url !== '/cpu-profile') {
next();

View File

@ -0,0 +1,52 @@
/**
* 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 exec = require('child_process').exec;
const fs = require('fs');
const path = require('path');
module.exports = function(req, res, next) {
if (req.url !== '/systrace') {
next();
return;
}
console.log('Dumping profile information...');
var dumpName = '/tmp/dump_' + Date.now() + '.json';
var prefix = process.env.TRACE_VIEWER_PATH || '';
var cmd = path.join(prefix, 'trace2html') + ' ' + dumpName;
fs.writeFileSync(dumpName, req.rawBody);
exec(cmd, function(error) {
if (error) {
if (error.code === 127) {
var response = '\n** Failed executing `' + cmd + '` **\n\n' +
'Google trace-viewer is required to visualize the data, ' +
'You can install it with `brew install trace2html`\n\n' +
'NOTE: Your profile data was kept at:\n' + dumpName;
console.log(response);
res.end(response);
} else {
console.error(error);
res.end('Unknown error: ' + error.message);
}
return;
} else {
exec('rm ' + dumpName);
exec('open ' + dumpName.replace(/json$/, 'html'), function(err) {
if (err) {
console.error(err);
res.end(err.message);
} else {
res.end();
}
});
}
});
};