Permalink
Browse files

initial commit

  • Loading branch information...
1 parent 6acdc11 commit b98984a1191a14acf26cac39bf892738fba58a92 Alan Clarke committed Jul 31, 2012
Showing with 276 additions and 3 deletions.
  1. +22 −0 LICENSE-MIT
  2. +37 −3 README.md
  3. +39 −0 grunt.js
  4. +65 −0 lib/speedsnitch.js
  5. +35 −0 package.json
  6. +78 −0 test/speedsnitch_test.js
View
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Alan Clarke
+
+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.
View
@@ -1,4 +1,38 @@
-speedsnitch
-===========
+# speedsnitch
-simple lightweight nodejs profiling tool
+simple lightweight nodejs profiling tool
+
+## Getting Started
+Install the module with: `npm install speedsnitch`
+
+```javascript
+var Snitch = require('speedsnitch');
+var snitch = new Snitch(); // "awesome"
+
+
+
+snitch.next('lets see how fast some code runs');
+/*some code*/
+
+snitch.next('testing some other code');
+/*some other code*/
+
+snitch.stop();
+/* break */
+
+
+snitch.next('lets test last bit of code');
+/*last bit of code code*/
+
+
+console.log(snitch.summarize());
+//gives you array of json objects with time in ms, description and line numbers, ordered by most expensive bit of code
+
+```
+
+## Documentation
+there's not much else to it really!
+
+## License
+Copyright (c) 2012 Alan Clarke
+Licensed under the MIT license.
View
@@ -0,0 +1,39 @@
+module.exports = function(grunt) {
+
+ // Project configuration.
+ grunt.initConfig({
+ pkg: '<json:package.json>',
+ test: {
+ files: ['test/**/*.js']
+ },
+ lint: {
+ files: ['grunt.js', 'lib/**/*.js', 'test/**/*.js']
+ },
+ watch: {
+ files: '<config:lint.files>',
+ tasks: 'default'
+ },
+ jshint: {
+ options: {
+ curly: true,
+ eqeqeq: true,
+ immed: true,
+ latedef: true,
+ newcap: true,
+ noarg: true,
+ sub: true,
+ undef: true,
+ boss: true,
+ eqnull: true,
+ node: true
+ },
+ globals: {
+ exports: true
+ }
+ }
+ });
+
+ // Default task.
+ grunt.registerTask('default', 'lint test');
+
+};
View
@@ -0,0 +1,65 @@
+/*
+ * ghetto-profiler
+ *
+ *
+ * Copyright (c) 2012 Alan Clarke
+ * Licensed under the MIT license.
+ */
+
+var Snitch = function(log){
+ this.opts = { log:log };
+ this.data = [];
+};
+
+Snitch.prototype = {
+ next: function(name){
+ var s = this;
+ var last = s.stop();
+ s.data.push({ name:name, start:s._capture()});
+ return last;
+ },
+ stop: function(){
+ var s = this;
+ if(s.data.length){
+ var last = s.data[s.data.length-1];
+ if(!last.stop){
+ last.stop = s._capture();
+ last.ms = last.stop.time- last.start.time;
+
+ if(s.opts.log){
+ console.log(last);
+ }
+ return last;
+ }
+ }
+ },
+ _capture:function(){
+ var e = new Error();
+ var index = 3;
+ var callee_stack = e.stack.split('\n');
+ for(var i =0;i<callee_stack.length;i++){
+ if(callee_stack[i].indexOf(__filename.replace(/^.*\//,''))>=0){
+ index = i+1;
+ }
+ }
+ callee_stack = callee_stack[index].split(':');
+ return { time: new Date().valueOf(), file:callee_stack[0].replace(/^.*\//,''), line:callee_stack[1] };
+ },
+ summarize: function(){
+ var s = this;
+ var last = s.stop();
+ var data = s.data.sort(function(a,b){ return b.ms - a.ms; });
+ if(s.opts.log){
+ console.log(data);
+ }
+ return data;
+ },
+ summarise: function(){
+ return this.summarize();
+ }
+};
+
+module.exports = Snitch;
+
+
+
View
@@ -0,0 +1,35 @@
+{
+ "name": "speedsnitch",
+ "description": "simple lightweight nodejs profiling tool",
+ "version": "0.1.0",
+ "homepage": "https://github.com/alanclarke/speedsnitch",
+ "author": {
+ "name": "Alan Clarke",
+ "email": "self@alz.so",
+ "url": "alz.so"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git@github.com:alanclarke/speedsnitch.git"
+ },
+ "bugs": {
+ "url": "https://github.com/alanclarke/speedsnitch/issues"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "https://github.com/alanclarke/speedsnitch/blob/master/LICENSE-MIT"
+ }
+ ],
+ "main": "lib/speedsnitch",
+ "engines": {
+ "node": ">= 0.6.0"
+ },
+ "scripts": {
+ "test": "grunt test"
+ },
+ "devDependencies": {
+ "grunt": "~0.3.11"
+ },
+ "keywords": []
+}
View
@@ -0,0 +1,78 @@
+var snitch = require('../lib/speedsnitch.js');
+
+/*
+ ======== A Handy Little Nodeunit Reference ========
+ https://github.com/caolan/nodeunit
+
+ Test methods:
+ test.expect(numAssertions)
+ test.done()
+ Test assertions:
+ test.ok(value, [message])
+ test.equal(actual, expected, [message])
+ test.notEqual(actual, expected, [message])
+ test.deepEqual(actual, expected, [message])
+ test.notDeepEqual(actual, expected, [message])
+ test.strictEqual(actual, expected, [message])
+ test.notStrictEqual(actual, expected, [message])
+ test.throws(block, [error], [message])
+ test.doesNotThrow(block, [error], [message])
+ test.ifError(value)
+*/
+
+
+exports['speedsnitch'] = {
+ setUp: function(done) {
+ // setup here
+ done();
+ },
+ 'exists': function(test) {
+ test.expect(1);
+ test.ok(snitch, 'snitch should not be null');
+ test.done();
+ },
+ 'methods':function(test){
+ var methods = 'next stop summarise'.split(' ');
+ test.expect(methods.length||3);
+ var p = new snitch();
+ methods.forEach(function(m){
+ test.ok(p[m], 'snitch should have method '+m);
+ });
+ test.done();
+ },
+ 'flow':function(test){
+ test.expect(16);
+ var p = new snitch();
+ test.ok(!p.stop(), 'stop should return null value as no items have been added');
+ p.next('test');
+ setTimeout(function(){
+ var c = p.stop();
+ test.ok(c.name==='test','stop should return object with name equal to test');
+ test.ok(c.start.time,'stop should return object with date as start prop');
+ test.ok(c.stop.time,'stop should return object with date as stop prop');
+
+ test.ok(c.start.file===__filename.replace(/^.*\//,''),'start should reference this file as filename');
+ test.ok(c.stop.file===__filename.replace(/^.*\//,''),'stop should reference this file as filename');
+ test.ok(typeof c.ms === 'number' && c.ms <1000 && c.ms > 0,'stop should return object with ms taken in ms');
+ var sum = p.summarise();
+
+ test.ok(sum.length===1,'summarise should return an array length 1');
+ test.ok(sum[0].name===c.name&&sum[0].ms===c.ms,'summarise item should be same as test item')
+
+ p.next('test2');
+ setTimeout(function(){
+ var c2 = p.stop();
+ test.ok(c2.name==='test2','stop should return object with name equal to test');
+ test.ok(c2.start.time,'stop should return object with date as start prop');
+ test.ok(c2.stop.time,'stop should return object with date as start prop');
+ test.ok(typeof c2.ms === 'number' && c2.ms <1000 && c2.ms > 0,'stop should return object with ms taken in ms');
+ var sum = p.summarise();
+ console.log(sum);
+ test.ok(sum.length===2,'summarise should return an array length 2');
+ test.ok(sum[0].name===c2.name&&sum[0].ms===c2.ms,'summarise item should be same as test item')
+ test.ok(sum[1].name===c.name&&sum[1].ms===c.ms,'summarise last item should be fastest item')
+ test.done();
+ },10);
+ },1);
+ }
+};

0 comments on commit b98984a

Please sign in to comment.