Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit b1e127c62110555cf98c44430641e49421c25837 @3rd-Eden 3rd-Eden committed Jul 11, 2012
Showing with 166 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +6 −0 Makefile
  3. +38 −0 lib/processes.js
  4. +47 −0 lib/ticks.js
  5. +26 −0 package.json
  6. +17 −0 test/common.js
  7. +3 −0 test/mocha.opts
  8. +28 −0 test/ticks.test.js
@@ -0,0 +1 @@
+node_modules
@@ -0,0 +1,6 @@
+ALL_TESTS = $(shell find test -name '*.test.js')
+
+test:
+ @./node_modules/.bin/mocha $(ALL_TESTS)
+
+.PHONY: test
@@ -0,0 +1,38 @@
+"use strict";
+
+/**
+ * Diagnostics about the current running processes in the event loop.
+ *
+ * This file provides diagnostics about the current running processes and
+ * handles in the event loop. This file should provide the following intel:
+ *
+ * - Determing the different type's of handles
+ * - Count's of the different handles
+ * - Active request counts etc
+ *
+ * @copyright (c) 2012 observe.it (observe.it) <opensource@observe.it>
+ * MIT Licensed
+ */
+
+var net = require('net')
+ , http = require('http');
+
+/**
+ * Detect the instance of a handle.
+ *
+ * @param {Mixed} instance
+ * @returns {String}
+ * @api private
+ */
+
+function type(instance) {
+ if (instance instanceof net.Socket) {
+ if ('incomming' in instance) return 'http.incomingmessage';
+ return 'net.socket';
+ }
+
+ if (instance instanceof net.Server) return 'net.server';
+ if (instance instanceof http.Server) return 'http.server';
+ if (instance instanceof http.IncomingMessage) return 'http.incomingmessage';
+ if (instance instanceof http.OutgoingMessage) return 'http.outgoingmessage';
+}
@@ -0,0 +1,47 @@
+"use strict";
+
+/**
+ * Get the duration of a single tick.
+ *
+ * @TODO use process.hrtime() instead
+ * @TODO get the overhead of this call and substract it from the time
+ * @param {Function} fn
+ * @api private
+ */
+
+exports.tickDuration = function tickDuration(fn) {
+ process.nextTick(function tick() {
+ var start = Date.now();
+
+ process.nextTick(function tok() {
+ fn(Date.now() - start);
+ });
+ });
+};
+
+/**
+ * Get the amount of ticks occures within on a timespan.
+ *
+ * @TODO setTimeout's are not accurate to the milisecond so we should really
+ * check a Date.now() or a process.hrtime() on each nextTick call to see if we
+ * didn't blow passed our timeout.
+ * @param {Number} duration the amount of seconds we want to count
+ * @param {Function} fn
+ * @api private
+ */
+
+exports.ticksPerSecond = function ticksPerSecond(duration, fn) {
+ var ticks = 0
+ , running = true;
+
+ process.nextTick(function ticktock() {
+ if (!running) return fn(Math.round(ticks / duration));
+
+ ticks++;
+ process.nextTick(ticktock);
+ });
+
+ setTimeout(function timeout() {
+ running = false;
+ }, duration * 1000);
+};
@@ -0,0 +1,26 @@
+{
+ "name": "diagnostics",
+ "version": "0.0.0",
+ "description": "event loop diagnostics",
+ "main": "index.js",
+ "scripts": {
+ "test": "make test"
+ },
+ "keywords": [
+ "event",
+ "loop",
+ "diagnostics",
+ "event",
+ "loop",
+ "diagnostic",
+ "metrics",
+ "stats",
+ "goats"
+ ],
+ "author": "Arnout Kazemier",
+ "license": "MIT",
+ "devDependencies": {
+ "mocha": "1.3.0",
+ "chai": "1.1.0"
+ }
+}
@@ -0,0 +1,17 @@
+"use strict";
+
+/**
+ * Expose some globals which will be used during the test suite.
+ */
+global.chai = require('chai');
+global.chai.Assertion.includeStack = true;
+global.expect = global.chai.expect;
+
+/**
+ * Awesome global hack to automatically increase numbers
+ */
+var testnumbers = 10000;
+
+global.__defineGetter__('TESTNUMBER', function testnumber() {
+ return testnumbers++;
+});
@@ -0,0 +1,3 @@
+--require ./test/common
+--reporter spec
+--ui bdd
@@ -0,0 +1,28 @@
+/*global expect */
+"use strict";
+
+var ticker = require('../lib/ticks');
+
+describe('ticker', function () {
+ describe('#tickDuration', function () {
+ it('should return a tick', function (done) {
+ ticker.tickDuration(function (duration) {
+ console.log('tick', duration);
+ expect(duration).to.be.a('number');
+
+ done();
+ });
+ });
+ });
+
+ describe('#ticksPerSecond', function () {
+ it('should return a count', function (done) {
+ ticker.ticksPerSecond(1, function (ticks) {
+ expect(ticks).to.be.a('number');
+ expect(ticks).to.be.above(100);
+
+ done();
+ });
+ });
+ });
+});

0 comments on commit b1e127c

Please sign in to comment.