Michal Vasko | 99544ca | 2021-06-29 10:45:19 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file perf.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief performance tests |
| 5 | * |
| 6 | * Copyright (c) 2021 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #define _GNU_SOURCE |
| 16 | |
| 17 | #include <assert.h> |
| 18 | #include <inttypes.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <sys/time.h> |
| 21 | #include <time.h> |
| 22 | |
| 23 | #include "libyang.h" |
| 24 | #include "tests_config.h" |
| 25 | |
| 26 | #ifdef HAVE_CALLGRIND |
| 27 | # include <valgrind/callgrind.h> |
| 28 | #endif |
| 29 | |
| 30 | #define TEMP_FILE "perf_tmp" |
| 31 | |
| 32 | /** |
| 33 | * @brief Test state structure. |
| 34 | */ |
| 35 | struct test_state { |
| 36 | const struct lys_module *mod; |
| 37 | uint32_t count; |
| 38 | struct lyd_node *data1; |
| 39 | struct lyd_node *data2; |
| 40 | }; |
| 41 | |
| 42 | typedef LY_ERR (*setup_cb)(const struct lys_module *mod, uint32_t count, struct test_state *state); |
| 43 | |
| 44 | typedef LY_ERR (*test_cb)(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end); |
| 45 | |
| 46 | /** |
| 47 | * @brief Single test structure. |
| 48 | */ |
| 49 | struct test { |
| 50 | const char *name; |
| 51 | setup_cb setup; |
| 52 | test_cb test; |
| 53 | }; |
| 54 | |
| 55 | /** |
| 56 | * @brief Get current time as timespec. |
| 57 | * |
| 58 | * @param[out] ts Timespect to fill. |
| 59 | */ |
| 60 | static void |
| 61 | time_get(struct timespec *ts) |
| 62 | { |
| 63 | #ifdef CLOCK_MONOTONIC_RAW |
| 64 | clock_gettime(CLOCK_MONOTONIC_RAW, ts); |
| 65 | #elif defined (CLOCK_MONOTONIC) |
| 66 | clock_gettime(CLOCK_MONOTONIC, ts); |
| 67 | #elif defined (CLOCK_REALTIME) |
| 68 | /* no monotonic clock available, return realtime */ |
| 69 | clock_gettime(CLOCK_REALTIME, ts); |
| 70 | #else |
| 71 | int rc; |
| 72 | struct timeval tv; |
| 73 | |
| 74 | gettimeofday(&tv, NULL); |
| 75 | ts->tv_sec = (time_t)tv.tv_sec; |
| 76 | ts->tv_nsec = 1000L * (long)tv.tv_usec; |
| 77 | #endif |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @brief Get the difference of 2 timespecs in microseconds. |
| 82 | * |
| 83 | * @param[in] ts1 Smaller (older) timespec. |
| 84 | * @param[in] ts2 Larger (later) timespec. |
| 85 | * @return Difference of timespecs in usec. |
| 86 | */ |
| 87 | static uint64_t |
| 88 | time_diff(const struct timespec *ts1, const struct timespec *ts2) |
| 89 | { |
| 90 | uint64_t usec_diff = 0; |
| 91 | int64_t nsec_diff; |
| 92 | |
| 93 | assert(ts1->tv_sec <= ts2->tv_sec); |
| 94 | |
| 95 | /* seconds diff */ |
| 96 | usec_diff += (ts2->tv_sec - ts1->tv_sec) * 1000000; |
| 97 | |
| 98 | /* nanoseconds diff */ |
| 99 | nsec_diff = ts2->tv_nsec - ts1->tv_nsec; |
| 100 | usec_diff += nsec_diff ? nsec_diff / 1000 : 0; |
| 101 | |
| 102 | return usec_diff; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @brief Create data tree with list instances. |
| 107 | * |
| 108 | * @param[in] mod Module of the top-level node. |
| 109 | * @param[in] offset Starting offset of the identifier number values. |
| 110 | * @param[in] count Number of list instances to create, with increasing identifier numbers. |
| 111 | * @param[out] data Created data. |
| 112 | * @return LY_ERR value. |
| 113 | */ |
| 114 | static LY_ERR |
| 115 | create_list_inst(const struct lys_module *mod, uint32_t offset, uint32_t count, struct lyd_node **data) |
| 116 | { |
| 117 | LY_ERR ret; |
| 118 | uint32_t i; |
| 119 | char k1_val[32], k2_val[32], l_val[32]; |
| 120 | struct lyd_node *list; |
| 121 | |
| 122 | if ((ret = lyd_new_inner(NULL, mod, "cont", 0, data))) { |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | for (i = 0; i < count; ++i) { |
| 127 | sprintf(k1_val, "%" PRIu32, i + offset); |
| 128 | sprintf(k2_val, "str%" PRIu32, i + offset); |
| 129 | sprintf(l_val, "l%" PRIu32, i + offset); |
| 130 | |
| 131 | if ((ret = lyd_new_list(*data, NULL, "lst", 0, &list, k1_val, k2_val))) { |
| 132 | return ret; |
| 133 | } |
| 134 | if ((ret = lyd_new_term(list, NULL, "l", l_val, 0, NULL))) { |
| 135 | return ret; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return LY_SUCCESS; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @brief Execute a test. |
| 144 | * |
| 145 | * @param[in] setup Setup callback to call once. |
| 146 | * @param[in] test Test callback. |
| 147 | * @param[in] name Name of the test. |
| 148 | * @param[in] mod Module of testing data. |
| 149 | * @param[in] count Count of list instances, size of the testing data set. |
| 150 | * @param[in] tries Number of (re)tries of the test to get more accurate measurements. |
| 151 | * @return LY_ERR value. |
| 152 | */ |
| 153 | static LY_ERR |
| 154 | exec_test(setup_cb setup, test_cb test, const char *name, const struct lys_module *mod, uint32_t count, uint32_t tries) |
| 155 | { |
| 156 | LY_ERR ret; |
| 157 | struct timespec ts_start, ts_end; |
| 158 | struct test_state state = {0}; |
| 159 | const uint32_t name_fixed_len = 37; |
| 160 | char str[name_fixed_len + 1]; |
| 161 | uint32_t i, printed; |
| 162 | uint64_t time_usec = 0; |
| 163 | |
| 164 | /* print test start */ |
| 165 | printed = sprintf(str, "| %s ", name); |
| 166 | while (printed + 1 < name_fixed_len) { |
| 167 | printed += sprintf(str + printed, "."); |
| 168 | } |
| 169 | if (printed < name_fixed_len) { |
| 170 | printed += sprintf(str + printed, " "); |
| 171 | } |
| 172 | sprintf(str + printed, "|"); |
| 173 | fputs(str, stdout); |
| 174 | fflush(stdout); |
| 175 | |
| 176 | /* setup */ |
| 177 | if ((ret = setup(mod, count, &state))) { |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | /* test */ |
| 182 | for (i = 0; i < tries; ++i) { |
| 183 | if ((ret = test(&state, &ts_start, &ts_end))) { |
| 184 | return ret; |
| 185 | } |
| 186 | time_usec += time_diff(&ts_start, &ts_end); |
| 187 | } |
| 188 | time_usec /= tries; |
| 189 | |
| 190 | /* teardown */ |
| 191 | lyd_free_siblings(state.data1); |
| 192 | lyd_free_siblings(state.data2); |
| 193 | |
| 194 | /* print time */ |
| 195 | printf(" %" PRIu64 ".%06" PRIu64 " s |\n", time_usec / 1000000, time_usec % 1000000); |
| 196 | |
| 197 | return LY_SUCCESS; |
| 198 | } |
| 199 | |
| 200 | static void |
| 201 | TEST_START(struct timespec *ts) |
| 202 | { |
| 203 | time_get(ts); |
| 204 | |
| 205 | #ifdef HAVE_CALLGRIND |
| 206 | CALLGRIND_START_INSTRUMENTATION; |
| 207 | #endif |
| 208 | } |
| 209 | |
| 210 | static void |
| 211 | TEST_END(struct timespec *ts) |
| 212 | { |
| 213 | time_get(ts); |
| 214 | |
| 215 | #ifdef HAVE_CALLGRIND |
| 216 | CALLGRIND_STOP_INSTRUMENTATION; |
| 217 | #endif |
| 218 | } |
| 219 | |
| 220 | /* TEST SETUP */ |
| 221 | static LY_ERR |
| 222 | setup_basic(const struct lys_module *mod, uint32_t count, struct test_state *state) |
| 223 | { |
| 224 | state->mod = mod; |
| 225 | state->count = count; |
| 226 | |
| 227 | return LY_SUCCESS; |
| 228 | } |
| 229 | |
| 230 | static LY_ERR |
| 231 | setup_data_single_tree(const struct lys_module *mod, uint32_t count, struct test_state *state) |
| 232 | { |
| 233 | state->mod = mod; |
| 234 | state->count = count; |
| 235 | |
| 236 | return create_list_inst(mod, 0, count, &state->data1); |
| 237 | } |
| 238 | |
| 239 | static LY_ERR |
| 240 | setup_data_same_trees(const struct lys_module *mod, uint32_t count, struct test_state *state) |
| 241 | { |
| 242 | LY_ERR ret; |
| 243 | |
| 244 | state->mod = mod; |
| 245 | state->count = count; |
| 246 | |
| 247 | if ((ret = create_list_inst(mod, 0, count, &state->data1))) { |
| 248 | return ret; |
| 249 | } |
| 250 | if ((ret = create_list_inst(mod, 0, count, &state->data2))) { |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | return LY_SUCCESS; |
| 255 | } |
| 256 | |
| 257 | static LY_ERR |
| 258 | setup_data_no_same_trees(const struct lys_module *mod, uint32_t count, struct test_state *state) |
| 259 | { |
| 260 | LY_ERR ret; |
| 261 | |
| 262 | state->mod = mod; |
| 263 | state->count = count; |
| 264 | |
| 265 | if ((ret = create_list_inst(mod, 0, count, &state->data1))) { |
| 266 | return ret; |
| 267 | } |
| 268 | if ((ret = create_list_inst(mod, count, count, &state->data2))) { |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | return LY_SUCCESS; |
| 273 | } |
| 274 | |
| 275 | static LY_ERR |
| 276 | setup_data_offset_tree(const struct lys_module *mod, uint32_t count, struct test_state *state) |
| 277 | { |
| 278 | LY_ERR ret; |
| 279 | |
| 280 | state->mod = mod; |
| 281 | state->count = count; |
| 282 | |
| 283 | if ((ret = create_list_inst(mod, count, count, &state->data2))) { |
| 284 | return ret; |
| 285 | } |
| 286 | |
| 287 | return LY_SUCCESS; |
| 288 | } |
| 289 | |
| 290 | /* TEST CB */ |
| 291 | static LY_ERR |
| 292 | test_create_new_text(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 293 | { |
| 294 | LY_ERR r; |
| 295 | struct lyd_node *data = NULL; |
| 296 | |
| 297 | TEST_START(ts_start); |
| 298 | |
| 299 | if ((r = create_list_inst(state->mod, 0, state->count, &data))) { |
| 300 | return r; |
| 301 | } |
| 302 | |
| 303 | TEST_END(ts_end); |
| 304 | |
| 305 | lyd_free_siblings(data); |
| 306 | |
| 307 | return LY_SUCCESS; |
| 308 | } |
| 309 | |
| 310 | static LY_ERR |
| 311 | test_create_new_bin(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 312 | { |
| 313 | LY_ERR r; |
| 314 | struct lyd_node *data = NULL; |
| 315 | uint32_t i, k2_len, l_len; |
| 316 | char k2_val[32], l_val[32]; |
| 317 | struct lyd_node *list; |
| 318 | |
| 319 | TEST_START(ts_start); |
| 320 | |
| 321 | if ((r = lyd_new_inner(NULL, state->mod, "cont", 0, &data))) { |
| 322 | return r; |
| 323 | } |
| 324 | |
| 325 | for (i = 0; i < state->count; ++i) { |
| 326 | k2_len = sprintf(k2_val, "str%" PRIu32, i); |
| 327 | l_len = sprintf(l_val, "l%" PRIu32, i); |
| 328 | |
| 329 | if ((r = lyd_new_list_bin(data, NULL, "lst", 0, &list, &i, sizeof i, k2_val, k2_len))) { |
| 330 | return r; |
| 331 | } |
| 332 | if ((r = lyd_new_term_bin(list, NULL, "l", l_val, l_len, 0, NULL))) { |
| 333 | return r; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | TEST_END(ts_end); |
| 338 | |
| 339 | lyd_free_siblings(data); |
| 340 | |
| 341 | return LY_SUCCESS; |
| 342 | } |
| 343 | |
| 344 | static LY_ERR |
| 345 | test_create_path(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 346 | { |
| 347 | LY_ERR r; |
| 348 | struct lyd_node *data = NULL; |
| 349 | uint32_t i; |
| 350 | char path[64], l_val[32]; |
| 351 | |
| 352 | TEST_START(ts_start); |
| 353 | |
| 354 | if ((r = lyd_new_inner(NULL, state->mod, "cont", 0, &data))) { |
| 355 | return r; |
| 356 | } |
| 357 | |
| 358 | for (i = 0; i < state->count; ++i) { |
| 359 | sprintf(path, "/perf:cont/lst[k1='%" PRIu32 "'][k2='str%" PRIu32 "']/l", i, i); |
| 360 | sprintf(l_val, "l%" PRIu32, i); |
| 361 | |
| 362 | if ((r = lyd_new_path(data, NULL, path, l_val, 0, NULL))) { |
| 363 | return r; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | TEST_END(ts_end); |
| 368 | |
| 369 | lyd_free_siblings(data); |
| 370 | |
| 371 | return LY_SUCCESS; |
| 372 | } |
| 373 | |
| 374 | static LY_ERR |
Michal Vasko | c606f31 | 2021-06-30 14:08:54 +0200 | [diff] [blame^] | 375 | test_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 376 | { |
| 377 | LY_ERR r; |
| 378 | |
| 379 | TEST_START(ts_start); |
| 380 | |
| 381 | if ((r = lyd_validate_all(&state->data1, NULL, LYD_VALIDATE_PRESENT, NULL))) { |
| 382 | return r; |
| 383 | } |
| 384 | |
| 385 | TEST_END(ts_end); |
| 386 | |
| 387 | return LY_SUCCESS; |
| 388 | } |
| 389 | |
| 390 | static LY_ERR |
Michal Vasko | 99544ca | 2021-06-29 10:45:19 +0200 | [diff] [blame] | 391 | _test_parse(struct test_state *state, LYD_FORMAT format, ly_bool use_file, uint32_t print_options, uint32_t parse_options, |
| 392 | uint32_t validate_options, struct timespec *ts_start, struct timespec *ts_end) |
| 393 | { |
| 394 | LY_ERR ret = LY_SUCCESS; |
| 395 | struct lyd_node *data = NULL; |
| 396 | char *buf = NULL; |
| 397 | struct ly_in *in = NULL; |
| 398 | |
| 399 | if (use_file) { |
| 400 | if ((ret = lyd_print_path(TEMP_FILE, state->data1, format, print_options))) { |
| 401 | goto cleanup; |
| 402 | } |
| 403 | if ((ret = ly_in_new_filepath(TEMP_FILE, 0, &in))) { |
| 404 | goto cleanup; |
| 405 | } |
| 406 | } else { |
| 407 | if ((ret = lyd_print_mem(&buf, state->data1, format, print_options))) { |
| 408 | goto cleanup; |
| 409 | } |
| 410 | if ((ret = ly_in_new_memory(buf, &in))) { |
| 411 | goto cleanup; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | TEST_START(ts_start); |
| 416 | |
| 417 | if ((ret = lyd_parse_data(state->mod->ctx, NULL, in, format, parse_options, validate_options, &data))) { |
| 418 | goto cleanup; |
| 419 | } |
| 420 | |
| 421 | TEST_END(ts_end); |
| 422 | |
| 423 | cleanup: |
| 424 | free(buf); |
| 425 | ly_in_free(in, 0); |
| 426 | lyd_free_siblings(data); |
| 427 | return ret; |
| 428 | } |
| 429 | |
| 430 | static LY_ERR |
| 431 | test_parse_xml_mem_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 432 | { |
| 433 | return _test_parse(state, LYD_XML, 0, LYD_PRINT_SHRINK, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, ts_start, ts_end); |
| 434 | } |
| 435 | |
| 436 | static LY_ERR |
| 437 | test_parse_xml_mem_no_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 438 | { |
| 439 | return _test_parse(state, LYD_XML, 0, LYD_PRINT_SHRINK, LYD_PARSE_STRICT | LYD_PARSE_ONLY, 0, ts_start, ts_end); |
| 440 | } |
| 441 | |
| 442 | static LY_ERR |
| 443 | test_parse_xml_file_no_validate_format(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 444 | { |
| 445 | return _test_parse(state, LYD_XML, 1, 0, LYD_PARSE_STRICT | LYD_PARSE_ONLY, 0, ts_start, ts_end); |
| 446 | } |
| 447 | |
| 448 | static LY_ERR |
| 449 | test_parse_json_mem_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 450 | { |
| 451 | return _test_parse(state, LYD_JSON, 0, LYD_PRINT_SHRINK, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, ts_start, ts_end); |
| 452 | } |
| 453 | |
| 454 | static LY_ERR |
| 455 | test_parse_json_mem_no_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 456 | { |
| 457 | return _test_parse(state, LYD_JSON, 0, LYD_PRINT_SHRINK, LYD_PARSE_STRICT | LYD_PARSE_ONLY, 0, ts_start, ts_end); |
| 458 | } |
| 459 | |
| 460 | static LY_ERR |
| 461 | test_parse_json_file_no_validate_format(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 462 | { |
| 463 | return _test_parse(state, LYD_JSON, 1, 0, LYD_PARSE_STRICT | LYD_PARSE_ONLY, 0, ts_start, ts_end); |
| 464 | } |
| 465 | |
| 466 | static LY_ERR |
| 467 | test_parse_lyb_mem_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 468 | { |
| 469 | return _test_parse(state, LYD_LYB, 0, LYD_PRINT_SHRINK, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, ts_start, ts_end); |
| 470 | } |
| 471 | |
| 472 | static LY_ERR |
| 473 | test_parse_lyb_mem_no_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 474 | { |
| 475 | return _test_parse(state, LYD_LYB, 0, LYD_PRINT_SHRINK, LYD_PARSE_STRICT | LYD_PARSE_ONLY, 0, ts_start, ts_end); |
| 476 | } |
| 477 | |
| 478 | static LY_ERR |
| 479 | test_parse_lyb_file_no_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 480 | { |
| 481 | return _test_parse(state, LYD_LYB, 1, 0, LYD_PARSE_STRICT | LYD_PARSE_ONLY, 0, ts_start, ts_end); |
| 482 | } |
| 483 | |
| 484 | static LY_ERR |
| 485 | _test_print(struct test_state *state, LYD_FORMAT format, uint32_t print_options, struct timespec *ts_start, |
| 486 | struct timespec *ts_end) |
| 487 | { |
| 488 | LY_ERR ret = LY_SUCCESS; |
| 489 | char *buf = NULL; |
| 490 | |
| 491 | TEST_START(ts_start); |
| 492 | |
| 493 | if ((ret = lyd_print_mem(&buf, state->data1, format, print_options))) { |
| 494 | goto cleanup; |
| 495 | } |
| 496 | |
| 497 | TEST_END(ts_end); |
| 498 | |
| 499 | cleanup: |
| 500 | free(buf); |
| 501 | return ret; |
| 502 | } |
| 503 | |
| 504 | static LY_ERR |
| 505 | test_print_xml(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 506 | { |
| 507 | return _test_print(state, LYD_XML, LYD_PRINT_SHRINK, ts_start, ts_end); |
| 508 | } |
| 509 | |
| 510 | static LY_ERR |
| 511 | test_print_json(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 512 | { |
| 513 | return _test_print(state, LYD_JSON, LYD_PRINT_SHRINK, ts_start, ts_end); |
| 514 | } |
| 515 | |
| 516 | static LY_ERR |
| 517 | test_print_lyb(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 518 | { |
| 519 | return _test_print(state, LYD_LYB, LYD_PRINT_SHRINK, ts_start, ts_end); |
| 520 | } |
| 521 | |
| 522 | static LY_ERR |
| 523 | test_dup(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 524 | { |
| 525 | LY_ERR r; |
| 526 | struct lyd_node *data; |
| 527 | |
| 528 | TEST_START(ts_start); |
| 529 | |
| 530 | if ((r = lyd_dup_siblings(state->data1, NULL, LYD_DUP_RECURSIVE, &data))) { |
| 531 | return r; |
| 532 | } |
| 533 | |
| 534 | TEST_END(ts_end); |
| 535 | |
| 536 | lyd_free_siblings(data); |
| 537 | |
| 538 | return LY_SUCCESS; |
| 539 | } |
| 540 | |
| 541 | static LY_ERR |
| 542 | test_free(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 543 | { |
| 544 | LY_ERR r; |
| 545 | struct lyd_node *data; |
| 546 | |
| 547 | if ((r = create_list_inst(state->mod, 0, state->count, &data))) { |
| 548 | return r; |
| 549 | } |
| 550 | |
| 551 | TEST_START(ts_start); |
| 552 | |
| 553 | lyd_free_siblings(data); |
| 554 | |
| 555 | TEST_END(ts_end); |
| 556 | |
| 557 | return LY_SUCCESS; |
| 558 | } |
| 559 | |
| 560 | static LY_ERR |
| 561 | test_xpath_find(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 562 | { |
| 563 | LY_ERR r; |
| 564 | struct ly_set *set; |
| 565 | char path[64]; |
| 566 | |
| 567 | sprintf(path, "/perf:cont/lst[k1=%" PRIu32 " and k2='str%" PRIu32 "']", state->count / 2, state->count / 2); |
| 568 | |
| 569 | TEST_START(ts_start); |
| 570 | |
| 571 | if ((r = lyd_find_xpath(state->data1, path, &set))) { |
| 572 | return r; |
| 573 | } |
| 574 | |
| 575 | TEST_END(ts_end); |
| 576 | |
| 577 | ly_set_free(set, NULL); |
| 578 | |
| 579 | return LY_SUCCESS; |
| 580 | } |
| 581 | |
| 582 | static LY_ERR |
| 583 | test_xpath_find_hash(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 584 | { |
| 585 | LY_ERR r; |
| 586 | struct ly_set *set; |
| 587 | char path[64]; |
| 588 | |
| 589 | sprintf(path, "/perf:cont/lst[k1=%" PRIu32 "][k2='str%" PRIu32 "']", state->count / 2, state->count / 2); |
| 590 | |
| 591 | TEST_START(ts_start); |
| 592 | |
| 593 | if ((r = lyd_find_xpath(state->data1, path, &set))) { |
| 594 | return r; |
| 595 | } |
| 596 | |
| 597 | TEST_END(ts_end); |
| 598 | |
| 599 | ly_set_free(set, NULL); |
| 600 | |
| 601 | return LY_SUCCESS; |
| 602 | } |
| 603 | |
| 604 | static LY_ERR |
| 605 | test_compare_same(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 606 | { |
| 607 | LY_ERR r; |
| 608 | |
| 609 | TEST_START(ts_start); |
| 610 | |
| 611 | if ((r = lyd_compare_siblings(state->data1, state->data2, LYD_COMPARE_FULL_RECURSION))) { |
| 612 | return r; |
| 613 | } |
| 614 | |
| 615 | TEST_END(ts_end); |
| 616 | |
| 617 | return LY_SUCCESS; |
| 618 | } |
| 619 | |
| 620 | static LY_ERR |
| 621 | test_diff_same(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 622 | { |
| 623 | LY_ERR r; |
| 624 | struct lyd_node *diff; |
| 625 | |
| 626 | TEST_START(ts_start); |
| 627 | |
| 628 | if ((r = lyd_diff_siblings(state->data1, state->data2, 0, &diff))) { |
| 629 | return r; |
| 630 | } |
| 631 | |
| 632 | TEST_END(ts_end); |
| 633 | |
| 634 | lyd_free_siblings(diff); |
| 635 | |
| 636 | return LY_SUCCESS; |
| 637 | } |
| 638 | |
| 639 | static LY_ERR |
| 640 | test_diff_no_same(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 641 | { |
| 642 | LY_ERR r; |
| 643 | struct lyd_node *diff; |
| 644 | |
| 645 | TEST_START(ts_start); |
| 646 | |
| 647 | if ((r = lyd_diff_siblings(state->data1, state->data2, 0, &diff))) { |
| 648 | return r; |
| 649 | } |
| 650 | |
| 651 | TEST_END(ts_end); |
| 652 | |
| 653 | lyd_free_siblings(diff); |
| 654 | |
| 655 | return LY_SUCCESS; |
| 656 | } |
| 657 | |
| 658 | static LY_ERR |
| 659 | test_merge_same(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 660 | { |
| 661 | LY_ERR r; |
| 662 | |
| 663 | TEST_START(ts_start); |
| 664 | |
| 665 | if ((r = lyd_merge_siblings(&state->data1, state->data2, 0))) { |
| 666 | return r; |
| 667 | } |
| 668 | |
| 669 | TEST_END(ts_end); |
| 670 | |
| 671 | return LY_SUCCESS; |
| 672 | } |
| 673 | |
| 674 | static LY_ERR |
| 675 | test_merge_no_same(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 676 | { |
| 677 | LY_ERR r; |
| 678 | struct lyd_node *data1; |
| 679 | |
| 680 | if ((r = create_list_inst(state->mod, 0, state->count, &data1))) { |
| 681 | return r; |
| 682 | } |
| 683 | |
| 684 | TEST_START(ts_start); |
| 685 | |
| 686 | if ((r = lyd_merge_siblings(&data1, state->data2, 0))) { |
| 687 | return r; |
| 688 | } |
| 689 | |
| 690 | TEST_END(ts_end); |
| 691 | |
| 692 | lyd_free_siblings(data1); |
| 693 | |
| 694 | return LY_SUCCESS; |
| 695 | } |
| 696 | |
| 697 | static LY_ERR |
| 698 | test_merge_no_same_destruct(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 699 | { |
| 700 | LY_ERR r; |
| 701 | struct lyd_node *data1, *data2; |
| 702 | |
| 703 | if ((r = create_list_inst(state->mod, 0, state->count, &data1))) { |
| 704 | return r; |
| 705 | } |
| 706 | if ((r = create_list_inst(state->mod, state->count, state->count, &data2))) { |
| 707 | return r; |
| 708 | } |
| 709 | |
| 710 | TEST_START(ts_start); |
| 711 | |
| 712 | if ((r = lyd_merge_siblings(&data1, data2, LYD_MERGE_DESTRUCT))) { |
| 713 | return r; |
| 714 | } |
| 715 | |
| 716 | TEST_END(ts_end); |
| 717 | |
| 718 | lyd_free_siblings(data1); |
| 719 | |
| 720 | return LY_SUCCESS; |
| 721 | } |
| 722 | |
| 723 | struct test tests[] = { |
| 724 | { "create new text", setup_basic, test_create_new_text }, |
| 725 | { "create new bin", setup_basic, test_create_new_bin }, |
| 726 | { "create path", setup_basic, test_create_path }, |
Michal Vasko | c606f31 | 2021-06-30 14:08:54 +0200 | [diff] [blame^] | 727 | { "validate", setup_data_single_tree, test_validate }, |
Michal Vasko | 99544ca | 2021-06-29 10:45:19 +0200 | [diff] [blame] | 728 | { "parse xml mem validate", setup_data_single_tree, test_parse_xml_mem_validate }, |
| 729 | { "parse xml mem no validate", setup_data_single_tree, test_parse_xml_mem_no_validate }, |
| 730 | { "parse xml file no validate format", setup_data_single_tree, test_parse_xml_file_no_validate_format }, |
| 731 | { "parse json mem validate", setup_data_single_tree, test_parse_json_mem_validate }, |
| 732 | { "parse json mem no validate", setup_data_single_tree, test_parse_json_mem_no_validate }, |
| 733 | { "parse json file no validate format", setup_data_single_tree, test_parse_json_file_no_validate_format }, |
| 734 | { "parse lyb mem validate", setup_data_single_tree, test_parse_lyb_mem_validate }, |
| 735 | { "parse lyb mem no validate", setup_data_single_tree, test_parse_lyb_mem_no_validate }, |
| 736 | { "parse lyb file no validate", setup_data_single_tree, test_parse_lyb_file_no_validate }, |
| 737 | { "print xml", setup_data_single_tree, test_print_xml }, |
| 738 | { "print json", setup_data_single_tree, test_print_json }, |
| 739 | { "print lyb", setup_data_single_tree, test_print_lyb }, |
| 740 | { "dup", setup_data_single_tree, test_dup }, |
| 741 | { "free", setup_basic, test_free }, |
| 742 | { "xpath find", setup_data_single_tree, test_xpath_find }, |
| 743 | { "xpath find hash", setup_data_single_tree, test_xpath_find_hash }, |
| 744 | { "compare same", setup_data_same_trees, test_compare_same }, |
| 745 | { "diff same", setup_data_same_trees, test_diff_same }, |
| 746 | { "diff no same", setup_data_no_same_trees, test_diff_no_same }, |
| 747 | { "merge same", setup_data_same_trees, test_merge_same }, |
| 748 | { "merge no same", setup_data_offset_tree, test_merge_no_same }, |
| 749 | { "merge no same destruct", setup_basic, test_merge_no_same_destruct }, |
| 750 | }; |
| 751 | |
| 752 | int |
| 753 | main(int argc, char **argv) |
| 754 | { |
| 755 | LY_ERR ret = LY_SUCCESS; |
| 756 | struct ly_ctx *ctx = NULL; |
| 757 | const struct lys_module *mod; |
| 758 | uint32_t i, count, tries; |
| 759 | |
| 760 | if (argc < 3) { |
| 761 | fprintf(stderr, "Usage:\n%s list-instance-count test-tries\n\n", argv[0]); |
| 762 | return LY_EINVAL; |
| 763 | } |
| 764 | |
| 765 | count = atoi(argv[1]); |
| 766 | if (!count) { |
| 767 | fprintf(stderr, "Invalid count \"%s\".\n", argv[1]); |
| 768 | return LY_EINVAL; |
| 769 | } |
| 770 | |
| 771 | tries = atoi(argv[2]); |
| 772 | if (!tries) { |
| 773 | fprintf(stderr, "Invalid tries \"%s\".\n", argv[2]); |
| 774 | return LY_EINVAL; |
| 775 | } |
| 776 | |
| 777 | printf("\nly_perf:\n\tdata set size: %" PRIu32 "\n\teach test executed: %" PRIu32 " %s\n\n", count, tries, |
| 778 | (tries > 1) ? "times" : "time"); |
| 779 | |
| 780 | /* create context */ |
| 781 | if ((ret = ly_ctx_new(TESTS_SRC "/perf", 0, &ctx))) { |
| 782 | goto cleanup; |
| 783 | } |
| 784 | |
| 785 | /* load modules */ |
| 786 | if (!(mod = ly_ctx_load_module(ctx, "perf", NULL, NULL))) { |
| 787 | ret = LY_ENOTFOUND; |
| 788 | goto cleanup; |
| 789 | } |
| 790 | |
| 791 | /* tests */ |
| 792 | for (i = 0; i < (sizeof tests / sizeof(struct test)); ++i) { |
| 793 | if ((ret = exec_test(tests[i].setup, tests[i].test, tests[i].name, mod, count, tries))) { |
| 794 | goto cleanup; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | printf("\n"); |
| 799 | |
| 800 | cleanup: |
| 801 | ly_ctx_destroy(ctx); |
| 802 | return ret; |
| 803 | } |