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 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
|
/**
* @file perf.c
* @author Michal Vasko <mvasko@cesnet.cz>
* @author Ondrej Kusnirik <Ondrej.Kusnirik@cesnet.cz>
* @brief performance tests
*
* Copyright (c) 2021 - 2024 Deutsche Telekom AG.
* Copyright (c) 2021 - 2024 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 <stdint.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <libyang/libyang.h>
#include "common.h"
#include "config.h"
#include "plugins_datastore.h"
#include "sysrepo.h"
#include "tests/tcommon.h"
#ifdef SR_HAVE_CALLGRIND
# include <valgrind/callgrind.h>
#endif
#define TABLE_WIDTH 67
#define NAME_FIXED_LEN 33
#define COL_FIXED_LEN 15
#define MILLION 1000000
#define ABS(x) (x < 0) * (-x) + (x >= 0) * x
/**
* @brief Test state structure.
*/
struct test_state {
sr_conn_ctx_t *conn;
sr_session_ctx_t *sess;
sr_subscription_ctx_t *sub;
const struct lys_module *mod;
uint32_t count;
};
typedef int (*setup_cb)(struct test_state *state);
typedef int (*test_cb)(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end);
typedef void (*teardown_cb)(struct test_state *state);
/**
* @brief Single test structure.
*/
struct test {
const char *name;
setup_cb setup;
test_cb test;
teardown_cb teardown;
};
/**
* @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;
assert(ts1->tv_sec <= ts2->tv_sec);
/* seconds diff */
usec_diff += (ts2->tv_sec - ts1->tv_sec) * 1000000;
/* nanoseconds diff */
usec_diff += (ts2->tv_nsec - ts1->tv_nsec) / 1000;
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 SR ERR value.
*/
static int
create_list_inst(const struct lys_module *mod, uint32_t offset, uint32_t count, struct lyd_node **data)
{
uint32_t i;
char k1_val[32], k2_val[32], l_val[32];
struct lyd_node *list;
if (lyd_new_inner(NULL, mod, "cont", 0, data)) {
return SR_ERR_LY;
}
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 (lyd_new_list(*data, NULL, "lst", 0, &list, k1_val, k2_val)) {
return SR_ERR_LY;
}
if (lyd_new_term(list, NULL, "l", l_val, 0, NULL)) {
return SR_ERR_LY;
}
}
return SR_ERR_OK;
}
/**
* @brief Create data tree with user ordered 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 SR ERR value.
*/
static int
create_user_order_list_inst(const struct lys_module *mod, uint32_t offset, uint32_t count, struct lyd_node **data)
{
uint32_t i;
char k_val[32], l_val[32];
struct lyd_node *list;
if (lyd_new_inner(NULL, mod, "cont", 0, data)) {
return SR_ERR_LY;
}
for (i = 0; i < count; ++i) {
sprintf(k_val, "%" PRIu32, i + offset);
sprintf(l_val, "l%" PRIu32, i + offset);
if (lyd_new_list(*data, NULL, "usr-lst", 0, &list, k_val)) {
return SR_ERR_LY;
}
if (lyd_new_term(list, NULL, "l", l_val, 0, NULL)) {
return SR_ERR_LY;
}
}
return SR_ERR_OK;
}
/**
* @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] 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 SR ERR value.
*/
static int
exec_test(setup_cb setup, test_cb test, teardown_cb teardown, uint32_t tries, int64_t *time, struct test_state *state)
{
int ret;
struct timespec ts_start, ts_end;
uint32_t i;
uint64_t time_usec = 0;
/* setup */
if ((ret = setup(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;
/* save time for later printing */
*time = (int64_t)time_usec;
/* teardown */
teardown(state);
return SR_ERR_OK;
}
static void
TEST_START(struct timespec *ts)
{
time_get(ts);
#ifdef SR_HAVE_CALLGRIND
CALLGRIND_START_INSTRUMENTATION;
#endif
}
static void
TEST_END(struct timespec *ts)
{
time_get(ts);
#ifdef SR_HAVE_CALLGRIND
CALLGRIND_STOP_INSTRUMENTATION;
#endif
}
/* TEST SYSREPO CB */
static int
oper_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *module_name, const char *path,
const char *request_xpath, uint32_t request_id, struct lyd_node **parent, void *private_data)
{
struct test_state *state = private_data;
(void)session;
(void)sub_id;
(void)module_name;
(void)path;
(void)request_xpath;
(void)request_id;
return create_list_inst(state->mod, 0, state->count, parent);
}
/* TEST SETUPS */
static int
setup_empty(struct test_state *state)
{
int r;
if ((r = sr_connect(SR_CONN_DEFAULT, &state->conn))) {
return r;
}
if ((r = sr_session_start(state->conn, SR_DS_RUNNING, &(state->sess)))) {
return r;
}
state->mod = ly_ctx_get_module_implemented(sr_acquire_context(state->conn), "perf");
return SR_ERR_OK;
}
static int
setup_empty_oper(struct test_state *state)
{
int r;
if ((r = sr_connect(SR_CONN_DEFAULT, &state->conn))) {
return r;
}
if ((r = sr_session_start(state->conn, SR_DS_OPERATIONAL, &(state->sess)))) {
return r;
}
state->mod = ly_ctx_get_module_implemented(sr_acquire_context(state->conn), "perf");
return SR_ERR_OK;
}
static int
setup_running(struct test_state *state)
{
int r;
struct lyd_node *data;
if ((r = sr_connect(SR_CONN_DEFAULT, &state->conn))) {
return r;
}
if ((r = sr_session_start(state->conn, SR_DS_RUNNING, &(state->sess)))) {
return r;
}
state->mod = ly_ctx_get_module_implemented(sr_acquire_context(state->conn), "perf");
/* set running data */
if ((r = create_list_inst(state->mod, 0, state->count, &data))) {
return r;
}
if ((r = sr_edit_batch(state->sess, data, "merge"))) {
return r;
}
if ((r = sr_apply_changes(state->sess, state->count * 100))) {
return r;
}
lyd_free_siblings(data);
return SR_ERR_OK;
}
static int
setup_running_cached(struct test_state *state)
{
int r;
struct lyd_node *data;
sr_cache_running(1);
if ((r = sr_connect(0, &state->conn))) {
return r;
}
if ((r = sr_session_start(state->conn, SR_DS_RUNNING, &state->sess))) {
return r;
}
state->mod = ly_ctx_get_module_implemented(sr_acquire_context(state->conn), "perf");
/* set running data */
if ((r = create_list_inst(state->mod, 0, state->count, &data))) {
return r;
}
if ((r = sr_edit_batch(state->sess, data, "merge"))) {
return r;
}
if ((r = sr_apply_changes(state->sess, state->count * 100))) {
return r;
}
lyd_free_siblings(data);
return SR_ERR_OK;
}
static int
setup_oper(struct test_state *state)
{
int r;
struct lyd_node *data;
if ((r = sr_connect(SR_CONN_DEFAULT, &state->conn))) {
return r;
}
if ((r = sr_session_start(state->conn, SR_DS_OPERATIONAL, &(state->sess)))) {
return r;
}
state->mod = ly_ctx_get_module_implemented(sr_acquire_context(state->conn), "perf");
/* set operational data */
if ((r = create_list_inst(state->mod, 0, state->count, &data))) {
return r;
}
if ((r = sr_edit_batch(state->sess, data, "merge"))) {
return r;
}
if ((r = sr_apply_changes(state->sess, state->count * 100))) {
return r;
}
lyd_free_siblings(data);
return SR_ERR_OK;
}
static int
setup_userordered_running(struct test_state *state)
{
int r;
struct lyd_node *data;
if ((r = sr_connect(SR_CONN_DEFAULT, &state->conn))) {
return r;
}
if ((r = sr_session_start(state->conn, SR_DS_RUNNING, &(state->sess)))) {
return r;
}
state->mod = ly_ctx_get_module_implemented(sr_acquire_context(state->conn), "perf");
/* set running data */
if ((r = create_user_order_list_inst(state->mod, 0, state->count, &data))) {
return r;
}
if ((r = sr_edit_batch(state->sess, data, "merge"))) {
return r;
}
if ((r = sr_apply_changes(state->sess, state->count * 100))) {
return r;
}
lyd_free_siblings(data);
return SR_ERR_OK;
}
static int
setup_subscribe_oper(struct test_state *state)
{
int r;
if ((r = sr_connect(SR_CONN_DEFAULT, &state->conn))) {
return r;
}
if ((r = sr_session_start(state->conn, SR_DS_OPERATIONAL, &state->sess))) {
return r;
}
if ((r = sr_oper_get_subscribe(state->sess, "perf", "/perf:cont", oper_cb, state, 0, &state->sub))) {
return r;
}
state->mod = ly_ctx_get_module_implemented(sr_acquire_context(state->conn), "perf");
return SR_ERR_OK;
}
static void
teardown_empty(struct test_state *state)
{
sr_session_stop(state->sess);
sr_release_context(state->conn);
sr_disconnect(state->conn);
}
static void
teardown_running(struct test_state *state)
{
sr_delete_item(state->sess, "/perf:cont", 0);
sr_apply_changes(state->sess, state->count * 100);
sr_session_stop(state->sess);
sr_release_context(state->conn);
sr_disconnect(state->conn);
}
static void
teardown_oper(struct test_state *state)
{
sr_delete_item(state->sess, "/perf:cont", 0);
sr_apply_changes(state->sess, state->count * 100);
sr_session_stop(state->sess);
sr_release_context(state->conn);
sr_disconnect(state->conn);
}
static void
teardown_subscribe_oper(struct test_state *state)
{
sr_session_switch_ds(state->sess, SR_DS_RUNNING);
sr_unsubscribe(state->sub);
state->sub = NULL;
sr_session_stop(state->sess);
sr_release_context(state->conn);
sr_disconnect(state->conn);
}
static int
test_get_tree(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
int r;
sr_data_t *data;
char path[64];
sprintf(path, "/perf:cont/lst[k1='%" PRIu32 "' and k2='str%" PRIu32 "']/l", state->count / 2, state->count / 2);
TEST_START(ts_start);
if ((r = sr_get_data(state->sess, path, 0, 0, 0, &data))) {
return r;
}
TEST_END(ts_end);
sr_release_data(data);
return SR_ERR_OK;
}
static int
test_get_item(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
int r;
sr_val_t *val;
char path[64];
sprintf(path, "/perf:cont/lst[k1='%" PRIu32 "' and k2='str%" PRIu32 "']/l", state->count / 2, state->count / 2);
TEST_START(ts_start);
if ((r = sr_get_item(state->sess, path, 0, &val))) {
return r;
}
TEST_END(ts_end);
sr_free_val(val);
return SR_ERR_OK;
}
static int
test_get_tree_hash(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
int r;
sr_data_t *data;
char path[64];
sprintf(path, "/perf:cont/lst[k1='%" PRIu32 "'][k2='str%" PRIu32 "']/l", state->count / 2, state->count / 2);
TEST_START(ts_start);
if ((r = sr_get_data(state->sess, path, 0, 0, 0, &data))) {
return r;
}
TEST_END(ts_end);
sr_release_data(data);
return SR_ERR_OK;
}
static int
test_get_user_order_tree(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
int r;
sr_data_t *data;
TEST_START(ts_start);
if ((r = sr_get_data(state->sess, "/perf:cont", 0, 0, 0, &data))) {
return r;
}
TEST_END(ts_end);
sr_release_data(data);
return SR_ERR_OK;
}
static int
test_get_oper_tree(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
int r;
sr_data_t *data;
TEST_START(ts_start);
if ((r = sr_get_data(state->sess, "/perf:cont", 0, 0, 0, &data))) {
return r;
}
TEST_END(ts_end);
sr_release_data(data);
return SR_ERR_OK;
}
static int
test_batch_create(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
int r;
struct lyd_node *data;
TEST_START(ts_start);
if ((r = create_list_inst(state->mod, 0, state->count, &data))) {
return r;
}
if ((r = sr_edit_batch(state->sess, data, "merge"))) {
return r;
}
if ((r = sr_apply_changes(state->sess, state->count * 100))) {
return r;
}
TEST_END(ts_end);
lyd_free_siblings(data);
if ((r = sr_delete_item(state->sess, "/perf:cont/lst", 0))) {
return r;
}
if ((r = sr_apply_changes(state->sess, state->count * 100))) {
return r;
}
return SR_ERR_OK;
}
static int
test_user_order_items_create(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
int r;
uint32_t i;
char path[64], l_val[32];
TEST_START(ts_start);
for (i = 0; i < state->count; ++i) {
sprintf(path, "/perf:cont/usr-lst[k='%" PRIu32 "']/l", i);
sprintf(l_val, "l%" PRIu32, i);
if ((r = sr_set_item_str(state->sess, path, l_val, NULL, 0))) {
return r;
}
}
if ((r = sr_apply_changes(state->sess, state->count * 100))) {
return r;
}
TEST_END(ts_end);
if ((r = sr_delete_item(state->sess, "/perf:cont/usr-lst", 0))) {
return r;
}
if ((r = sr_apply_changes(state->sess, state->count * 100))) {
return r;
}
return SR_ERR_OK;
}
static int
test_items_create(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
int r;
uint32_t i;
char path[64], l_val[32];
TEST_START(ts_start);
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 = sr_set_item_str(state->sess, path, l_val, NULL, 0))) {
return r;
}
}
if ((r = sr_apply_changes(state->sess, state->count * 100))) {
return r;
}
TEST_END(ts_end);
if ((r = sr_delete_item(state->sess, "/perf:cont/lst", 0))) {
return r;
}
if ((r = sr_apply_changes(state->sess, state->count * 100))) {
return r;
}
return SR_ERR_OK;
}
static int
test_items_create_oper(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
int r;
uint32_t i;
char path[64], l_val[32];
TEST_START(ts_start);
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 = sr_set_item_str(state->sess, path, l_val, NULL, 0))) {
return r;
}
}
if ((r = sr_apply_changes(state->sess, state->count * 100))) {
return r;
}
TEST_END(ts_end);
if ((r = sr_delete_item(state->sess, "/perf:cont/lst", 0))) {
return r;
}
if ((r = sr_apply_changes(state->sess, state->count * 100))) {
return r;
}
return SR_ERR_OK;
}
static int
dummy_change_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *module_name, const char *xpath, sr_event_t event,
uint32_t request_id, void *private_data)
{
(void)session;
(void)sub_id;
(void)module_name;
(void)xpath;
(void)event;
(void)request_id;
(void)private_data;
return SR_ERR_OK;
}
static int
test_many_oper_change_subs(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
int r;
uint32_t i, j;
char path[64], l_val[32];
const uint32_t SCALE = 12;
sr_conn_ctx_t *conn[12] = {NULL};
sr_session_ctx_t *sess[12] = {NULL};
sr_subscription_ctx_t *subscr[12] = {NULL};
/* create many change_subs */
for (i = 0; i < SCALE; ++i) {
/* create connection */
if ((r = sr_connect(0, &conn[i]))) {
return r;
}
if ((r = sr_session_start(conn[i], SR_DS_OPERATIONAL, &sess[i]))) {
return r;
}
if ((r = sr_module_change_subscribe(sess[i], "perf", NULL, dummy_change_cb, NULL, 0, 0, &subscr[i]))) {
return r;
}
}
TEST_START(ts_start);
for (j = 0; j < 10; j++) {
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 = sr_set_item_str(state->sess, path, l_val, NULL, 0))) {
return r;
}
}
if ((r = sr_apply_changes(state->sess, state->count * 100))) {
return r;
}
if ((r = sr_delete_item(state->sess, "/perf:cont/lst", 0))) {
return r;
}
if ((r = sr_apply_changes(state->sess, state->count * 100))) {
return r;
}
}
TEST_END(ts_end);
for (i = 0; i < SCALE; ++i) {
sr_unsubscribe(subscr[i]);
sr_disconnect(conn[i]);
}
return SR_ERR_OK;
}
static int
test_items_remove(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
int r;
TEST_START(ts_start);
if ((r = sr_delete_item(state->sess, "/perf:cont/lst", 0))) {
return r;
}
if ((r = sr_apply_changes(state->sess, state->count * 100))) {
return r;
}
TEST_END(ts_end);
return SR_ERR_OK;
}
static int
test_items_remove_subtree(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
int r;
TEST_START(ts_start);
if ((r = sr_delete_item(state->sess, "/perf:cont", 0))) {
return r;
}
if ((r = sr_apply_changes(state->sess, state->count * 100))) {
return r;
}
TEST_END(ts_end);
return SR_ERR_OK;
}
static int
test_item_create(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
int r;
char path[64], l_val[32];
sprintf(path, "/perf:cont/lst[k1='%" PRIu32 "'][k2='str%" PRIu32 "']/l", state->count / 2, state->count / 2);
if ((r = sr_delete_item(state->sess, path, 0))) {
return r;
}
if ((r = sr_apply_changes(state->sess, 0))) {
return r;
}
TEST_START(ts_start);
sprintf(l_val, "l%" PRIu32, 0);
if ((r = sr_set_item_str(state->sess, path, l_val, NULL, SR_EDIT_STRICT))) {
return r;
}
if ((r = sr_apply_changes(state->sess, 0))) {
return r;
}
TEST_END(ts_end);
return SR_ERR_OK;
}
static int
test_item_create_oper(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
int r;
char path[64], l_val[32];
sprintf(path, "/perf:cont/lst[k1='%" PRIu32 "'][k2='str%" PRIu32 "']/l", state->count / 2, state->count / 2);
if ((r = sr_delete_item(state->sess, path, 0))) {
return r;
}
if ((r = sr_apply_changes(state->sess, 0))) {
return r;
}
TEST_START(ts_start);
sprintf(l_val, "l%" PRIu32, 0);
if ((r = sr_set_item_str(state->sess, path, l_val, NULL, 0))) {
return r;
}
if ((r = sr_apply_changes(state->sess, 0))) {
return r;
}
TEST_END(ts_end);
return SR_ERR_OK;
}
static int
test_item_modify(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
int r;
char path[64], l_val[32];
TEST_START(ts_start);
sprintf(path, "/perf:cont/lst[k1='%" PRIu32 "'][k2='str%" PRIu32 "']/l", state->count / 2, state->count / 2);
sprintf(l_val, "l%" PRIu32, 1);
if ((r = sr_set_item_str(state->sess, path, l_val, NULL, 0))) {
return r;
}
if ((r = sr_apply_changes(state->sess, 0))) {
return r;
}
TEST_END(ts_end);
return SR_ERR_OK;
}
static int
test_item_remove(struct test_state *state, struct timespec *ts_start, struct timespec *ts_end)
{
int r;
char path[64], l_val[32];
TEST_START(ts_start);
sprintf(path, "/perf:cont/lst[k1='%" PRIu32 "'][k2='str%" PRIu32 "']/l", state->count / 2, state->count / 2);
sprintf(l_val, "l%" PRIu32, 0);
if ((r = sr_delete_item(state->sess, path, 0))) {
return r;
}
if ((r = sr_apply_changes(state->sess, 0))) {
return r;
}
TEST_END(ts_end);
return SR_ERR_OK;
}
static int
sysrepo_init(const char *plg_name, struct test_state *state, uint32_t count)
{
int ret, i;
sr_conn_ctx_t *conn;
sr_module_ds_t mod_ds;
for (i = 0; i < 5; ++i) {
mod_ds.plugin_name[i] = plg_name;
}
mod_ds.plugin_name[5] = "JSON notif";
/* turn on logging */
sr_log_stderr(SR_LL_WRN);
/* create connection */
if ((ret = sr_connect(SR_CONN_DEFAULT, &conn))) {
return ret;
}
/* disable logging */
sr_log_stderr(SR_LL_NONE);
/* remove module if it was installed previously */
ret = sr_remove_module(conn, "perf", 1);
if (ret && (ret != SR_ERR_NOT_FOUND)) {
return ret;
}
/* install module */
ret = sr_install_module2(conn, TESTS_SRC_DIR "/files/perf.yang", NULL, NULL, &mod_ds, NULL, NULL, 0, NULL, NULL, LYD_XML);
if (ret) {
return ret;
}
/* turn on logging */
sr_log_stderr(SR_LL_WRN);
/* disconnect */
sr_disconnect(conn);
state->count = count;
return SR_ERR_OK;
}
static int
sysrepo_destroy(void)
{
int ret;
sr_conn_ctx_t *conn;
/* create connection */
if ((ret = sr_connect(0, &conn))) {
return ret;
}
/* remove module */
ret = sr_remove_module(conn, "perf", 0);
/* disconnect */
sr_disconnect(conn);
if (ret) {
return ret;
}
return SR_ERR_OK;
}
struct test tests[] = {
{"get tree", setup_running, test_get_tree, teardown_running},
{"get item", setup_running, test_get_item, teardown_running},
{"get tree hash", setup_running, test_get_tree_hash, teardown_running},
{"get user ordered tree", setup_userordered_running, test_get_user_order_tree, teardown_running},
{"get oper tree", setup_subscribe_oper, test_get_oper_tree, teardown_subscribe_oper},
{"create batch", setup_empty, test_batch_create, teardown_empty},
{"create user ordered items", setup_empty, test_user_order_items_create, teardown_empty},
{"create all items", setup_empty, test_items_create, teardown_empty},
{"create all items oper", setup_empty_oper, test_items_create_oper, teardown_empty},
{"many oper change_subs", setup_empty_oper, test_many_oper_change_subs, teardown_empty},
{"remove all items", setup_running, test_items_remove, teardown_empty},
{"remove whole subtree", setup_running, test_items_remove_subtree, teardown_empty},
{"create an item", setup_running, test_item_create, teardown_running},
{"create an item oper", setup_oper, test_item_create_oper, teardown_oper},
{"modify an item", setup_running, test_item_modify, teardown_running},
{"remove an item", setup_running, test_item_remove, teardown_running},
{"get tree hash cached", setup_running_cached, test_get_tree_hash, teardown_running},
{"remove all items cached", setup_running_cached, test_items_remove, teardown_empty},
{"remove whole subtree cached", setup_running_cached, test_items_remove_subtree, teardown_empty},
{"create an item cached", setup_running_cached, test_item_create, teardown_running},
{"modify an item cached", setup_running_cached, test_item_modify, teardown_running},
{"remove an item cached", setup_running_cached, test_item_remove, teardown_running},
};
void
print_top_table_boundary(const char *plugin_name)
{
uint32_t i;
const char *first = " test name ", *second = " time ";
const char *third = " comparison ";
printf("\n %s\n", plugin_name);
printf(" ");
for (i = 0; i < TABLE_WIDTH; ++i) {
printf("_");
}
printf(" \n|");
for (i = 0; i < TABLE_WIDTH; ++i) {
printf(" ");
}
printf("|\n|");
printf("%s", first);
for (i = strlen(first); i <= NAME_FIXED_LEN + 2; ++i) {
printf(" ");
}
printf("|");
for (i = strlen(second); i < COL_FIXED_LEN - 1; ++i) {
printf(" ");
}
printf("%s", second);
printf("|");
for (i = strlen(third); i < COL_FIXED_LEN; ++i) {
printf(" ");
}
printf("%s", third);
printf("|\n");
// top table boundary
printf("|");
for (i = 0; i < TABLE_WIDTH; ++i) {
printf("_");
}
printf("|\n");
printf("|");
for (i = 0; i < TABLE_WIDTH; ++i) {
printf(" ");
}
printf("|\n");
}
void
print_test_results(const char *test_name, int64_t time, int64_t dflt_time)
{
uint32_t printed;
long double ratio = 1.0f;
uint64_t ratio_decimal;
printf("| %s ", test_name);
printed = strlen(test_name);
while (printed < NAME_FIXED_LEN) {
printf(".");
printed += 1;
}
printf(" | %3" PRId64 ".%06" PRId64 " s |", time / MILLION, time % MILLION);
printf(" ");
if (time < dflt_time) {
printf("\033[0;32;1m");
ratio = ABS((long double)(dflt_time) / (long double)(time));
} else if (time > dflt_time) {
printf("\033[0;31;1m");
ratio = ABS((long double)(time) / (long double)(dflt_time));
}
ratio_decimal = (uint64_t)((ratio - (uint64_t)(ratio)) * 1000);
printf("%7" PRIu64 ".%03" PRIu64 " x ", (uint64_t)(ratio), ratio_decimal);
printf("\033[0;37;1m");
printf("|\n");
}
static void
print_bottom_table_boundary(void)
{
uint32_t i;
printf("|");
for (i = 0; i < TABLE_WIDTH; ++i) {
printf("_");
}
printf("|\n\n");
}
int
main(int argc, char **argv)
{
int ret = 0;
uint32_t i, j, count, tries, plg_cnt, test_cnt;
const char *plg_name;
int64_t *times = NULL, time;
struct test_state state = {0};
/* handle arguments */
if (argc < 3) {
fprintf(stderr, "Usage:\n%s list-instance-count test-tries\n\n", argv[0]);
return SR_ERR_INVAL_ARG;
}
count = atoi(argv[1]);
if (count <= 0) {
fprintf(stderr, "Invalid count \"%s\".\n", argv[1]);
return SR_ERR_INVAL_ARG;
}
tries = atoi(argv[2]);
if (tries <= 0) {
fprintf(stderr, "Invalid tries \"%s\".\n", argv[2]);
return SR_ERR_INVAL_ARG;
}
/* establish the number of plugins and tests */
plg_cnt = sr_ds_plugin_int_count();
test_cnt = (sizeof tests / sizeof(struct test));
/* allocate a time var for every test of the default plugin */
times = calloc(test_cnt, sizeof *times);
if (!times) {
fprintf(stderr, "Out of memory.\n");
return SR_ERR_NO_MEMORY;
}
/* change print color */
printf("\033[0;37;1m");
printf("\n| Options\n\n Data set size : %" PRIu32 "\n Each test executed : %" PRIu32 " %s\n\n", count, tries,
(tries > 1) ? "times" : "time");
printf("\n| Performance tests\n");
/* for every plugin run a set of tests */
for (i = 0; i < plg_cnt; ++i) {
/* plugin name */
plg_name = sr_internal_ds_plugins[i]->name;
print_top_table_boundary(plg_name);
/* init */
if ((ret = sysrepo_init(plg_name, &state, count))) {
goto cleanup;
}
/* tests */
for (j = 0; j < test_cnt; ++j) {
if ((ret = exec_test(tests[j].setup, tests[j].test, tests[j].teardown, tries, &time, &state))) {
/* one of the tests failed */
goto cleanup;
}
/* store defaults plugin times to calculate the differences */
if (i == 0) {
times[j] = time;
}
print_test_results(tests[j].name, time, times[j]);
}
/* destroy */
if ((ret = sysrepo_destroy())) {
goto cleanup;
}
print_bottom_table_boundary();
}
printf("\nAll comparisons refer to how many times faster (green) or slower (red) the current plugin is compared to the first plugin.\n\n");
cleanup:
/* change print color back */
printf(" \033[0;37m");
free(times);
return ret;
}
|