aboutsummaryrefslogtreecommitdiff
path: root/tests/embind
diff options
context:
space:
mode:
authorChad Austin <chad@imvu.com>2013-05-02 00:01:08 -0700
committerChad Austin <chad@imvu.com>2013-05-02 18:55:54 -0700
commit58352074e51c15bd108a71a3c31354206d488eee (patch)
tree21321b4f764f8ad24e651c23005edc3bc0badf01 /tests/embind
parent9607e5be25f12ac986436622bdd3b710505a596c (diff)
Add a benchmark for calling from C++ into JS via interfaces
Diffstat (limited to 'tests/embind')
-rwxr-xr-xtests/embind/build_benchmark2
-rw-r--r--tests/embind/embind.benchmark.js26
-rw-r--r--tests/embind/embind_benchmark.cpp71
3 files changed, 96 insertions, 3 deletions
diff --git a/tests/embind/build_benchmark b/tests/embind/build_benchmark
new file mode 100755
index 00000000..f878a2d4
--- /dev/null
+++ b/tests/embind/build_benchmark
@@ -0,0 +1,2 @@
+#!/bin/bash
+EMCC_LLVM_TARGET=le32-unknown-nacl ~/projects/emscripten/emcc --minify 0 --bind --pre-js embind.benchmark.js -O2 --shell-file shell.html -o embind_benchmark.html embind_benchmark.cpp
diff --git a/tests/embind/embind.benchmark.js b/tests/embind/embind.benchmark.js
index fcbd64ef..7b20db88 100644
--- a/tests/embind/embind.benchmark.js
+++ b/tests/embind/embind.benchmark.js
@@ -222,3 +222,29 @@ function _pass_gameobject_ptr_benchmark_embind_js() {
Module.print("JS embind pass_gameobject_ptr " + N + " iters: " + (b-a) + " msecs.");
}
+function _call_through_interface0() {
+ var N = 1000000;
+ var obj = Module['Interface'].implement({
+ call0: function() {
+ }
+ });
+ var start = _emscripten_get_now();
+ Module['callInterface0'](N, obj);
+ var elapsed = _emscripten_get_now() - start;
+ Module.print("C++ -> JS void through interface " + N + " iters: " + elapsed + " msecs.");
+ obj.delete();
+}
+
+function _call_through_interface1() {
+ var N = 1000000;
+ var obj = Module['Interface'].implement({
+ call1: function(s1, s2) {
+ return s1 + s2;
+ }
+ });
+ var start = _emscripten_get_now();
+ Module['callInterface1'](N, obj);
+ var elapsed = _emscripten_get_now() - start;
+ Module.print("C++ -> JS std::wstring through interface " + N + " iters: " + elapsed + " msecs.");
+ obj.delete();
+}
diff --git a/tests/embind/embind_benchmark.cpp b/tests/embind/embind_benchmark.cpp
index cdfe998c..b6a834c9 100644
--- a/tests/embind/embind_benchmark.cpp
+++ b/tests/embind/embind_benchmark.cpp
@@ -5,8 +5,7 @@
int counter = 0;
-extern "C"
-{
+extern "C" {
int __attribute__((noinline)) get_counter()
{
@@ -48,6 +47,9 @@ extern void move_gameobjects_benchmark_embind_js();
extern void pass_gameobject_ptr_benchmark();
extern void pass_gameobject_ptr_benchmark_embind_js();
+
+extern void call_through_interface0();
+extern void call_through_interface1();
}
class Vec3
@@ -128,6 +130,56 @@ public:
int class_counter;
};
+class Interface
+{
+public:
+ virtual void call0() = 0;
+ virtual std::wstring call1(const std::wstring& str1, const std::wstring& str2) = 0;
+};
+
+class InterfaceWrapper : public emscripten::wrapper<Interface>
+{
+public:
+ EMSCRIPTEN_WRAPPER(InterfaceWrapper);
+
+ void call0() override {
+ return call<void>("call0");
+ }
+
+ std::wstring call1(const std::wstring& str1, const std::wstring& str2) {
+ return call<std::wstring>("call1", str1, str2);
+ }
+};
+
+void callInterface0(unsigned N, Interface& o) {
+ for (unsigned i = 0; i < N; i += 8) {
+ o.call0();
+ o.call0();
+ o.call0();
+ o.call0();
+ o.call0();
+ o.call0();
+ o.call0();
+ o.call0();
+ }
+}
+
+void callInterface1(unsigned N, Interface& o) {
+ static std::wstring foo(L"foo");
+ static std::wstring bar(L"bar");
+ static std::wstring baz(L"baz");
+ static std::wstring qux(L"qux");
+ for (unsigned i = 0; i < N; i += 7) {
+ o.call1(
+ o.call1(
+ o.call1(foo, bar),
+ o.call1(baz, qux)),
+ o.call1(
+ o.call1(qux, foo),
+ o.call1(bar, baz)));
+ }
+}
+
EMSCRIPTEN_BINDINGS(benchmark)
{
using namespace emscripten;
@@ -164,7 +216,15 @@ EMSCRIPTEN_BINDINGS(benchmark)
.constructor<>()
.function("incr_global_counter", &Foo::incr_global_counter)
.function("incr_class_counter", &Foo::incr_class_counter)
- .function("class_counter_val", &Foo::class_counter_val);
+ .function("class_counter_val", &Foo::class_counter_val)
+ ;
+
+ class_<Interface>("Interface")
+ .allow_subclass<InterfaceWrapper>()
+ ;
+
+ function("callInterface0", &callInterface0);
+ function("callInterface1", &callInterface1);
}
void __attribute__((noinline)) emscripten_get_now_benchmark(int N)
@@ -334,6 +394,7 @@ void __attribute__((noinline)) pass_gameobject_ptr_benchmark()
int main()
{
+ /*
for(int i = 1000; i <= 100000; i *= 10)
emscripten_get_now_benchmark(i);
@@ -370,4 +431,8 @@ int main()
printf("\n");
pass_gameobject_ptr_benchmark();
pass_gameobject_ptr_benchmark_embind_js();
+ */
+ emscripten_get_now();
+ call_through_interface0();
+ call_through_interface1();
}