Browse Source

Initial Commit

Kyle P Davis 11 years ago
commit
c65a7350ff
13 changed files with 266 additions and 0 deletions
  1. 16 0
      .gitignore
  2. 38 0
      .jshintrc
  3. 29 0
      example/array.js
  4. 32 0
      example/array_manually.js
  5. 36 0
      example/json.js
  6. 1 0
      example/node_modules/benchmarksman
  7. 3 0
      index.js
  8. 5 0
      lib/index.js
  9. 17 0
      lib/reporter.js
  10. 49 0
      lib/runner.js
  11. 17 0
      main.js
  12. 19 0
      package.json
  13. 4 0
      test/mocha.opts

+ 16 - 0
.gitignore

@@ -0,0 +1,16 @@
+logs/*
+!.gitkeep
+
+# node
+/node_modules
+npm-debug.log
+
+# IDE files
+/.idea
+/.settings.xml
+/.settings
+/.c9revisions/
+
+# misc files
+*.swp
+.DS_Store

+ 38 - 0
.jshintrc

@@ -0,0 +1,38 @@
+{
+
+	"predef": [
+		"before",
+		"after",
+		"it",
+		"describe",
+		"beforeEach",
+		"afterEach"
+	],
+
+	"node": true,
+	"devel": false,
+
+	"camelcase": true,
+	"curly": false,
+	"eqeqeq": true,
+	"forin": true,
+	"immed": true,
+	"latedef": true,
+	"newcap": true,
+	"noempty": true,
+	"nonew": true,
+	"quotmark": false,
+	"undef": true,
+	"unused": "vars",
+	"strict": false,
+	"trailing": true,
+	"maxparams": 13,
+	"maxdepth": 7,
+	"maxstatements": 42,
+	"maxcomplexity": 11,
+	"maxlen": 140,
+
+	"boss": true,
+	"globalstrict": true
+
+}

+ 29 - 0
example/array.js

@@ -0,0 +1,29 @@
+"use strict";
+// An example of benchmarking Arrays using benchmarksman
+
+
+// setup
+var noop = function(){},
+	arr = new Array(1000).map(function (v, i) {
+		return i;
+	});
+
+
+// tests
+exports.Array = {
+
+	"for": function() {
+		for (var i = 0, l = arr.length; i < l; i++) {
+			noop(arr[i]);
+		}
+	},
+
+	"forEach": function() {
+		arr.forEach(noop);
+	},
+
+};
+
+
+// if run directly run benchmarks
+if (!module.main) return require("benchmarksman").runner(exports);

+ 32 - 0
example/array_manually.js

@@ -0,0 +1,32 @@
+"use strict";
+// An example of benchmarking Arrays using benchmark.js for comparison
+var benchmark = require("benchmark");
+
+
+// setup
+var noop = function(){},
+	arr = new Array(1000).map(function (v, i) {
+		return i;
+	});
+
+
+// tests
+console.log(
+	String(
+		benchmark("forEach", function(){
+			arr.forEach(noop);
+		})
+		.run()
+	)
+);
+console.log(
+	String(
+		benchmark("for", function(){
+			for(var i = 0, l = arr.length; i < l; i++){
+				noop(arr[i]);
+			}
+		})
+		.run()
+	)
+);
+

+ 36 - 0
example/json.js

@@ -0,0 +1,36 @@
+"use strict";
+// An example of benchmarking JSON stringify using benchmarksman
+
+
+// setup
+var getAlphabetObj = function (val) {
+		return "abcdefghijklmnopqrstuvwxyz".split("").reduce(function (r, k, i) {
+			r[k] = val instanceof Function ? val(k, i, r) : val;
+			return r;
+		}, {});
+	},
+	obj = getAlphabetObj(getAlphabetObj(getAlphabetObj(1))),
+	str = JSON.stringify(obj),
+	buf = new Buffer(JSON.stringify(str));
+
+
+// tests
+exports.JSON = {
+
+	".parse()": {
+
+		"a String": function(){
+			JSON.parse(str);
+		},
+
+		"a Buffer": function(){
+			JSON.parse(buf);
+		}
+
+	}
+
+};
+
+
+// if run directly run benchmarks
+if(!module.main) return require("benchmarksman").runner(exports);

+ 1 - 0
example/node_modules/benchmarksman

@@ -0,0 +1 @@
+../../lib/

+ 3 - 0
index.js

@@ -0,0 +1,3 @@
+"use strict";
+module.exports = require("./lib");
+if (!module.parent) require("./main")();

+ 5 - 0
lib/index.js

@@ -0,0 +1,5 @@
+"use strict";
+module.exports = {
+	reporter: require("./reporter"),
+	runner: require("./runner")
+};

+ 17 - 0
lib/reporter.js

@@ -0,0 +1,17 @@
+"use strict";
+
+/**
+ * Reporter module for benchmark.js instances.
+ * @private
+ */
+module.exports = function reporter(bench){
+	var ctx = this;
+	return bench
+		.on("error", function(){
+			if (ctx.file) console.error("ERROR in file: " + ctx.file);
+			throw bench.error;
+		})
+		.on("complete", function() {
+			console.log(String(this));
+		});
+};

+ 49 - 0
lib/runner.js

@@ -0,0 +1,49 @@
+"use strict";
+
+/**
+ * Create benchmarks defined similarly to tests in the mocha exports UI
+ */
+var path = require("path"),
+	benchmark = require("benchmark"),
+	reporter = require("./reporter");
+
+/**
+ * Runner for benchmarks defined similarly to tests in the mocha exports UI
+ * @param  {Object|Array|String} tests      The tests walk and execute
+ * @private
+ */
+module.exports = function runner(tests) {
+	var ctx = this && this !== global ? this : {};
+	if (!ctx.reporter) ctx.reporter = reporter;
+	if (typeof tests === "string") tests = require(ctx.file = path.resolve(tests));
+	if (Array.isArray(tests) && !ctx.parent) return tests.forEach(runner.bind(ctx));
+	if (tests.before instanceof Function) tests.before.call(ctx);
+	for (var name in tests) {
+		if (!tests.hasOwnProperty(name)) continue;
+		if (~["before", "after", "beforeEach", "afterEach"].indexOf(name)) continue;
+		var test = tests[name],
+			fullName = ctx.fullName ? ctx.fullName + " " + name : name;
+		if (test instanceof Function) {
+			var bench = benchmark(fullName, test, {defer: test.length === 1});
+			reporter.call(ctx, bench);
+			try {
+				if (tests.beforeEach instanceof Function) tests.beforeEach.call(ctx);
+				test(); // run once outside of bench for better error handling
+				bench.run();
+				if (tests.afterEach instanceof Function) tests.afterEach.call(ctx);
+			} catch (e) {
+				bench.emit("error", bench.error = e);
+			}
+		} else {
+			var newCtx = {};
+			for (var key in ctx) {
+				if (ctx.hasOwnProperty(key)) newCtx[key] = ctx[key];
+			}
+			newCtx.parent = ctx;
+			newCtx.name = name;
+			newCtx.fullName = fullName;
+			runner.call(newCtx, test);
+		}
+	}
+	if (tests.after instanceof Function) tests.after.call(ctx);
+};

+ 17 - 0
main.js

@@ -0,0 +1,17 @@
+#!/usr/bin/env node
+"use strict";
+
+var lib = require("./lib"),
+	runner = lib.runner;
+
+/**
+ * Main command handler if this is run directly
+ */
+module.exports = function main() {
+	var pkg = require("./package.json"),
+		args = process.argv.slice(2);
+	if (args.length === 0 || ~["-h", "--help"].indexOf(args[0])) {
+		console.error("USAGE: %s [bench/slow.js]", pkg.name);
+	}
+	args.forEach(runner);
+};

+ 19 - 0
package.json

@@ -0,0 +1,19 @@
+{
+  "name": "benchmarksman",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "bin": {
+    "benchmarksman": "main.js"
+  },
+  "scripts": {
+    "test": "mocha",
+    "benchmarks": "benchmarksman bench/"
+  },
+  "author": "",
+  "license": "MIT",
+  "dependencies": {
+    "benchmark": "^1.0.0",
+    "microtime": "^1.0.1"
+  }
+}

+ 4 - 0
test/mocha.opts

@@ -0,0 +1,4 @@
+--reporter spec
+--ui bdd
+--recursive
+**/*_test.js