aboutsummaryrefslogtreecommitdiff
path: root/tests/embind/embind_benchmark.cpp
blob: 5ae9a6be7a302afd9c0f44101d020eb5efe9d7c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#include <stdio.h>
#include <emscripten.h>
#include <bind.h>
#include <memory>

int counter = 0;

extern "C" {

int __attribute__((noinline)) get_counter()
{
    return counter;
}

void __attribute__((noinline)) increment_counter()
{
    ++counter;
}

int __attribute__((noinline)) sum_int(int v1, int v2, int v3, int v4, int v5, int v6, int v7, int v8, int v9)
{
    return v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9;
}

float __attribute__((noinline)) sum_float(float v1, float v2, float v3, float v4, float v5, float v6, float v7, float v8, float v9)
{
    return v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9;
}

int __attribute__((noinline)) returns_input(int i)
{
    return i;
}

extern void increment_counter_benchmark_js(int N);
extern void returns_input_benchmark_js();
extern void sum_int_benchmark_js();
extern void sum_float_benchmark_js();

extern void increment_counter_benchmark_embind_js(int N);
extern void returns_input_benchmark_embind_js();
extern void sum_int_benchmark_embind_js();
extern void sum_float_benchmark_embind_js();

extern void increment_class_counter_benchmark_embind_js(int N);
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();
extern void call_through_interface2();

extern void returns_val_benchmark();
}

emscripten::val returns_val(emscripten::val value)
{
    return emscripten::val(value.as<unsigned>() + 1);
}

class Vec3
{
public:
    Vec3():x(0),y(0),z(0) {}
    Vec3(float x_, float y_, float z_):x(x_),y(y_),z(z_) {}
    float x,y,z;
};

Vec3 add(const Vec3 &lhs, const Vec3 &rhs) { return Vec3(lhs.x+rhs.x, lhs.y+rhs.y, lhs.z+rhs.z); }

class Transform
{
public:
    Transform():scale(1) {}

    Vec3 pos;
    Vec3 rot;
    float scale;

    Vec3 __attribute__((noinline)) GetPosition() const { return pos; }
    Vec3 __attribute__((noinline)) GetRotation() const { return rot; }
    float __attribute__((noinline)) GetScale() const { return scale; }
    
    void __attribute__((noinline)) SetPosition(const Vec3 &pos_) { pos = pos_; }
    void __attribute__((noinline)) SetRotation(const Vec3 &rot_) { rot = rot_; }
    void __attribute__((noinline)) SetScale(float scale_) { scale = scale_; }
};
typedef std::shared_ptr<Transform> TransformPtr;

class GameObject
{
public:
    GameObject()
    {
        transform = std::make_shared<Transform>();
    }
    std::shared_ptr<Transform> transform;
    
    TransformPtr __attribute__((noinline)) GetTransform() const { return transform; }
};
typedef std::shared_ptr<GameObject> GameObjectPtr;

GameObjectPtr create_game_object()
{
    return std::make_shared<GameObject>();
}

GameObjectPtr __attribute__((noinline)) pass_gameobject_ptr(GameObjectPtr p)
{
    return p;
}

class Foo
{
public:
    Foo()
    :class_counter(0)
    {
    }

    void __attribute__((noinline)) incr_global_counter()
    {
        ++counter;
    }

    void __attribute__((noinline)) incr_class_counter()
    {
        ++class_counter;
    }

    int class_counter_val() const
    {
        return class_counter;
    }

    int class_counter;
};

class Interface
{
public:
    virtual void call0() = 0;
    virtual std::wstring call1(const std::wstring& str1, const std::wstring& str2) = 0;
    virtual void call_with_typed_array(size_t size, const void*) = 0;
    virtual void call_with_memory_view(size_t size, const void*) = 0;
};

EMSCRIPTEN_SYMBOL(HEAP8);
EMSCRIPTEN_SYMBOL(buffer);

EMSCRIPTEN_SYMBOL(call0);
EMSCRIPTEN_SYMBOL(call1);
EMSCRIPTEN_SYMBOL(call_with_typed_array);
EMSCRIPTEN_SYMBOL(call_with_memory_view);
EMSCRIPTEN_SYMBOL(Uint8Array);

class InterfaceWrapper : public emscripten::wrapper<Interface>
{
public:
    EMSCRIPTEN_WRAPPER(InterfaceWrapper);

    void call0() override {
        return call<void>(call0_symbol);
    }

    std::wstring call1(const std::wstring& str1, const std::wstring& str2) {
        return call<std::wstring>(call1_symbol, str1, str2);
    }

    void call_with_typed_array(size_t size, const void* data) {
        return call<void>(
            call_with_typed_array_symbol,
            emscripten::val::global(Uint8Array_symbol).new_(
                emscripten::val::module_property(HEAP8_symbol)[buffer_symbol],
                reinterpret_cast<uintptr_t>(data),
                size));
    }

    void call_with_memory_view(size_t size, const void* data) {
        return call<void>(
            call_with_memory_view_symbol,
            emscripten::memory_view(size, data));
    }
};

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)));
    }
}

void callInterface2(unsigned N, Interface& o) {
    int i = 0;
    for (unsigned i = 0; i < N; i += 8) {
        o.call_with_typed_array(sizeof(int), &i);
        o.call_with_typed_array(sizeof(int), &i);
        o.call_with_typed_array(sizeof(int), &i);
        o.call_with_typed_array(sizeof(int), &i);
        o.call_with_typed_array(sizeof(int), &i);
        o.call_with_typed_array(sizeof(int), &i);
        o.call_with_typed_array(sizeof(int), &i);
        o.call_with_typed_array(sizeof(int), &i);
    }
}

void callInterface3(unsigned N, Interface& o) {
    for (unsigned i = 0; i < N; i += 8) {
        o.call_with_memory_view(sizeof(int), &i);
        o.call_with_memory_view(sizeof(int), &i);
        o.call_with_memory_view(sizeof(int), &i);
        o.call_with_memory_view(sizeof(int), &i);
        o.call_with_memory_view(sizeof(int), &i);
        o.call_with_memory_view(sizeof(int), &i);
        o.call_with_memory_view(sizeof(int), &i);
        o.call_with_memory_view(sizeof(int), &i);
    }
}

EMSCRIPTEN_BINDINGS(benchmark)
{
    using namespace emscripten;
    
    class_<GameObject>("GameObject")
        .smart_ptr<GameObjectPtr>()
        .function("GetTransform", &GameObject::GetTransform);
        
    class_<Transform>("Transform")
        .smart_ptr<TransformPtr>()
        .function("GetPosition", &Transform::GetPosition)
        .function("GetRotation", &Transform::GetRotation)
        .function("GetScale", &Transform::GetScale)
        .function("SetPosition", &Transform::SetPosition)
        .function("SetRotation", &Transform::SetRotation)
        .function("SetScale", &Transform::SetScale);
        
    value_tuple<Vec3>("Vec3")
        .element(&Vec3::x)
        .element(&Vec3::y)
        .element(&Vec3::z);
        
    function("create_game_object", &create_game_object);
    function("pass_gameobject_ptr", &pass_gameobject_ptr);
    function("add", &add);
    
    function("get_counter", &get_counter);
    function("increment_counter", &increment_counter);
    function("returns_input", &returns_input);
    function("sum_int", &sum_int);
    function("sum_float", &sum_float);
    
    class_<Foo>("Foo")
        .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)
        ;

    class_<Interface>("Interface")
        .allow_subclass<InterfaceWrapper>()
        ;

    function("callInterface0", &callInterface0);
    function("callInterface1", &callInterface1);
    function("callInterface2", &callInterface2);
    function("callInterface3", &callInterface3);

    function("returns_val", &returns_val);
}

void __attribute__((noinline)) emscripten_get_now_benchmark(int N)
{
    volatile float t = emscripten_get_now();
    for(int i = 0; i < N; ++i)
    {
        emscripten_get_now();
        emscripten_get_now();
        emscripten_get_now();
        emscripten_get_now();
        emscripten_get_now();
        emscripten_get_now();
        emscripten_get_now();
        emscripten_get_now();
        emscripten_get_now();
        emscripten_get_now();
    }
    volatile float t2 = emscripten_get_now();
    printf("C++ emscripten_get_now %d iters: %f msecs.\n", N, (t2-t));
}

void __attribute__((noinline)) increment_counter_benchmark(int N)
{
    volatile float t = emscripten_get_now();
    for(int i = 0; i < N; ++i)
    {
        increment_counter();
        increment_counter();
        increment_counter();
        increment_counter();
        increment_counter();
        increment_counter();
        increment_counter();
        increment_counter();
        increment_counter();
        increment_counter();
    }
    volatile float t2 = emscripten_get_now();
    printf("C++ increment_counter %d iters: %f msecs.\n", N, (t2-t));
}

void __attribute__((noinline)) increment_class_counter_benchmark(int N)
{
    Foo foo;
    volatile float t = emscripten_get_now();
    for(int i = 0; i < N; ++i)
    {
        foo.incr_class_counter();
        foo.incr_class_counter();
        foo.incr_class_counter();
        foo.incr_class_counter();
        foo.incr_class_counter();
        foo.incr_class_counter();
        foo.incr_class_counter();
        foo.incr_class_counter();
        foo.incr_class_counter();
        foo.incr_class_counter();
    }
    volatile float t2 = emscripten_get_now();
    printf("C++ increment_class_counter %d iters: %f msecs. result: %d\n", N, (t2-t), foo.class_counter);
}

void __attribute__((noinline)) returns_input_benchmark()
{
    volatile int r = 0;
    volatile float t = emscripten_get_now();
    for(int i = 0; i < 100000; ++i)
    {
        r += returns_input(i);
        r += returns_input(i);
        r += returns_input(i);
        r += returns_input(i);
        r += returns_input(i);
        r += returns_input(i);
        r += returns_input(i);
        r += returns_input(i);
        r += returns_input(i);
        r += returns_input(i);
    }
    volatile float t2 = emscripten_get_now();
    printf("C++ returns_input 100000 iters: %f msecs.\n", (t2-t));
}

void __attribute__((noinline)) sum_int_benchmark()
{
    volatile float t = emscripten_get_now();
    volatile int r = 0;
    for(int i = 0; i < 100000; ++i)
    {
        r += sum_int(i,2,3,4,5,6,7,8,9);
        r += sum_int(i,2,3,4,5,6,7,8,9);
        r += sum_int(i,2,3,4,5,6,7,8,9);
        r += sum_int(i,2,3,4,5,6,7,8,9);
        r += sum_int(i,2,3,4,5,6,7,8,9);
        r += sum_int(i,2,3,4,5,6,7,8,9);
        r += sum_int(i,2,3,4,5,6,7,8,9);
        r += sum_int(i,2,3,4,5,6,7,8,9);
        r += sum_int(i,2,3,4,5,6,7,8,9);
        r += sum_int(i,2,3,4,5,6,7,8,9);
    }
    volatile float t2 = emscripten_get_now();
    printf("C++ sum_int 100000 iters: %f msecs.\n", (t2-t));
}

void __attribute__((noinline)) sum_float_benchmark()
{
    volatile float f = 0.f;
    volatile float t = emscripten_get_now();
    for(int i = 0; i < 100000; ++i)
    {
        f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
        f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
        f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
        f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
        f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
        f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
        f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
        f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
        f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
        f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
    }
    volatile float t2 = emscripten_get_now();
    printf("C++ sum_float 100000 iters: %f msecs.\n", (t2-t));
}

void __attribute__((noinline)) move_gameobjects_benchmark()
{
    const int N = 10000;
    GameObjectPtr objects[N];
    for(int i = 0; i < N; ++i)
        objects[i] = create_game_object();
    
    volatile float t = emscripten_get_now();
    for(int i = 0; i < N; ++i)
    {
        TransformPtr t = objects[i]->GetTransform();
        Vec3 pos = add(t->GetPosition(), Vec3(2.f, 0.f, 1.f));
        Vec3 rot = add(t->GetRotation(), Vec3(0.1f, 0.2f, 0.3f));
        t->SetPosition(pos);
        t->SetRotation(rot);
    }
    volatile float t2 = emscripten_get_now();

    Vec3 accum;
    for(int i = 0; i < N; ++i)
        accum = add(add(accum, objects[i]->GetTransform()->GetPosition()), objects[i]->GetTransform()->GetRotation());
    printf("C++ move_gameobjects %d iters: %f msecs. Result: %f\n", N, (t2-t), accum.x+accum.y+accum.z);
}

void __attribute__((noinline)) pass_gameobject_ptr_benchmark()
{
    const int N = 100000;
    GameObjectPtr objects[N];
    for(int i = 0; i < N; ++i)
        objects[i] = create_game_object();
    
    volatile float t = emscripten_get_now();
    for(int i = 0; i < N; ++i)
    {
        objects[i] = pass_gameobject_ptr(objects[i]);
    }
    volatile float t2 = emscripten_get_now();

    printf("C++ pass_gameobject_ptr %d iters: %f msecs.\n", N, (t2-t));
}

int main()
{
    /*
    for(int i = 1000; i <= 100000; i *= 10)
        emscripten_get_now_benchmark(i);

    printf("\n");
    for(int i = 1000; i <= 100000; i *= 10)
    {
        increment_counter_benchmark(i);
        increment_counter_benchmark_js(i);
        increment_counter_benchmark_embind_js(i);
        printf("\n");
    }

    for(int i = 1000; i <= 100000; i *= 10)
    {
        increment_class_counter_benchmark(i);
        increment_class_counter_benchmark_embind_js(i);
        printf("\n");
    }

    returns_input_benchmark();
    returns_input_benchmark_js();
    returns_input_benchmark_embind_js();
    printf("\n");
    sum_int_benchmark();
    sum_int_benchmark_js();
    sum_int_benchmark_embind_js();
    printf("\n");
    sum_float_benchmark();
    sum_float_benchmark_js();
    sum_float_benchmark_embind_js();
    printf("\n");
    move_gameobjects_benchmark();
    move_gameobjects_benchmark_embind_js();
    printf("\n");
    pass_gameobject_ptr_benchmark();
    pass_gameobject_ptr_benchmark_embind_js();
    */
    emscripten_get_now();
    call_through_interface0();
    call_through_interface1();
    call_through_interface2();
    returns_val_benchmark();
}