summaryrefslogtreecommitdiffstats
path: root/tests/perf/perf.c
blob: e15a2c6098e0ef10a2efe48dee06ec2259591dc6 (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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/**
 * @file perf.c
 * @author Michal Vasko <mvasko@cesnet.cz>
 * @brief performance tests
 *
 * Copyright (c) 2021 CESNET, z.s.p.o.
 *
 * This source code is licensed under BSD 3-Clause License (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/BSD-3-Clause
 */

#define _GNU_SOURCE

#include <assert.h>
#include <inttypes.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>

#include "libyang.h"
#include "tests_config.h"

#ifdef HAVE_CALLGRIND
# include <valgrind/callgrind.h>
#endif

#define TEMP_FILE "perf_tmp"

/**
 * @brief Test state structure.
 */
struct test_state {
    const struct lys_module *mod;
    uint32_t count;
    struct lyd_node *data1;
    struct lyd_node *data2;
};

typedef LY_ERR (*setup_cb)(const struct lys_module *mod, uint32_t count, struct test_state *state);

typedef LY_ERR (*test_cb)(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end);

/**
 * @brief Single test structure.
 */
struct test {
    const char *name;
    setup_cb setup;
    test_cb test;
};

/**
 * @brief Get current time as timespec.
 *
 * @param[out] ts Timespect to fill.
 */
static void
time_get(struct timespec *ts)
{
#ifdef CLOCK_MONOTONIC_RAW
    clock_gettime(CLOCK_MONOTONIC_RAW, ts);
#elif defined (CLOCK_MONOTONIC)
    clock_gettime(CLOCK_MONOTONIC, ts);
#elif defined (CLOCK_REALTIME)
    /* no monotonic clock available, return realtime */
    clock_gettime(CLOCK_REALTIME, ts);
#else
    int rc;
    struct timeval tv;

    gettimeofday(&tv, NULL);
    ts->tv_sec = (time_t)tv.tv_sec;
    ts->tv_nsec = 1000L * (long)tv.tv_usec;
#endif
}

/**
 * @brief Get the difference of 2 timespecs in microseconds.
 *
 * @param[in] ts1 Smaller (older) timespec.
 * @param[in] ts2 Larger (later) timespec.
 * @return Difference of timespecs in usec.
 */
static uint64_t
time_diff(const struct timespec *ts1, const struct timespec *ts2)
{
    uint64_t usec_diff = 0;
    int64_t nsec_diff;

    assert(ts1->tv_sec <= ts2->tv_sec);

    /* seconds diff */
    usec_diff += (ts2->tv_sec - ts1->tv_sec) * 1000000;

    /* nanoseconds diff */
    nsec_diff = ts2->tv_nsec - ts1->tv_nsec;
    usec_diff += nsec_diff ? nsec_diff / 1000 : 0;

    return usec_diff;
}

/**
 * @brief Create data tree with list instances.
 *
 * @param[in] mod Module of the top-level node.
 * @param[in] offset Starting offset of the identifier number values.
 * @param[in] count Number of list instances to create, with increasing identifier numbers.
 * @param[out] data Created data.
 * @return LY_ERR value.
 */
static LY_ERR
create_list_inst(const struct lys_module *mod, uint32_t offset, uint32_t count, struct lyd_node **data)
{
    LY_ERR ret;
    uint32_t i;
    char k1_val[32], k2_val[32], l_val[32], lfl_val[32];
    struct lyd_node *list;

    if ((ret = lyd_new_inner(NULL, mod, "cont", 0, data))) {
        return ret;
    }

    for (i = 0; i < count; ++i) {
        sprintf(k1_val, "%" PRIu32, i + offset);
        sprintf(k2_val, "str%" PRIu32, i + offset);
        sprintf(l_val, "l%" PRIu32, i + offset);

        if ((ret = lyd_new_list(*data, NULL, "lst", 0, &list, k1_val, k2_val))) {
            return ret;
        }
        if ((ret = lyd_new_term(list, NULL, "l", l_val, 0, NULL))) {
            return ret;
        }
    }

    /* Last list contains a "lfl" leaf-list with @p count terms. */
    for (i = 0; i < count; ++i) {
        sprintf(lfl_val, "%" PRIu32, i + offset);
        if ((ret = lyd_new_term(list, NULL, "lfl", lfl_val, 0, NULL))) {
            return ret;
        }
    }

    return LY_SUCCESS;
}

/**
 * @brief Execute a test.
 *
 * @param[in] setup Setup callback to call once.
 * @param[in] test Test callback.
 * @param[in] name Name of the test.
 * @param[in] mod Module of testing data.
 * @param[in] count Count of list instances, size of the testing data set.
 * @param[in] tries Number of (re)tries of the test to get more accurate measurements.
 * @return LY_ERR value.
 */
static LY_ERR
exec_test(setup_cb setup, test_cb test, const char *name, const struct lys_module *mod, uint32_t count, uint32_t tries)
{
    LY_ERR ret;
    struct timespec ts_start, ts_end;
    struct test_state state = {0};
    const uint32_t name_fixed_len = 38;
    char str[name_fixed_len + 1];
    uint32_t i, printed;
    uint64_t time_usec = 0;

    /* print test start */
    printed = sprintf(str, "| %s ", name);
    while (printed + 2 < name_fixed_len) {
        printed += sprintf(str + printed, ".");
    }
    if (printed + 1 < name_fixed_len) {
        printed += sprintf(str + printed, " ");
    }
    sprintf(str + printed, "|");
    fputs(str, stdout);
    fflush(stdout);

    /* setup */
    if ((ret = setup(mod, count, &state))) {
        return ret;
    }

    /* test */
    for (i = 0; i < tries; ++i) {
        if ((ret = test(&state, &ts_start, &ts_end))) {
            return ret;
        }
        time_usec += time_diff(&ts_start, &ts_end);
    }
    time_usec /= tries;

    /* teardown */
    lyd_free_siblings(state.data1);
    lyd_free_siblings(state.data2);

    /* print time */
    printf(" %" PRIu64 ".%06" PRIu64 " s |\n", time_usec / 1000000, time_usec % 1000000);

    return LY_SUCCESS;
}

static void
TEST_START(struct timespec *ts)
{
    time_get(ts);

#ifdef HAVE_CALLGRIND
    CALLGRIND_START_INSTRUMENTATION;
#endif
}

static void
TEST_END(struct timespec *ts)
{
    time_get(ts);

#ifdef HAVE_CALLGRIND
    CALLGRIND_STOP_INSTRUMENTATION;
#endif
}

/* TEST SETUP */
static LY_ERR
setup_basic(const struct lys_module *mod, uint32_t count, struct test_state *state)
{
    state->mod = mod;
    state->count = count;

    return LY_SUCCESS;
}

static LY_ERR
setup_data_single_tree(const struct lys_module *mod, uint32_t count, struct test_state *state)
{
    state->mod = mod;
    state->count = count;

    return create_list_inst(mod, 0, count, &state->data1);
}

static LY_ERR
setup_data_same_trees(const struct lys_module *mod, uint32_t count, struct test_state *state)
{
    LY_ERR ret;

    state->mod = mod;
    state->count = count;

    if ((ret = create_list_inst(mod, 0, count, &state->data1))) {
        return ret;
    }
    if ((ret = create_list_inst(mod, 0, count, &state->data2))) {
        return ret;
    }

    return LY_SUCCESS;
}

static LY_ERR
setup_data_no_same_trees(const struct lys_module *mod, uint32_t count, struct test_state *state)
{
    LY_ERR ret;

    state->mod = mod;
    state->count = count;

    if ((ret = create_list_inst(mod, 0, count, &state->data1))) {
        return ret;
    }
    if ((ret = create_list_inst(mod, count, count, &state->data2))) {
        return ret;
    }

    return LY_SUCCESS;
}

static LY_ERR
setup_data_offset_tree(const struct lys_module *mod, uint32_t count, struct test_state *state)
{
    LY_ERR ret;

    state->mod = mod;
    state->count = count;

    if ((ret = create_list_inst(mod, count, count, &state->data2))) {
        return ret;
    }

    return LY_SUCCESS;
}

/* TEST CB */
static LY_ERR
test_create_new_text(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    LY_ERR r;
    struct lyd_node *data = NULL;

    TEST_START(ts_start);

    if ((r = create_list_inst(state->mod, 0, state->count, &data))) {
        return r;
    }

    TEST_END(ts_end);

    lyd_free_siblings(data);

    return LY_SUCCESS;
}

static LY_ERR
test_create_new_bin(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    LY_ERR r;
    struct lyd_node *data = NULL;
    uint32_t i, k2_len, l_len;
    char k2_val[32], l_val[32];
    struct lyd_node *list;

    TEST_START(ts_start);

    if ((r = lyd_new_inner(NULL, state->mod, "cont", 0, &data))) {
        return r;
    }

    for (i = 0; i < state->count; ++i) {
        k2_len = sprintf(k2_val, "str%" PRIu32, i);
        l_len = sprintf(l_val, "l%" PRIu32, i);

        if ((r = lyd_new_list_bin(data, NULL, "lst", 0, &list, &i, sizeof i, k2_val, k2_len))) {
            return r;
        }
        if ((r = lyd_new_term_bin(list, NULL, "l", l_val, l_len, 0, NULL))) {
            return r;
        }
    }

    TEST_END(ts_end);

    lyd_free_siblings(data);

    return LY_SUCCESS;
}

static LY_ERR
test_create_path(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    LY_ERR r;
    struct lyd_node *data = NULL;
    uint32_t i;
    char path[64], l_val[32];

    TEST_START(ts_start);

    if ((r = lyd_new_inner(NULL, state->mod, "cont", 0, &data))) {
        return r;
    }

    for (i = 0; i < state->count; ++i) {
        sprintf(path, "/perf:cont/lst[k1='%" PRIu32 "'][k2='str%" PRIu32 "']/l", i, i);
        sprintf(l_val, "l%" PRIu32, i);

        if ((r = lyd_new_path(data, NULL, path, l_val, 0, NULL))) {
            return r;
        }
    }

    TEST_END(ts_end);

    lyd_free_siblings(data);

    return LY_SUCCESS;
}

static LY_ERR
test_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    LY_ERR r;

    TEST_START(ts_start);

    if ((r = lyd_validate_all(&state->data1, NULL, LYD_VALIDATE_PRESENT, NULL))) {
        return r;
    }

    TEST_END(ts_end);

    return LY_SUCCESS;
}

static LY_ERR
_test_parse(struct test_state *state, LYD_FORMAT format, ly_bool use_file, uint32_t print_options, uint32_t parse_options,
        uint32_t validate_options, struct timespec *ts_start, struct timespec *ts_end)
{
    LY_ERR ret = LY_SUCCESS;
    struct lyd_node *data = NULL;
    char *buf = NULL;
    struct ly_in *in = NULL;

    if (use_file) {
        if ((ret = lyd_print_path(TEMP_FILE, state->data1, format, print_options))) {
            goto cleanup;
        }
        if ((ret = ly_in_new_filepath(TEMP_FILE, 0, &in))) {
            goto cleanup;
        }
    } else {
        if ((ret = lyd_print_mem(&buf, state->data1, format, print_options))) {
            goto cleanup;
        }
        if ((ret = ly_in_new_memory(buf, &in))) {
            goto cleanup;
        }
    }

    TEST_START(ts_start);

    if ((ret = lyd_parse_data(state->mod->ctx, NULL, in, format, parse_options, validate_options, &data))) {
        goto cleanup;
    }

    TEST_END(ts_end);

cleanup:
    free(buf);
    ly_in_free(in, 0);
    lyd_free_siblings(data);
    return ret;
}

static LY_ERR
test_parse_xml_mem_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    return _test_parse(state, LYD_XML, 0, LYD_PRINT_SHRINK, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, ts_start, ts_end);
}

static LY_ERR
test_parse_xml_mem_no_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    return _test_parse(state, LYD_XML, 0, LYD_PRINT_SHRINK, LYD_PARSE_STRICT | LYD_PARSE_ONLY | LYD_PARSE_ORDERED, 0,
            ts_start, ts_end);
}

static LY_ERR
test_parse_xml_file_no_validate_format(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    return _test_parse(state, LYD_XML, 1, 0, LYD_PARSE_STRICT | LYD_PARSE_ONLY | LYD_PARSE_ORDERED, 0, ts_start, ts_end);
}

static LY_ERR
test_parse_json_mem_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    return _test_parse(state, LYD_JSON, 0, LYD_PRINT_SHRINK, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, ts_start, ts_end);
}

static LY_ERR
test_parse_json_mem_no_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    return _test_parse(state, LYD_JSON, 0, LYD_PRINT_SHRINK, LYD_PARSE_STRICT | LYD_PARSE_ONLY | LYD_PARSE_ORDERED, 0,
            ts_start, ts_end);
}

static LY_ERR
test_parse_json_file_no_validate_format(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    return _test_parse(state, LYD_JSON, 1, 0, LYD_PARSE_STRICT | LYD_PARSE_ONLY | LYD_PARSE_ORDERED, 0, ts_start, ts_end);
}

static LY_ERR
test_parse_lyb_mem_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    return _test_parse(state, LYD_LYB, 0, LYD_PRINT_SHRINK, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, ts_start, ts_end);
}

static LY_ERR
test_parse_lyb_mem_no_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    return _test_parse(state, LYD_LYB, 0, LYD_PRINT_SHRINK, LYD_PARSE_STRICT | LYD_PARSE_ONLY | LYD_PARSE_ORDERED, 0,
            ts_start, ts_end);
}

static LY_ERR
test_parse_lyb_file_no_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    return _test_parse(state, LYD_LYB, 1, 0, LYD_PARSE_STRICT | LYD_PARSE_ONLY | LYD_PARSE_ORDERED, 0, ts_start, ts_end);
}

static LY_ERR
_test_print(struct test_state *state, LYD_FORMAT format, uint32_t print_options, struct timespec *ts_start,
        struct timespec *ts_end)
{
    LY_ERR ret = LY_SUCCESS;
    char *buf = NULL;

    TEST_START(ts_start);

    if ((ret = lyd_print_mem(&buf, state->data1, format, print_options))) {
        goto cleanup;
    }

    TEST_END(ts_end);

cleanup:
    free(buf);
    return ret;
}

static LY_ERR
test_print_xml(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    return _test_print(state, LYD_XML, LYD_PRINT_SHRINK, ts_start, ts_end);
}

static LY_ERR
test_print_json(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    return _test_print(state, LYD_JSON, LYD_PRINT_SHRINK, ts_start, ts_end);
}

static LY_ERR
test_print_lyb(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    return _test_print(state, LYD_LYB, LYD_PRINT_SHRINK, ts_start, ts_end);
}

static LY_ERR
test_dup(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    LY_ERR r;
    struct lyd_node *data;

    TEST_START(ts_start);

    if ((r = lyd_dup_siblings(state->data1, NULL, LYD_DUP_RECURSIVE, &data))) {
        return r;
    }

    TEST_END(ts_end);

    lyd_free_siblings(data);

    return LY_SUCCESS;
}

static LY_ERR
test_free(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    LY_ERR r;
    struct lyd_node *data;

    if ((r = create_list_inst(state->mod, 0, state->count, &data))) {
        return r;
    }

    TEST_START(ts_start);

    lyd_free_siblings(data);

    TEST_END(ts_end);

    return LY_SUCCESS;
}

static LY_ERR
test_xpath_find(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    LY_ERR r;
    struct ly_set *set;
    char path[64];

    sprintf(path, "/perf:cont/lst[k1=%" PRIu32 " and k2='str%" PRIu32 "']", state->count / 2, state->count / 2);

    TEST_START(ts_start);

    if ((r = lyd_find_xpath(state->data1, path, &set))) {
        return r;
    }

    TEST_END(ts_end);

    ly_set_free(set, NULL);

    return LY_SUCCESS;
}

static LY_ERR
test_xpath_find_hash(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    LY_ERR r;
    struct ly_set *set;
    char path[64];

    sprintf(path, "/perf:cont/lst[k1=%" PRIu32 "][k2='str%" PRIu32 "']", state->count / 2, state->count / 2);

    TEST_START(ts_start);

    if ((r = lyd_find_xpath(state->data1, path, &set))) {
        return r;
    }

    TEST_END(ts_end);

    ly_set_free(set, NULL);

    return LY_SUCCESS;
}

static LY_ERR
test_compare_same(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    LY_ERR r;

    TEST_START(ts_start);

    if ((r = lyd_compare_siblings(state->data1, state->data2, LYD_COMPARE_FULL_RECURSION))) {
        return r;
    }

    TEST_END(ts_end);

    return LY_SUCCESS;
}

static LY_ERR
test_diff_same(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    LY_ERR r;
    struct lyd_node *diff;

    TEST_START(ts_start);

    if ((r = lyd_diff_siblings(state->data1, state->data2, 0, &diff))) {
        return r;
    }

    TEST_END(ts_end);

    lyd_free_siblings(diff);

    return LY_SUCCESS;
}

static LY_ERR
test_diff_no_same(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    LY_ERR r;
    struct lyd_node *diff;

    TEST_START(ts_start);

    if ((r = lyd_diff_siblings(state->data1, state->data2, 0, &diff))) {
        return r;
    }

    TEST_END(ts_end);

    lyd_free_siblings(diff);

    return LY_SUCCESS;
}

static LY_ERR
test_merge_same(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    LY_ERR r;

    TEST_START(ts_start);

    if ((r = lyd_merge_siblings(&state->data1, state->data2, 0))) {
        return r;
    }

    TEST_END(ts_end);

    return LY_SUCCESS;
}

static LY_ERR
test_merge_no_same(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    LY_ERR r;
    struct lyd_node *data1;

    if ((r = create_list_inst(state->mod, 0, state->count, &data1))) {
        return r;
    }

    TEST_START(ts_start);

    if ((r = lyd_merge_siblings(&data1, state->data2, 0))) {
        return r;
    }

    TEST_END(ts_end);

    lyd_free_siblings(data1);

    return LY_SUCCESS;
}

static LY_ERR
test_merge_no_same_destruct(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
    LY_ERR r;
    struct lyd_node *data1, *data2;

    if ((r = create_list_inst(state->mod, 0, state->count, &data1))) {
        return r;
    }
    if ((r = create_list_inst(state->mod, state->count, state->count, &data2))) {
        return r;
    }

    TEST_START(ts_start);

    if ((r = lyd_merge_siblings(&data1, data2, LYD_MERGE_DESTRUCT))) {
        return r;
    }

    TEST_END(ts_end);

    lyd_free_siblings(data1);

    return LY_SUCCESS;
}

struct test tests[] = {
    {"create new text", setup_basic, test_create_new_text},
    {"create new bin", setup_basic, test_create_new_bin},
    {"create path", setup_basic, test_create_path},
    {"validate", setup_data_single_tree, test_validate},
    {"parse xml mem validate", setup_data_single_tree, test_parse_xml_mem_validate},
    {"parse xml mem no validate", setup_data_single_tree, test_parse_xml_mem_no_validate},
    {"parse xml file no validate format", setup_data_single_tree, test_parse_xml_file_no_validate_format},
    {"parse json mem validate", setup_data_single_tree, test_parse_json_mem_validate},
    {"parse json mem no validate", setup_data_single_tree, test_parse_json_mem_no_validate},
    {"parse json file no validate format", setup_data_single_tree, test_parse_json_file_no_validate_format},
    {"parse lyb mem validate", setup_data_single_tree, test_parse_lyb_mem_validate},
    {"parse lyb mem no validate", setup_data_single_tree, test_parse_lyb_mem_no_validate},
    {"parse lyb file no validate", setup_data_single_tree, test_parse_lyb_file_no_validate},
    {"print xml", setup_data_single_tree, test_print_xml},
    {"print json", setup_data_single_tree, test_print_json},
    {"print lyb", setup_data_single_tree, test_print_lyb},
    {"dup", setup_data_single_tree, test_dup},
    {"free", setup_basic, test_free},
    {"xpath find", setup_data_single_tree, test_xpath_find},
    {"xpath find hash", setup_data_single_tree, test_xpath_find_hash},
    {"compare same", setup_data_same_trees, test_compare_same},
    {"diff same", setup_data_same_trees, test_diff_same},
    {"diff no same", setup_data_no_same_trees, test_diff_no_same},
    {"merge same", setup_data_same_trees, test_merge_same},
    {"merge no same", setup_data_offset_tree, test_merge_no_same},
    {"merge no same destruct", setup_basic, test_merge_no_same_destruct},
};

int
main(int argc, char **argv)
{
    LY_ERR ret = LY_SUCCESS;
    struct ly_ctx *ctx = NULL;
    const struct lys_module *mod;
    uint32_t i, count, tries;

    if (argc < 3) {
        fprintf(stderr, "Usage:\n%s list-instance-count test-tries\n\n", argv[0]);
        return LY_EINVAL;
    }

    count = atoi(argv[1]);
    if (!count) {
        fprintf(stderr, "Invalid count \"%s\".\n", argv[1]);
        return LY_EINVAL;
    }

    tries = atoi(argv[2]);
    if (!tries) {
        fprintf(stderr, "Invalid tries \"%s\".\n", argv[2]);
        return LY_EINVAL;
    }

    printf("\nly_perf:\n\tdata set size: %" PRIu32 "\n\teach test executed: %" PRIu32 " %s\n\n", count, tries,
            (tries > 1) ? "times" : "time");

    /* create context */
    if ((ret = ly_ctx_new(TESTS_SRC "/perf", 0, &ctx))) {
        goto cleanup;
    }

    /* load modules */
    if (!(mod = ly_ctx_load_module(ctx, "perf", NULL, NULL))) {
        ret = LY_ENOTFOUND;
        goto cleanup;
    }

    /* tests */
    for (i = 0; i < (sizeof tests / sizeof(struct test)); ++i) {
        if ((ret = exec_test(tests[i].setup, tests[i].test, tests[i].name, mod, count, tries))) {
            goto cleanup;
        }
    }

    printf("\n");

cleanup:
    ly_ctx_destroy(ctx);
    return ret;
}