Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 20ae4fbd2fd981649b7eb0c46db038bfd8707cce @mape committed Aug 1, 2010
Showing with 104 additions and 0 deletions.
  1. +20 −0 LICENSE
  2. +14 −0 README.md
  3. +62 −0 bin/nodeprofile
  4. +8 −0 package.json
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Mathias Pettersson, mape@mape.me
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14 README.md
@@ -0,0 +1,14 @@
+# node-profile
+
+A script that makes profiling node.js apps a little easier.
+
+## Usage
+`> nodeprofile yourapp.js`
+
+## "Dependencies"
+* python
+* That the `locate` command can find a copy of `linux-tick-processor.py` (bundled with node.js)
+
+## Output
+
+<img src="http://mape.me/nodeprofile.png" alt="">
62 bin/nodeprofile
@@ -0,0 +1,62 @@
+#!/usr/bin/env node
+
+var exec=require('child_process').exec
+, fs = require('fs')
+
+process.addListener('SIGINT', function () {});
+
+var logName = 'profile-'+Date.now()+Math.floor(Math.random()*10000)+'.log';
+
+var command = 'node --prof --logfile='+logName+' '+process.ARGV[2];
+
+console.log('Running: '+command);
+
+exec(command, function (error, stdout, stderr) {
+ exec('locate linux-tick-processor.py', function(err, stdout, stderr) {
+ if (err) throw err;
+
+ var tickLocations = stdout.trim().split('\n');
+ if (tickLocations[0]) {
+ exec('python '+tickLocations[0]+' '+logName, function(err, stdout, stderr) {
+ console.log(stderr);
+ if (!err) {
+ console.log(colorFormat(stdout));
+ } else {
+ console.log(style(31, '\nCould not generate profile.'));
+ }
+
+ fs.unlink(logName, function (err) {
+ if (err) throw err;
+ console.log('Successfully deleted temporary: '+logName);
+ });
+ });
+ } else {
+ throw new Error('Could not find linux-tick-processor.py');
+ }
+ });
+});
+
+function colorFormat(profileLog)
+{
+ var lines = profileLog.split('\n');
+ var newLog = [];
+
+ lines.forEach(function(line) {
+ if (line.match(/ *\[.*\]/)) {
+ newLog.push(style(33, line));
+ } else if (line.match(/^[^ ]/)) {
+ newLog.push(style(37, line));
+ } else if (line.match(/^ [^ ]/)) {
+ newLog.push(style(35, line));
+ } else {
+ newLog.push(line.replace(/ *([^ ]+)/, function(match){return style(31, match)}).replace(/([0-9]+\.[0-9]+%)/, function(match){return style(33, match)}));
+ }
+ });
+
+ return newLog.join('\n');
+}
+
+function style(colorCode, str)
+{
+ return '\x1B[1m\x1B['+colorCode+'m'+str+'\x1B[0m';
+}
8 package.json
@@ -0,0 +1,8 @@
+{
+ "name": "profile",
+ "description": "A script that makes profiling node.js apps easier.",
+ "version": "0.0.1",
+ "author" : "Mathias Pettersson <mape@mape.me>",
+ "bin": { "nodeprofile": "./bin/nodeprofile" },
+ "engines": { "node": ">= 0.1.100" }
+}

0 comments on commit 20ae4fb

Please sign in to comment.