bench: Add function_call to bench-misc
authorisaacs <i@izs.me>
Tue, 12 Feb 2013 02:22:43 +0000 (18:22 -0800)
committerisaacs <i@izs.me>
Tue, 19 Feb 2013 22:14:33 +0000 (14:14 -0800)
benchmark/function_call/bench.js [deleted file]
benchmark/function_call/wscript [deleted file]
benchmark/misc/function_call/.gitignore [new file with mode: 0644]
benchmark/misc/function_call/Makefile [new file with mode: 0644]
benchmark/misc/function_call/binding.cc [moved from benchmark/function_call/binding.cc with 74% similarity]
benchmark/misc/function_call/binding.gyp [new file with mode: 0644]
benchmark/misc/function_call/index.js [new file with mode: 0644]

diff --git a/benchmark/function_call/bench.js b/benchmark/function_call/bench.js
deleted file mode 100644 (file)
index 55e6ea1..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-var binding = require('./build/default/binding');
-
-c = 0 
-
-function js() {
-  return c++; //(new Date()).getTime();
-}
-
-var cxx = binding.hello;
-
-var i, N = 100000000;
-
-console.log(js());
-console.log(cxx());
-
-
-
-var start = new Date();
-for (i = 0; i < N; i++) {
-  js();
-}
-var jsDiff = new Date() - start;
-console.log(N +" JS function calls: " + jsDiff);
-
-
-var start = new Date();
-for (i = 0; i < N; i++) {
-  cxx();
-}
-var cxxDiff = new Date() - start;
-console.log(N +" C++ function calls: " + cxxDiff);
-
-function toMicro (diff) {
-  return (diff / N) * 1000000;
-}
-
-console.log("\nJS function call speed: %d microseconds", toMicro(jsDiff));
-console.log("C++ function call speed: %d microseconds", toMicro(cxxDiff));
-
-
-console.log("\nJS speedup " + (cxxDiff / jsDiff)); 
-
-
diff --git a/benchmark/function_call/wscript b/benchmark/function_call/wscript
deleted file mode 100644 (file)
index 3db367f..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-srcdir = '.'
-blddir = 'build'
-VERSION = '0.0.1'
-
-def set_options(opt):
-  opt.tool_options('compiler_cxx')
-
-def configure(conf):
-  conf.check_tool('compiler_cxx')
-  conf.check_tool('node_addon')
-
-def build(bld):
-  obj = bld.new_task_gen('cxx', 'shlib', 'node_addon')
-  obj.target = 'binding'
-  obj.source = 'binding.cc'
diff --git a/benchmark/misc/function_call/.gitignore b/benchmark/misc/function_call/.gitignore
new file mode 100644 (file)
index 0000000..567609b
--- /dev/null
@@ -0,0 +1 @@
+build/
diff --git a/benchmark/misc/function_call/Makefile b/benchmark/misc/function_call/Makefile
new file mode 100644 (file)
index 0000000..9dc1f30
--- /dev/null
@@ -0,0 +1,2 @@
+binding:
+       node-gyp rebuild --nodedir=../../..
similarity index 74%
rename from benchmark/function_call/binding.cc
rename to benchmark/misc/function_call/binding.cc
index 75882c1..6ea9284 100644 (file)
@@ -1,6 +1,5 @@
 #include <v8.h>
 #include <node.h>
-#include <time.h>
 
 using namespace v8;
 
@@ -8,12 +7,12 @@ static int c = 0;
 
 static Handle<Value> Hello(const Arguments& args) {
   HandleScope scope;
-  //time_t tv = time(NULL);
   return scope.Close(Integer::New(c++));
 }
 
 extern "C" void init (Handle<Object> target) {
   HandleScope scope;
-  //target->Set(String::New("hello"), String::New("World"));
   NODE_SET_METHOD(target, "hello", Hello);
 }
+
+NODE_MODULE(binding, init);
diff --git a/benchmark/misc/function_call/binding.gyp b/benchmark/misc/function_call/binding.gyp
new file mode 100644 (file)
index 0000000..3bfb844
--- /dev/null
@@ -0,0 +1,8 @@
+{
+  'targets': [
+    {
+      'target_name': 'binding',
+      'sources': [ 'binding.cc' ]
+    }
+  ]
+}
diff --git a/benchmark/misc/function_call/index.js b/benchmark/misc/function_call/index.js
new file mode 100644 (file)
index 0000000..ac05478
--- /dev/null
@@ -0,0 +1,33 @@
+// show the difference between calling a short js function
+// relative to a comparable C++ function.
+// Reports millions of calls per second.
+// Note that JS speed goes up, while cxx speed stays about the same.
+
+var assert = require('assert');
+var common = require('../../common.js');
+
+var binding = require('./build/Release/binding');
+var cxx = binding.hello;
+
+var c = 0;
+function js() {
+  return c++;
+}
+
+assert(js() === cxx());
+
+var bench = common.createBenchmark(main, {
+  type: ['js', 'cxx'],
+  millions: [1,10,50]
+});
+
+function main(conf) {
+  var n = +conf.millions * 1e6;
+
+  var fn = conf.type === 'cxx' ? cxx : js;
+  bench.start();
+  for (var i = 0; i < n; i++) {
+    fn();
+  }
+  bench.end(+conf.millions);
+}