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; |
aPiecek | b54dbdb | 2021-09-02 14:39:15 +0200 | [diff] [blame^] | 119 | char k1_val[32], k2_val[32], l_val[32], lfl_val[32]; |
Michal Vasko | 99544ca | 2021-06-29 10:45:19 +0200 | [diff] [blame] | 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 | |
aPiecek | b54dbdb | 2021-09-02 14:39:15 +0200 | [diff] [blame^] | 139 | /* Last list contains a "lfl" leaf-list with @p count terms. */ |
| 140 | for (i = 0; i < count; ++i) { |
| 141 | sprintf(lfl_val, "%" PRIu32, i + offset); |
| 142 | if ((ret = lyd_new_term(list, NULL, "lfl", lfl_val, 0, NULL))) { |
| 143 | return ret; |
| 144 | } |
| 145 | } |
| 146 | |
Michal Vasko | 99544ca | 2021-06-29 10:45:19 +0200 | [diff] [blame] | 147 | return LY_SUCCESS; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @brief Execute a test. |
| 152 | * |
| 153 | * @param[in] setup Setup callback to call once. |
| 154 | * @param[in] test Test callback. |
| 155 | * @param[in] name Name of the test. |
| 156 | * @param[in] mod Module of testing data. |
| 157 | * @param[in] count Count of list instances, size of the testing data set. |
| 158 | * @param[in] tries Number of (re)tries of the test to get more accurate measurements. |
| 159 | * @return LY_ERR value. |
| 160 | */ |
| 161 | static LY_ERR |
| 162 | exec_test(setup_cb setup, test_cb test, const char *name, const struct lys_module *mod, uint32_t count, uint32_t tries) |
| 163 | { |
| 164 | LY_ERR ret; |
| 165 | struct timespec ts_start, ts_end; |
| 166 | struct test_state state = {0}; |
Michal Vasko | 4318ac3 | 2021-07-02 09:18:30 +0200 | [diff] [blame] | 167 | const uint32_t name_fixed_len = 38; |
Michal Vasko | 99544ca | 2021-06-29 10:45:19 +0200 | [diff] [blame] | 168 | char str[name_fixed_len + 1]; |
| 169 | uint32_t i, printed; |
| 170 | uint64_t time_usec = 0; |
| 171 | |
| 172 | /* print test start */ |
| 173 | printed = sprintf(str, "| %s ", name); |
Michal Vasko | 4318ac3 | 2021-07-02 09:18:30 +0200 | [diff] [blame] | 174 | while (printed + 2 < name_fixed_len) { |
Michal Vasko | 99544ca | 2021-06-29 10:45:19 +0200 | [diff] [blame] | 175 | printed += sprintf(str + printed, "."); |
| 176 | } |
Michal Vasko | 4318ac3 | 2021-07-02 09:18:30 +0200 | [diff] [blame] | 177 | if (printed + 1 < name_fixed_len) { |
Michal Vasko | 99544ca | 2021-06-29 10:45:19 +0200 | [diff] [blame] | 178 | printed += sprintf(str + printed, " "); |
| 179 | } |
| 180 | sprintf(str + printed, "|"); |
| 181 | fputs(str, stdout); |
| 182 | fflush(stdout); |
| 183 | |
| 184 | /* setup */ |
| 185 | if ((ret = setup(mod, count, &state))) { |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | /* test */ |
| 190 | for (i = 0; i < tries; ++i) { |
| 191 | if ((ret = test(&state, &ts_start, &ts_end))) { |
| 192 | return ret; |
| 193 | } |
| 194 | time_usec += time_diff(&ts_start, &ts_end); |
| 195 | } |
| 196 | time_usec /= tries; |
| 197 | |
| 198 | /* teardown */ |
| 199 | lyd_free_siblings(state.data1); |
| 200 | lyd_free_siblings(state.data2); |
| 201 | |
| 202 | /* print time */ |
| 203 | printf(" %" PRIu64 ".%06" PRIu64 " s |\n", time_usec / 1000000, time_usec % 1000000); |
| 204 | |
| 205 | return LY_SUCCESS; |
| 206 | } |
| 207 | |
| 208 | static void |
| 209 | TEST_START(struct timespec *ts) |
| 210 | { |
| 211 | time_get(ts); |
| 212 | |
| 213 | #ifdef HAVE_CALLGRIND |
| 214 | CALLGRIND_START_INSTRUMENTATION; |
| 215 | #endif |
| 216 | } |
| 217 | |
| 218 | static void |
| 219 | TEST_END(struct timespec *ts) |
| 220 | { |
| 221 | time_get(ts); |
| 222 | |
| 223 | #ifdef HAVE_CALLGRIND |
| 224 | CALLGRIND_STOP_INSTRUMENTATION; |
| 225 | #endif |
| 226 | } |
| 227 | |
| 228 | /* TEST SETUP */ |
| 229 | static LY_ERR |
| 230 | setup_basic(const struct lys_module *mod, uint32_t count, struct test_state *state) |
| 231 | { |
| 232 | state->mod = mod; |
| 233 | state->count = count; |
| 234 | |
| 235 | return LY_SUCCESS; |
| 236 | } |
| 237 | |
| 238 | static LY_ERR |
| 239 | setup_data_single_tree(const struct lys_module *mod, uint32_t count, struct test_state *state) |
| 240 | { |
| 241 | state->mod = mod; |
| 242 | state->count = count; |
| 243 | |
| 244 | return create_list_inst(mod, 0, count, &state->data1); |
| 245 | } |
| 246 | |
| 247 | static LY_ERR |
| 248 | setup_data_same_trees(const struct lys_module *mod, uint32_t count, struct test_state *state) |
| 249 | { |
| 250 | LY_ERR ret; |
| 251 | |
| 252 | state->mod = mod; |
| 253 | state->count = count; |
| 254 | |
| 255 | if ((ret = create_list_inst(mod, 0, count, &state->data1))) { |
| 256 | return ret; |
| 257 | } |
| 258 | if ((ret = create_list_inst(mod, 0, count, &state->data2))) { |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | return LY_SUCCESS; |
| 263 | } |
| 264 | |
| 265 | static LY_ERR |
| 266 | setup_data_no_same_trees(const struct lys_module *mod, uint32_t count, struct test_state *state) |
| 267 | { |
| 268 | LY_ERR ret; |
| 269 | |
| 270 | state->mod = mod; |
| 271 | state->count = count; |
| 272 | |
| 273 | if ((ret = create_list_inst(mod, 0, count, &state->data1))) { |
| 274 | return ret; |
| 275 | } |
| 276 | if ((ret = create_list_inst(mod, count, count, &state->data2))) { |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | return LY_SUCCESS; |
| 281 | } |
| 282 | |
| 283 | static LY_ERR |
| 284 | setup_data_offset_tree(const struct lys_module *mod, uint32_t count, struct test_state *state) |
| 285 | { |
| 286 | LY_ERR ret; |
| 287 | |
| 288 | state->mod = mod; |
| 289 | state->count = count; |
| 290 | |
| 291 | if ((ret = create_list_inst(mod, count, count, &state->data2))) { |
| 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | return LY_SUCCESS; |
| 296 | } |
| 297 | |
| 298 | /* TEST CB */ |
| 299 | static LY_ERR |
| 300 | test_create_new_text(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 301 | { |
| 302 | LY_ERR r; |
| 303 | struct lyd_node *data = NULL; |
| 304 | |
| 305 | TEST_START(ts_start); |
| 306 | |
| 307 | if ((r = create_list_inst(state->mod, 0, state->count, &data))) { |
| 308 | return r; |
| 309 | } |
| 310 | |
| 311 | TEST_END(ts_end); |
| 312 | |
| 313 | lyd_free_siblings(data); |
| 314 | |
| 315 | return LY_SUCCESS; |
| 316 | } |
| 317 | |
| 318 | static LY_ERR |
| 319 | test_create_new_bin(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 320 | { |
| 321 | LY_ERR r; |
| 322 | struct lyd_node *data = NULL; |
| 323 | uint32_t i, k2_len, l_len; |
| 324 | char k2_val[32], l_val[32]; |
| 325 | struct lyd_node *list; |
| 326 | |
| 327 | TEST_START(ts_start); |
| 328 | |
| 329 | if ((r = lyd_new_inner(NULL, state->mod, "cont", 0, &data))) { |
| 330 | return r; |
| 331 | } |
| 332 | |
| 333 | for (i = 0; i < state->count; ++i) { |
| 334 | k2_len = sprintf(k2_val, "str%" PRIu32, i); |
| 335 | l_len = sprintf(l_val, "l%" PRIu32, i); |
| 336 | |
| 337 | if ((r = lyd_new_list_bin(data, NULL, "lst", 0, &list, &i, sizeof i, k2_val, k2_len))) { |
| 338 | return r; |
| 339 | } |
| 340 | if ((r = lyd_new_term_bin(list, NULL, "l", l_val, l_len, 0, NULL))) { |
| 341 | return r; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | TEST_END(ts_end); |
| 346 | |
| 347 | lyd_free_siblings(data); |
| 348 | |
| 349 | return LY_SUCCESS; |
| 350 | } |
| 351 | |
| 352 | static LY_ERR |
| 353 | test_create_path(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 354 | { |
| 355 | LY_ERR r; |
| 356 | struct lyd_node *data = NULL; |
| 357 | uint32_t i; |
| 358 | char path[64], l_val[32]; |
| 359 | |
| 360 | TEST_START(ts_start); |
| 361 | |
| 362 | if ((r = lyd_new_inner(NULL, state->mod, "cont", 0, &data))) { |
| 363 | return r; |
| 364 | } |
| 365 | |
| 366 | for (i = 0; i < state->count; ++i) { |
| 367 | sprintf(path, "/perf:cont/lst[k1='%" PRIu32 "'][k2='str%" PRIu32 "']/l", i, i); |
| 368 | sprintf(l_val, "l%" PRIu32, i); |
| 369 | |
| 370 | if ((r = lyd_new_path(data, NULL, path, l_val, 0, NULL))) { |
| 371 | return r; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | TEST_END(ts_end); |
| 376 | |
| 377 | lyd_free_siblings(data); |
| 378 | |
| 379 | return LY_SUCCESS; |
| 380 | } |
| 381 | |
| 382 | static LY_ERR |
Michal Vasko | c606f31 | 2021-06-30 14:08:54 +0200 | [diff] [blame] | 383 | test_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 384 | { |
| 385 | LY_ERR r; |
| 386 | |
| 387 | TEST_START(ts_start); |
| 388 | |
| 389 | if ((r = lyd_validate_all(&state->data1, NULL, LYD_VALIDATE_PRESENT, NULL))) { |
| 390 | return r; |
| 391 | } |
| 392 | |
| 393 | TEST_END(ts_end); |
| 394 | |
| 395 | return LY_SUCCESS; |
| 396 | } |
| 397 | |
| 398 | static LY_ERR |
Michal Vasko | 99544ca | 2021-06-29 10:45:19 +0200 | [diff] [blame] | 399 | _test_parse(struct test_state *state, LYD_FORMAT format, ly_bool use_file, uint32_t print_options, uint32_t parse_options, |
| 400 | uint32_t validate_options, struct timespec *ts_start, struct timespec *ts_end) |
| 401 | { |
| 402 | LY_ERR ret = LY_SUCCESS; |
| 403 | struct lyd_node *data = NULL; |
| 404 | char *buf = NULL; |
| 405 | struct ly_in *in = NULL; |
| 406 | |
| 407 | if (use_file) { |
| 408 | if ((ret = lyd_print_path(TEMP_FILE, state->data1, format, print_options))) { |
| 409 | goto cleanup; |
| 410 | } |
| 411 | if ((ret = ly_in_new_filepath(TEMP_FILE, 0, &in))) { |
| 412 | goto cleanup; |
| 413 | } |
| 414 | } else { |
| 415 | if ((ret = lyd_print_mem(&buf, state->data1, format, print_options))) { |
| 416 | goto cleanup; |
| 417 | } |
| 418 | if ((ret = ly_in_new_memory(buf, &in))) { |
| 419 | goto cleanup; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | TEST_START(ts_start); |
| 424 | |
| 425 | if ((ret = lyd_parse_data(state->mod->ctx, NULL, in, format, parse_options, validate_options, &data))) { |
| 426 | goto cleanup; |
| 427 | } |
| 428 | |
| 429 | TEST_END(ts_end); |
| 430 | |
| 431 | cleanup: |
| 432 | free(buf); |
| 433 | ly_in_free(in, 0); |
| 434 | lyd_free_siblings(data); |
| 435 | return ret; |
| 436 | } |
| 437 | |
| 438 | static LY_ERR |
| 439 | test_parse_xml_mem_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 440 | { |
| 441 | return _test_parse(state, LYD_XML, 0, LYD_PRINT_SHRINK, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, ts_start, ts_end); |
| 442 | } |
| 443 | |
| 444 | static LY_ERR |
| 445 | test_parse_xml_mem_no_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 446 | { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 447 | return _test_parse(state, LYD_XML, 0, LYD_PRINT_SHRINK, LYD_PARSE_STRICT | LYD_PARSE_ONLY | LYD_PARSE_ORDERED, 0, |
| 448 | ts_start, ts_end); |
Michal Vasko | 99544ca | 2021-06-29 10:45:19 +0200 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static LY_ERR |
| 452 | test_parse_xml_file_no_validate_format(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 453 | { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 454 | return _test_parse(state, LYD_XML, 1, 0, LYD_PARSE_STRICT | LYD_PARSE_ONLY | LYD_PARSE_ORDERED, 0, ts_start, ts_end); |
Michal Vasko | 99544ca | 2021-06-29 10:45:19 +0200 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | static LY_ERR |
| 458 | test_parse_json_mem_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 459 | { |
| 460 | return _test_parse(state, LYD_JSON, 0, LYD_PRINT_SHRINK, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, ts_start, ts_end); |
| 461 | } |
| 462 | |
| 463 | static LY_ERR |
| 464 | test_parse_json_mem_no_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 465 | { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 466 | return _test_parse(state, LYD_JSON, 0, LYD_PRINT_SHRINK, LYD_PARSE_STRICT | LYD_PARSE_ONLY | LYD_PARSE_ORDERED, 0, |
| 467 | ts_start, ts_end); |
Michal Vasko | 99544ca | 2021-06-29 10:45:19 +0200 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | static LY_ERR |
| 471 | test_parse_json_file_no_validate_format(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 472 | { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 473 | return _test_parse(state, LYD_JSON, 1, 0, LYD_PARSE_STRICT | LYD_PARSE_ONLY | LYD_PARSE_ORDERED, 0, ts_start, ts_end); |
Michal Vasko | 99544ca | 2021-06-29 10:45:19 +0200 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static LY_ERR |
| 477 | test_parse_lyb_mem_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 478 | { |
| 479 | return _test_parse(state, LYD_LYB, 0, LYD_PRINT_SHRINK, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, ts_start, ts_end); |
| 480 | } |
| 481 | |
| 482 | static LY_ERR |
| 483 | test_parse_lyb_mem_no_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 484 | { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 485 | return _test_parse(state, LYD_LYB, 0, LYD_PRINT_SHRINK, LYD_PARSE_STRICT | LYD_PARSE_ONLY | LYD_PARSE_ORDERED, 0, |
| 486 | ts_start, ts_end); |
Michal Vasko | 99544ca | 2021-06-29 10:45:19 +0200 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static LY_ERR |
| 490 | test_parse_lyb_file_no_validate(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 491 | { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 492 | return _test_parse(state, LYD_LYB, 1, 0, LYD_PARSE_STRICT | LYD_PARSE_ONLY | LYD_PARSE_ORDERED, 0, ts_start, ts_end); |
Michal Vasko | 99544ca | 2021-06-29 10:45:19 +0200 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | static LY_ERR |
| 496 | _test_print(struct test_state *state, LYD_FORMAT format, uint32_t print_options, struct timespec *ts_start, |
| 497 | struct timespec *ts_end) |
| 498 | { |
| 499 | LY_ERR ret = LY_SUCCESS; |
| 500 | char *buf = NULL; |
| 501 | |
| 502 | TEST_START(ts_start); |
| 503 | |
| 504 | if ((ret = lyd_print_mem(&buf, state->data1, format, print_options))) { |
| 505 | goto cleanup; |
| 506 | } |
| 507 | |
| 508 | TEST_END(ts_end); |
| 509 | |
| 510 | cleanup: |
| 511 | free(buf); |
| 512 | return ret; |
| 513 | } |
| 514 | |
| 515 | static LY_ERR |
| 516 | test_print_xml(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 517 | { |
| 518 | return _test_print(state, LYD_XML, LYD_PRINT_SHRINK, ts_start, ts_end); |
| 519 | } |
| 520 | |
| 521 | static LY_ERR |
| 522 | test_print_json(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 523 | { |
| 524 | return _test_print(state, LYD_JSON, LYD_PRINT_SHRINK, ts_start, ts_end); |
| 525 | } |
| 526 | |
| 527 | static LY_ERR |
| 528 | test_print_lyb(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 529 | { |
| 530 | return _test_print(state, LYD_LYB, LYD_PRINT_SHRINK, ts_start, ts_end); |
| 531 | } |
| 532 | |
| 533 | static LY_ERR |
| 534 | test_dup(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 535 | { |
| 536 | LY_ERR r; |
| 537 | struct lyd_node *data; |
| 538 | |
| 539 | TEST_START(ts_start); |
| 540 | |
| 541 | if ((r = lyd_dup_siblings(state->data1, NULL, LYD_DUP_RECURSIVE, &data))) { |
| 542 | return r; |
| 543 | } |
| 544 | |
| 545 | TEST_END(ts_end); |
| 546 | |
| 547 | lyd_free_siblings(data); |
| 548 | |
| 549 | return LY_SUCCESS; |
| 550 | } |
| 551 | |
| 552 | static LY_ERR |
| 553 | test_free(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 554 | { |
| 555 | LY_ERR r; |
| 556 | struct lyd_node *data; |
| 557 | |
| 558 | if ((r = create_list_inst(state->mod, 0, state->count, &data))) { |
| 559 | return r; |
| 560 | } |
| 561 | |
| 562 | TEST_START(ts_start); |
| 563 | |
| 564 | lyd_free_siblings(data); |
| 565 | |
| 566 | TEST_END(ts_end); |
| 567 | |
| 568 | return LY_SUCCESS; |
| 569 | } |
| 570 | |
| 571 | static LY_ERR |
| 572 | test_xpath_find(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 573 | { |
| 574 | LY_ERR r; |
| 575 | struct ly_set *set; |
| 576 | char path[64]; |
| 577 | |
| 578 | sprintf(path, "/perf:cont/lst[k1=%" PRIu32 " and k2='str%" PRIu32 "']", state->count / 2, state->count / 2); |
| 579 | |
| 580 | TEST_START(ts_start); |
| 581 | |
| 582 | if ((r = lyd_find_xpath(state->data1, path, &set))) { |
| 583 | return r; |
| 584 | } |
| 585 | |
| 586 | TEST_END(ts_end); |
| 587 | |
| 588 | ly_set_free(set, NULL); |
| 589 | |
| 590 | return LY_SUCCESS; |
| 591 | } |
| 592 | |
| 593 | static LY_ERR |
| 594 | test_xpath_find_hash(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 595 | { |
| 596 | LY_ERR r; |
| 597 | struct ly_set *set; |
| 598 | char path[64]; |
| 599 | |
| 600 | sprintf(path, "/perf:cont/lst[k1=%" PRIu32 "][k2='str%" PRIu32 "']", state->count / 2, state->count / 2); |
| 601 | |
| 602 | TEST_START(ts_start); |
| 603 | |
| 604 | if ((r = lyd_find_xpath(state->data1, path, &set))) { |
| 605 | return r; |
| 606 | } |
| 607 | |
| 608 | TEST_END(ts_end); |
| 609 | |
| 610 | ly_set_free(set, NULL); |
| 611 | |
| 612 | return LY_SUCCESS; |
| 613 | } |
| 614 | |
| 615 | static LY_ERR |
| 616 | test_compare_same(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 617 | { |
| 618 | LY_ERR r; |
| 619 | |
| 620 | TEST_START(ts_start); |
| 621 | |
| 622 | if ((r = lyd_compare_siblings(state->data1, state->data2, LYD_COMPARE_FULL_RECURSION))) { |
| 623 | return r; |
| 624 | } |
| 625 | |
| 626 | TEST_END(ts_end); |
| 627 | |
| 628 | return LY_SUCCESS; |
| 629 | } |
| 630 | |
| 631 | static LY_ERR |
| 632 | test_diff_same(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 633 | { |
| 634 | LY_ERR r; |
| 635 | struct lyd_node *diff; |
| 636 | |
| 637 | TEST_START(ts_start); |
| 638 | |
| 639 | if ((r = lyd_diff_siblings(state->data1, state->data2, 0, &diff))) { |
| 640 | return r; |
| 641 | } |
| 642 | |
| 643 | TEST_END(ts_end); |
| 644 | |
| 645 | lyd_free_siblings(diff); |
| 646 | |
| 647 | return LY_SUCCESS; |
| 648 | } |
| 649 | |
| 650 | static LY_ERR |
| 651 | test_diff_no_same(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 652 | { |
| 653 | LY_ERR r; |
| 654 | struct lyd_node *diff; |
| 655 | |
| 656 | TEST_START(ts_start); |
| 657 | |
| 658 | if ((r = lyd_diff_siblings(state->data1, state->data2, 0, &diff))) { |
| 659 | return r; |
| 660 | } |
| 661 | |
| 662 | TEST_END(ts_end); |
| 663 | |
| 664 | lyd_free_siblings(diff); |
| 665 | |
| 666 | return LY_SUCCESS; |
| 667 | } |
| 668 | |
| 669 | static LY_ERR |
| 670 | test_merge_same(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 671 | { |
| 672 | LY_ERR r; |
| 673 | |
| 674 | TEST_START(ts_start); |
| 675 | |
| 676 | if ((r = lyd_merge_siblings(&state->data1, state->data2, 0))) { |
| 677 | return r; |
| 678 | } |
| 679 | |
| 680 | TEST_END(ts_end); |
| 681 | |
| 682 | return LY_SUCCESS; |
| 683 | } |
| 684 | |
| 685 | static LY_ERR |
| 686 | test_merge_no_same(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 687 | { |
| 688 | LY_ERR r; |
| 689 | struct lyd_node *data1; |
| 690 | |
| 691 | if ((r = create_list_inst(state->mod, 0, state->count, &data1))) { |
| 692 | return r; |
| 693 | } |
| 694 | |
| 695 | TEST_START(ts_start); |
| 696 | |
| 697 | if ((r = lyd_merge_siblings(&data1, state->data2, 0))) { |
| 698 | return r; |
| 699 | } |
| 700 | |
| 701 | TEST_END(ts_end); |
| 702 | |
| 703 | lyd_free_siblings(data1); |
| 704 | |
| 705 | return LY_SUCCESS; |
| 706 | } |
| 707 | |
| 708 | static LY_ERR |
| 709 | test_merge_no_same_destruct(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end) |
| 710 | { |
| 711 | LY_ERR r; |
| 712 | struct lyd_node *data1, *data2; |
| 713 | |
| 714 | if ((r = create_list_inst(state->mod, 0, state->count, &data1))) { |
| 715 | return r; |
| 716 | } |
| 717 | if ((r = create_list_inst(state->mod, state->count, state->count, &data2))) { |
| 718 | return r; |
| 719 | } |
| 720 | |
| 721 | TEST_START(ts_start); |
| 722 | |
| 723 | if ((r = lyd_merge_siblings(&data1, data2, LYD_MERGE_DESTRUCT))) { |
| 724 | return r; |
| 725 | } |
| 726 | |
| 727 | TEST_END(ts_end); |
| 728 | |
| 729 | lyd_free_siblings(data1); |
| 730 | |
| 731 | return LY_SUCCESS; |
| 732 | } |
| 733 | |
| 734 | struct test tests[] = { |
Michal Vasko | 3770509 | 2021-07-01 13:32:46 +0200 | [diff] [blame] | 735 | {"create new text", setup_basic, test_create_new_text}, |
| 736 | {"create new bin", setup_basic, test_create_new_bin}, |
| 737 | {"create path", setup_basic, test_create_path}, |
| 738 | {"validate", setup_data_single_tree, test_validate}, |
| 739 | {"parse xml mem validate", setup_data_single_tree, test_parse_xml_mem_validate}, |
| 740 | {"parse xml mem no validate", setup_data_single_tree, test_parse_xml_mem_no_validate}, |
| 741 | {"parse xml file no validate format", setup_data_single_tree, test_parse_xml_file_no_validate_format}, |
| 742 | {"parse json mem validate", setup_data_single_tree, test_parse_json_mem_validate}, |
| 743 | {"parse json mem no validate", setup_data_single_tree, test_parse_json_mem_no_validate}, |
| 744 | {"parse json file no validate format", setup_data_single_tree, test_parse_json_file_no_validate_format}, |
| 745 | {"parse lyb mem validate", setup_data_single_tree, test_parse_lyb_mem_validate}, |
| 746 | {"parse lyb mem no validate", setup_data_single_tree, test_parse_lyb_mem_no_validate}, |
| 747 | {"parse lyb file no validate", setup_data_single_tree, test_parse_lyb_file_no_validate}, |
| 748 | {"print xml", setup_data_single_tree, test_print_xml}, |
| 749 | {"print json", setup_data_single_tree, test_print_json}, |
| 750 | {"print lyb", setup_data_single_tree, test_print_lyb}, |
| 751 | {"dup", setup_data_single_tree, test_dup}, |
| 752 | {"free", setup_basic, test_free}, |
| 753 | {"xpath find", setup_data_single_tree, test_xpath_find}, |
| 754 | {"xpath find hash", setup_data_single_tree, test_xpath_find_hash}, |
| 755 | {"compare same", setup_data_same_trees, test_compare_same}, |
| 756 | {"diff same", setup_data_same_trees, test_diff_same}, |
| 757 | {"diff no same", setup_data_no_same_trees, test_diff_no_same}, |
| 758 | {"merge same", setup_data_same_trees, test_merge_same}, |
| 759 | {"merge no same", setup_data_offset_tree, test_merge_no_same}, |
| 760 | {"merge no same destruct", setup_basic, test_merge_no_same_destruct}, |
Michal Vasko | 99544ca | 2021-06-29 10:45:19 +0200 | [diff] [blame] | 761 | }; |
| 762 | |
| 763 | int |
| 764 | main(int argc, char **argv) |
| 765 | { |
| 766 | LY_ERR ret = LY_SUCCESS; |
| 767 | struct ly_ctx *ctx = NULL; |
| 768 | const struct lys_module *mod; |
| 769 | uint32_t i, count, tries; |
| 770 | |
| 771 | if (argc < 3) { |
| 772 | fprintf(stderr, "Usage:\n%s list-instance-count test-tries\n\n", argv[0]); |
| 773 | return LY_EINVAL; |
| 774 | } |
| 775 | |
| 776 | count = atoi(argv[1]); |
| 777 | if (!count) { |
| 778 | fprintf(stderr, "Invalid count \"%s\".\n", argv[1]); |
| 779 | return LY_EINVAL; |
| 780 | } |
| 781 | |
| 782 | tries = atoi(argv[2]); |
| 783 | if (!tries) { |
| 784 | fprintf(stderr, "Invalid tries \"%s\".\n", argv[2]); |
| 785 | return LY_EINVAL; |
| 786 | } |
| 787 | |
| 788 | printf("\nly_perf:\n\tdata set size: %" PRIu32 "\n\teach test executed: %" PRIu32 " %s\n\n", count, tries, |
| 789 | (tries > 1) ? "times" : "time"); |
| 790 | |
| 791 | /* create context */ |
| 792 | if ((ret = ly_ctx_new(TESTS_SRC "/perf", 0, &ctx))) { |
| 793 | goto cleanup; |
| 794 | } |
| 795 | |
| 796 | /* load modules */ |
| 797 | if (!(mod = ly_ctx_load_module(ctx, "perf", NULL, NULL))) { |
| 798 | ret = LY_ENOTFOUND; |
| 799 | goto cleanup; |
| 800 | } |
| 801 | |
| 802 | /* tests */ |
| 803 | for (i = 0; i < (sizeof tests / sizeof(struct test)); ++i) { |
| 804 | if ((ret = exec_test(tests[i].setup, tests[i].test, tests[i].name, mod, count, tries))) { |
| 805 | goto cleanup; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | printf("\n"); |
| 810 | |
| 811 | cleanup: |
| 812 | ly_ctx_destroy(ctx); |
| 813 | return ret; |
| 814 | } |