aboutsummaryrefslogtreecommitdiffstats
path: root/src/benchmarks/eina/eina_bench_promise.c
blob: 00d55cc955d308c7d8735ab7d9b7455c3c2a920c (plain) (blame)
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
/* EINA - EFL data type library
 * Copyright (C) 2008 Cedric Bail
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;
 * if not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#include "Evas_Data.h"
#include "Ecore_Data.h"

#include "eina_stringshare.h"
#include "eina_bench.h"
#include "eina_convert.h"
#include "eina_main.h"

#include <assert.h>

#include <Eina.h>

struct value_type
{
  int x;
  int y;
  int w;
  int h;
};

void cb(void* data EINA_UNUSED, void* value, Eina_Promise* promise EINA_UNUSED)
{
  struct value_type* p = value;
  volatile int v = p->x * p->y * p->w * p->h;
  (void)v;
}

void pointer_cb(void* data EINA_UNUSED, void* value, Eina_Promise* promise EINA_UNUSED)
{
  struct value_type* p = *(struct value_type**)value;
  volatile int v = p->x * p->y * p->w * p->h;
  (void)v;
}

static void
eina_bench_promise_sync_then(int request)
{
   const char *tmp;
   unsigned int j;
   int i;

   eina_init();

   Eina_Promise_Owner* owner = eina_promise_default_add(sizeof(struct value_type));
   Eina_Promise* promise = eina_promise_owner_promise_get(owner);
   eina_promise_ref(promise);

   struct value_type v = { 0, 0, 0, 0 };
   eina_promise_owner_value_set(owner, &v, NULL);
   
   for (j = 0; j != 200; ++j)
      for (i = 0; i != request; ++i)
        {
           eina_promise_then(promise, &cb, NULL, NULL);
        }

   /* Suppress warnings as we really don't want to do anything. */
   (void) tmp;

   eina_promise_unref(promise);
   eina_shutdown();
}

static void
eina_bench_promise_copy_value_set_after_then(int request)
{
   const char *tmp;
   unsigned int j;
   int i;

   eina_init();

   struct value_type const v = {0, 0, 0, 0};

   for (j = 0; j != 200; ++j)
      for (i = 0; i != request; ++i)
        {
           Eina_Promise_Owner* owner = eina_promise_default_add(sizeof(struct value_type));
           Eina_Promise* promise = eina_promise_owner_promise_get(owner);

           eina_promise_then(promise, &cb, NULL, NULL);
           eina_promise_owner_value_set(owner, &v, NULL);
        }

   /* Suppress warnings as we really don't want to do anything. */
   (void) tmp;

   eina_shutdown();
}

static void
eina_bench_promise_no_copy_value_set_after_then(int request)
{
   const char *tmp;
   unsigned int j;
   int i;

   eina_init();

   for (j = 0; j != 200; ++j)
      for (i = 0; i != request; ++i)
        {
           Eina_Promise_Owner* owner = eina_promise_default_add(sizeof(struct value_type));
           Eina_Promise* promise = eina_promise_owner_promise_get(owner);
           struct value_type* v = eina_promise_owner_buffer_get(owner);

           eina_promise_then(promise, &cb, NULL, NULL);

           v->x = v->y = v-> w = v->h = 0;
           eina_promise_owner_value_set(owner, NULL, NULL);
        }

   /* Suppress warnings as we really don't want to do anything. */
   (void) tmp;

   eina_shutdown();
}

static void
eina_bench_promise_no_copy_value_set_before_then(int request)
{
   const char *tmp;
   unsigned int j;
   int i;

   eina_init();

   for (j = 0; j != 200; ++j)
      for (i = 0; i != request; ++i)
        {
           Eina_Promise_Owner* owner = eina_promise_default_add(sizeof(struct value_type));
           Eina_Promise* promise = eina_promise_owner_promise_get(owner);
           struct value_type* v = eina_promise_owner_buffer_get(owner);

           v->x = v->y = v-> w = v->h = 0;
           eina_promise_owner_value_set(owner, NULL, NULL);

           eina_promise_then(promise, &cb, NULL, NULL);
        }

   /* Suppress warnings as we really don't want to do anything. */
   (void) tmp;

   eina_shutdown();
}

static void
eina_bench_promise_copy_value_set_before_then(int request)
{
   const char *tmp;
   unsigned int j;
   int i;

   eina_init();

   struct value_type const v = {0, 0, 0, 0};

   for (j = 0; j != 200; ++j)
      for (i = 0; i != request; ++i)
        {
           Eina_Promise_Owner* owner = eina_promise_default_add(sizeof(struct value_type));
           Eina_Promise* promise = eina_promise_owner_promise_get(owner);

           eina_promise_then(promise, &cb, NULL, NULL);
           eina_promise_owner_value_set(owner, &v, NULL);
        }

   /* Suppress warnings as we really don't want to do anything. */
   (void) tmp;

   eina_shutdown();
}

static void indirect_free(void* p)
{
  free(*(void**)p);
}

static void
eina_bench_promise_pointer_value_set_before_then(int request)
{
   const char *tmp;
   unsigned int j;
   int i;

   eina_init();

   for (j = 0; j != 200; ++j)
      for (i = 0; i != request; ++i)
        {
           Eina_Promise_Owner* owner = eina_promise_default_add(sizeof(struct value_type*));
           Eina_Promise* promise = eina_promise_owner_promise_get(owner);

           struct value_type* p = *(struct value_type**)eina_promise_owner_buffer_get(owner)
             = malloc(sizeof(struct value_type));
           p->x = p->y = p->w = p->h = 0;

           eina_promise_then(promise, &pointer_cb, NULL, NULL);
           eina_promise_owner_value_set(owner, NULL, &indirect_free);
        }

   /* Suppress warnings as we really don't want to do anything. */
   (void) tmp;

   eina_shutdown();
}

static void
eina_bench_promise_pointer_value_set_after_then(int request)
{
   const char *tmp;
   unsigned int j;
   int i;

   eina_init();

   for (j = 0; j != 200; ++j)
      for (i = 0; i != request; ++i)
        {
           Eina_Promise_Owner* owner = eina_promise_default_add(sizeof(struct value_type*));
           Eina_Promise* promise = eina_promise_owner_promise_get(owner);

           struct value_type* p = *(struct value_type**)eina_promise_owner_buffer_get(owner)
             = malloc(sizeof(struct value_type));
           p->x = p->y = p->w = p->h = 0;

           eina_promise_owner_value_set(owner, NULL, &indirect_free);
           eina_promise_then(promise, &pointer_cb, NULL, NULL);
        }

   /* Suppress warnings as we really don't want to do anything. */
   (void) tmp;

   eina_shutdown();
}

static Eina_Mempool* mempool;

static void indirect_mempool_free(void* p)
{
   eina_mempool_free(mempool, *(void**)p);
}

static void
eina_bench_promise_pointer_value_set_before_then_pooled(int request)
{
   const char *tmp;
   unsigned int j;
   int i;

   eina_init();

   mempool = eina_mempool_add("chained_mempool", "", NULL, sizeof(struct value_type), 10);
   assert(mempool != NULL);

   for (j = 0; j != 200; ++j)
      for (i = 0; i != request; ++i)
        {
           Eina_Promise_Owner* owner = eina_promise_default_add(sizeof(struct value_type*));
           Eina_Promise* promise = eina_promise_owner_promise_get(owner);

           struct value_type* p = *(struct value_type**)eina_promise_owner_buffer_get(owner)
             = eina_mempool_malloc(mempool, sizeof(struct value_type));
           p->x = p->y = p->w = p->h = 0;

           eina_promise_then(promise, &pointer_cb, NULL, NULL);
           eina_promise_owner_value_set(owner, NULL, &indirect_mempool_free);
        }

   /* Suppress warnings as we really don't want to do anything. */
   (void) tmp;

   eina_mempool_del(mempool);
   
   eina_shutdown();
}

static void
eina_bench_promise_pointer_value_set_after_then_pooled(int request)
{
   const char *tmp;
   unsigned int j;
   int i;

   eina_init();

   mempool = eina_mempool_add("chained_mempool", "", NULL, sizeof(struct value_type), 10);
   assert(mempool != NULL);

   for (j = 0; j != 200; ++j)
      for (i = 0; i != request; ++i)
        {
           Eina_Promise_Owner* owner = eina_promise_default_add(sizeof(struct value_type*));
           Eina_Promise* promise = eina_promise_owner_promise_get(owner);

           struct value_type* p = *(struct value_type**)eina_promise_owner_buffer_get(owner)
             = eina_mempool_malloc(mempool, sizeof(struct value_type));
           p->x = p->y = p->w = p->h = 0;

           eina_promise_owner_value_set(owner, NULL, &indirect_mempool_free);
           eina_promise_then(promise, &pointer_cb, NULL, NULL);
        }

   /* Suppress warnings as we really don't want to do anything. */
   (void) tmp;

   eina_mempool_del(mempool);
   eina_shutdown();
}

void eina_bench_promise(Eina_Benchmark *bench)
{
   eina_benchmark_register(bench, "promise synchronous then",
                           EINA_BENCHMARK(
                              eina_bench_promise_sync_then), 100, 20100, 500);
   eina_benchmark_register(bench, "promise copy value set after then",
                           EINA_BENCHMARK(
                              eina_bench_promise_copy_value_set_after_then), 100, 20100, 500);
   eina_benchmark_register(bench, "promise copy value set before then",
                           EINA_BENCHMARK(
                              eina_bench_promise_copy_value_set_before_then), 100, 20100, 500);
   eina_benchmark_register(bench, "promise no copy value set after then",
                           EINA_BENCHMARK(
                              eina_bench_promise_no_copy_value_set_after_then), 100, 20100, 500);
   eina_benchmark_register(bench, "promise no copy value set before then",
                           EINA_BENCHMARK(
                              eina_bench_promise_no_copy_value_set_before_then), 100, 20100, 500);
   eina_benchmark_register(bench, "promise pointer value set after then",
                           EINA_BENCHMARK(
                              eina_bench_promise_pointer_value_set_after_then), 100, 20100, 500);
   eina_benchmark_register(bench, "promise pointer value set before then",
                           EINA_BENCHMARK(
                              eina_bench_promise_pointer_value_set_before_then), 100, 20100, 500);
   eina_benchmark_register(bench, "promise pointer value set after then mempool",
                           EINA_BENCHMARK(
                              eina_bench_promise_pointer_value_set_after_then_pooled), 100, 20100, 500);
   eina_benchmark_register(bench, "promise pointer value set before then mempool",
                           EINA_BENCHMARK(
                              eina_bench_promise_pointer_value_set_before_then_pooled), 100, 20100, 500);
}