summaryrefslogtreecommitdiff
path: root/tests/benchmark.c
blob: 1cea614e64ce377961c7b632eb51a1d47399c14d (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
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <limits.h>

#include "yasl/yasl.h"
#include "yasl/loop.h"
#include "yasl/timing.h"
#include "common/input.h"
#if YASL_HAS_WAYLAND
#include "yasl/native/wayland.h"
#endif

static inline uint64_t get_time_ns(void) {
    struct timespec ts;
    clock_gettime(CLOCK_MONOTONIC, &ts);
    return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
}

static void format_duration(char *buf, size_t buf_sz, double ns) {
    if (ns < 1000.0) {
        snprintf(buf, buf_sz, "%6.2f ns", ns);
    } else if (ns < 1000000.0) {
        snprintf(buf, buf_sz, "%6.2f us", ns / 1e3);
    } else if (ns < 1000000000.0) {
        snprintf(buf, buf_sz, "%6.2f ms", ns / 1e6);
    } else {
        snprintf(buf, buf_sz, "%6.2f s ", ns / 1e9);
    }
}

static void print_bench_row(const char *name, double per_cycle_ns, double total_wall_ms, const char *details) {
    char per_cycle_str[32];
    format_duration(per_cycle_str, sizeof(per_cycle_str), per_cycle_ns);
    printf("%-29s | %10s | %9.2f ms | %s\n", name, per_cycle_str, total_wall_ms, details);
}

static void print_bench_skipped(const char *name, const char *reason) {
    printf("%-29s |    skipped |         - | %s\n", name, reason);
}

#if YASL_HAS_WAYLAND
/* Benchmark 1: Public Wayland Surface Handle Lookup */
static void bench_surface_lookup(void) {
    const int num_surfaces = 16;
    const int lookups = 10000000;
    struct yasl_display_spec spec = {
        .app_id = "org.yasl.benchmark",
        .backend = YASL_BACKEND_WAYLAND,
    };
    struct yasl_display *d = yasl_display_open(NULL, &spec);
    struct yasl_surface *surfaces[num_surfaces];
    memset(surfaces, 0, sizeof(surfaces));

    if (d) {
        for (int i = 0; i < num_surfaces; i++) {
            struct yasl_window_spec sspec = {
                .title = "Benchmark",
                .size = { .x = 320, .y = 240 },
            };
            surfaces[i] = yasl_surface_create_toplevel(d, &sspec);
        }
    }

    uint64_t t0 = get_time_ns();
    uintptr_t sum = 0;
    for (int i = 0; i < lookups; i++) {
        struct wl_surface *found = yasl_wayland_surface_get_wl_surface(surfaces[i % num_surfaces]);
        sum += (uintptr_t)found;
    }
    uint64_t t1 = get_time_ns();
    double ms = (double)(t1 - t0) / 1e6;
    double per_cycle = (double)(t1 - t0) / lookups;
    char details[128];
    snprintf(details, sizeof(details), "%d lookups over %d surfaces (checksum=%lx)",
             lookups, num_surfaces, (unsigned long)sum);
    print_bench_row("[1] wayland_surface_lookup", per_cycle, ms, details);

    if (d) {
        for (int i = 0; i < num_surfaces; i++) {
            if (surfaces[i]) yasl_surface_destroy(surfaces[i]);
        }
        yasl_display_close(d);
    }
}

/* Benchmark 2: Public Surface Geometry & Refresh Timing */
static void bench_output_follow(void) {
    const int iters = 10000000;
    struct yasl_display_spec spec = {
        .app_id = "org.yasl.benchmark",
        .backend = YASL_BACKEND_WAYLAND,
    };
    struct yasl_display *d = yasl_display_open(NULL, &spec);
    struct yasl_surface *s = NULL;
    if (d) {
        struct yasl_window_spec sspec = {
            .title = "Benchmark",
            .size = { .x = 320, .y = 240 },
        };
        s = yasl_surface_create_toplevel(d, &sspec);
    }

    uint64_t t0 = get_time_ns();
    uint64_t sum = 0;
    for (int i = 0; i < iters; i++) {
        struct yasl_geometry geom = yasl_surface_geometry(s);
        struct yasl_timing timing = yasl_surface_timing(s);
        int dev = yasl_scale_to_device(320.0, geom.scale_120);
        sum += (uint64_t)dev + timing.refresh_ns;
    }
    uint64_t t1 = get_time_ns();
    double ms = (double)(t1 - t0) / 1e6;
    double per_cycle = (double)(t1 - t0) / iters;
    char details[128];
    snprintf(details, sizeof(details), "%d iterations (checksum=%llu)",
             iters, (unsigned long long)sum);
    print_bench_row("[2] surface_geometry_timing", per_cycle, ms, details);

    if (s) yasl_surface_destroy(s);
    if (d) yasl_display_close(d);
}
#endif

/* Benchmark 3: Input Queue Resizing Throughput */
static void bench_input_queue_resize(void) {
    const int batches = 50000;
    const int size = 2048;

    uint64_t t0 = get_time_ns();
    for (int b = 0; b < batches; b++) {
        struct input_queue q = {0};
        for (int i = 0; i < size; i++) {
            struct yasl_event ev = {
                .type = YASL_EVENT_POINTER_MOTION,
                .pointer = { .pos = { .x = i, .y = b } },
            };
            input_queue_push(&q, &ev);
        }
        input_queue_clear(&q);
    }
    uint64_t t1 = get_time_ns();
    double ms = (double)(t1 - t0) / 1e6;
    double per_cycle = (double)(t1 - t0) / ((double)batches * size);
    char details[128];
    snprintf(details, sizeof(details), "%d batches x %d events (102.4M total)", batches, size);
    print_bench_row("[3] input_queue_resize", per_cycle, ms, details);
}

/* Benchmark 4: Input Queue Push & Pop Throughput */
static void bench_input_queue_push_pop(void) {
    const int rounds = 20000;
    const int events_per_round = 500;
    struct input_queue q = {0};

    uint64_t t0 = get_time_ns();
    for (int r = 0; r < rounds; r++) {
        for (int i = 0; i < events_per_round; i++) {
            struct yasl_event ev = {
                .type = YASL_EVENT_POINTER_DOWN,
                .pointer = { .pos = { .x = i, .y = r }, .button = YASL_BUTTON_MIDDLE },
            };
            input_queue_push(&q, &ev);
        }
        struct yasl_event out;
        while (input_queue_pop(&q, &out)) {
            /* consume */
        }
    }
    uint64_t t1 = get_time_ns();
    input_queue_clear(&q);
    double ms = (double)(t1 - t0) / 1e6;
    double per_cycle = (double)(t1 - t0) / ((double)rounds * events_per_round);
    char details[128];
    snprintf(details, sizeof(details), "%d rounds x %d events (10.0M total)", rounds, events_per_round);
    print_bench_row("[4] input_queue_push_pop", per_cycle, ms, details);
}

/* Benchmark 5: Event Loop Timers Add & Delete */
static uint64_t timer_dummy_cb(void *ud, yasl_timer_id id, uint64_t interval_ns) {
    (void)ud; (void)id; (void)interval_ns;
    return 0;
}

static void bench_timers(void) {
    const int iters = 2000000;
    struct yasl_loop *loop = yasl_loop_create();

    uint64_t t0 = get_time_ns();
    for (int i = 0; i < iters; i++) {
        yasl_timer_id id = yasl_loop_add_timer(loop, 1000000, timer_dummy_cb, NULL);
        yasl_loop_del_timer(loop, id);
    }
    uint64_t t1 = get_time_ns();
    yasl_loop_destroy(loop);
    double ms = (double)(t1 - t0) / 1e6;
    double per_cycle = (double)(t1 - t0) / iters;
    char details[128];
    snprintf(details, sizeof(details), "%d arm/disarm cycles", iters);
    print_bench_row("[5] timers_add_del", per_cycle, ms, details);
}

/* Benchmark 6: Window Cold Create & Teardown */
static void bench_window_cold_create(void) {
    const int iters = 200;
    struct yasl_display_spec dspec = { .app_id = "org.yasl.bench.cold" };
    struct yasl_window_spec sspec = {
        .title = "Benchmark Cold",
        .size = { .x = 320, .y = 240 },
    };

    struct yasl_display *probe = yasl_display_open(NULL, &dspec);
    if (!probe) {
        print_bench_skipped("[6] window_cold_create", "no display server");
        return;
    }
    yasl_display_close(probe);

    uint64_t t0 = get_time_ns();
    int count = 0;
    for (int i = 0; i < iters; i++) {
        struct yasl_display *d = yasl_display_open(NULL, &dspec);
        if (d) {
            struct yasl_surface *s = yasl_surface_create_toplevel(d, &sspec);
            if (s) {
                yasl_surface_destroy(s);
                count++;
            }
            yasl_display_close(d);
        }
    }
    uint64_t t1 = get_time_ns();
    double ms = (double)(t1 - t0) / 1e6;
    double per_cycle = count ? (double)(t1 - t0) / count : 0.0;
    char details[128];
    snprintf(details, sizeof(details), "%d open/create/destroy/close cycles", count);
    print_bench_row("[6] window_cold_create", per_cycle, ms, details);
}

/* Benchmark 7: Window Warm Create */
static void bench_window_warm_create(void) {
    const int iters = 2000;
    struct yasl_display_spec dspec = { .app_id = "org.yasl.bench.warm" };
    struct yasl_display *d = yasl_display_open(NULL, &dspec);
    if (!d) {
        print_bench_skipped("[7] window_warm_create", "no display server");
        return;
    }

    struct yasl_window_spec sspec = {
        .title = "Benchmark Warm",
        .size = { .x = 320, .y = 240 },
    };

    uint64_t t0 = get_time_ns();
    int count = 0;
    for (int i = 0; i < iters; i++) {
        struct yasl_surface *s = yasl_surface_create_toplevel(d, &sspec);
        if (s) {
            yasl_surface_destroy(s);
            count++;
        }
    }
    uint64_t t1 = get_time_ns();
    yasl_display_close(d);
    double ms = (double)(t1 - t0) / 1e6;
    double per_cycle = count ? (double)(t1 - t0) / count : 0.0;
    char details[128];
    snprintf(details, sizeof(details), "%d surface create/destroy cycles", count);
    print_bench_row("[7] window_warm_create", per_cycle, ms, details);
}

/* Benchmark 8: Window Shutdown (Single) */
static void bench_window_shutdown_single(void) {
    const int iters = 2000;
    struct yasl_display_spec dspec = { .app_id = "org.yasl.bench.shutdown_single" };
    struct yasl_display *d = yasl_display_open(NULL, &dspec);
    if (!d) {
        print_bench_skipped("[8] window_shutdown_single", "no display server");
        return;
    }

    struct yasl_window_spec sspec = {
        .title = "Benchmark Shutdown Single",
        .size = { .x = 320, .y = 240 },
    };

    uint64_t destroy_time_ns = 0;
    int count = 0;
    for (int i = 0; i < iters; i++) {
        struct yasl_surface *s = yasl_surface_create_toplevel(d, &sspec);
        if (s) {
            uint64_t t0 = get_time_ns();
            yasl_surface_destroy(s);
            uint64_t t1 = get_time_ns();
            destroy_time_ns += (t1 - t0);
            count++;
        }
    }
    yasl_display_close(d);
    double ms = (double)destroy_time_ns / 1e6;
    double per_cycle = count ? (double)destroy_time_ns / count : 0.0;
    char details[128];
    snprintf(details, sizeof(details), "%d isolated surface destructions", count);
    print_bench_row("[8] window_shutdown_single", per_cycle, ms, details);
}

/* Benchmark 9: Window Shutdown (Bulk / All) */
static void bench_window_shutdown_all(void) {
    const int batches = 50;
    const int batch_size = 64;
    struct yasl_display_spec dspec = { .app_id = "org.yasl.bench.shutdown_all" };
    struct yasl_display *d = yasl_display_open(NULL, &dspec);
    if (!d) {
        print_bench_skipped("[9] window_shutdown_all", "no display server");
        return;
    }

    struct yasl_window_spec sspec = {
        .title = "Benchmark Bulk Window",
        .size = { .x = 320, .y = 240 },
    };

    struct yasl_surface *surfaces[64];
    uint64_t destroy_time_ns = 0;
    int total_destroyed = 0;

    for (int b = 0; b < batches; b++) {
        for (int i = 0; i < batch_size; i++) {
            surfaces[i] = yasl_surface_create_toplevel(d, &sspec);
        }
        uint64_t t0 = get_time_ns();
        for (int i = 0; i < batch_size; i++) {
            if (surfaces[i]) {
                yasl_surface_destroy(surfaces[i]);
                surfaces[i] = NULL;
                total_destroyed++;
            }
        }
        uint64_t t1 = get_time_ns();
        destroy_time_ns += (t1 - t0);
    }
    yasl_display_close(d);
    double ms = (double)destroy_time_ns / 1e6;
    double per_cycle = total_destroyed ? (double)destroy_time_ns / total_destroyed : 0.0;
    double per_batch_ns = batches ? (double)destroy_time_ns / batches : 0.0;
    char per_batch_str[32];
    format_duration(per_batch_str, sizeof(per_batch_str), per_batch_ns);
    char details[128];
    snprintf(details, sizeof(details), "%d windows (%d batches x %d, %s/batch)",
             total_destroyed, batches, batch_size, per_batch_str);
    print_bench_row("[9] window_shutdown_all", per_cycle, ms, details);
}

/* Benchmark 10: Window Titlebar Update */
static void bench_window_title_update(void) {
    const int iters = 200000;
    struct yasl_display_spec dspec = { .app_id = "org.yasl.bench.title" };
    struct yasl_display *d = yasl_display_open(NULL, &dspec);
    if (!d) {
        print_bench_skipped("[10] window_titlebar_update", "no display server");
        return;
    }

    struct yasl_window_spec sspec = {
        .title = "Initial Title",
        .size = { .x = 320, .y = 240 },
    };
    struct yasl_surface *s = yasl_surface_create_toplevel(d, &sspec);
    if (!s) {
        yasl_display_close(d);
        print_bench_skipped("[10] window_titlebar_update", "failed to create surface");
        return;
    }

    const char *titles[4] = {
        "YASL Benchmark - State 1",
        "YASL Benchmark - State 2",
        "Document Editor - Untitled",
        "Terminal - Session Active"
    };

    uint64_t t0 = get_time_ns();
    for (int i = 0; i < iters; i++) {
        yasl_surface_set_title(s, titles[i & 3]);
    }
    uint64_t t1 = get_time_ns();
    yasl_surface_destroy(s);
    yasl_display_close(d);
    double ms = (double)(t1 - t0) / 1e6;
    double per_cycle = (double)(t1 - t0) / iters;
    char details[128];
    snprintf(details, sizeof(details), "%d title changes", iters);
    print_bench_row("[10] window_titlebar_update", per_cycle, ms, details);
}

/* Benchmark 11: Window Resize / Dimension Reallocation */
static void bench_window_resize(void) {
    const int iters = 2000;
    struct yasl_display_spec dspec = { .app_id = "org.yasl.bench.resize" };
    struct yasl_display *d = yasl_display_open(NULL, &dspec);
    if (!d) {
        print_bench_skipped("[11] window_resize_cycle", "no display server");
        return;
    }

    static const struct yasl_vector2i sizes[] = {
        { 320, 240 },
        { 640, 480 },
        { 800, 600 },
        { 1024, 768 },
        { 1280, 720 },
        { 1920, 1080 }
    };
    const int num_sizes = sizeof(sizes) / sizeof(sizes[0]);

    uint64_t t0 = get_time_ns();
    int count = 0;
    for (int i = 0; i < iters; i++) {
        struct yasl_window_spec sspec = {
            .title = "Resize Bench",
            .size = sizes[i % num_sizes],
        };
        struct yasl_surface *s = yasl_surface_create_toplevel(d, &sspec);
        if (s) {
            struct yasl_geometry geom = yasl_surface_geometry(s);
            (void)geom;
            yasl_surface_destroy(s);
            count++;
        }
    }
    uint64_t t1 = get_time_ns();
    yasl_display_close(d);
    double ms = (double)(t1 - t0) / 1e6;
    double per_cycle = count ? (double)(t1 - t0) / count : 0.0;
    char details[128];
    snprintf(details, sizeof(details), "%d dynamic reallocations across 6 resolutions", count);
    print_bench_row("[11] window_resize_cycle", per_cycle, ms, details);
}

int main(void) {
    printf("=================================== YASL Performance Benchmark ===================================\n");
    printf("%-29s | %10s | %12s | %s\n", "Benchmark", "Per Cycle", "Total Wall", "Operations / Details");
    printf("------------------------------+------------+---------------+--------------------------------------\n");
#if YASL_HAS_WAYLAND
    bench_surface_lookup();
    bench_output_follow();
#endif
    bench_input_queue_resize();
    bench_input_queue_push_pop();
    bench_timers();
    bench_window_cold_create();
    bench_window_warm_create();
    bench_window_shutdown_single();
    bench_window_shutdown_all();
    bench_window_title_update();
    bench_window_resize();
    printf("==================================================================================================\n");
    return 0;
}