test_prd_utils_kv.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (C) 2023 Alibaba Group Holding Limited
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <getopt.h>
  8. #include <sys/time.h>
  9. #include <prd_utils_kv.h>
  10. #include <prd_utils_internal.h>
  11. #define KV_OP_MAX_DURATION 100 // unit: ms
  12. static char test_key[] = "test_key";
  13. static char test_val[] = "test value with spaces";
  14. static char max_length_key[PRD_UTILS_KEY_MAX_LENGTH] =
  15. "12345678901234567890123456789012345678901234567890" \
  16. "1234567890123";
  17. static char max_length_val[PRD_UTILS_VAL_MAX_LENGTH] =
  18. "12345678901234567890123456789012345678901234567890" \
  19. "12345678901234567890123456789012345678901234567890" \
  20. "12345678901234567890123456789012345678901234567890" \
  21. "12345678901234567890123456789012345678901234567890" \
  22. "12345678901234567890123456789012345678901234567890" \
  23. "12345";
  24. typedef struct test_performance_records
  25. {
  26. // unit: us
  27. int normal_set;
  28. int normal_get;
  29. int normal_clean;
  30. int max_len_set;
  31. int max_len_get;
  32. int max_len_clean;
  33. // tmp values
  34. struct timeval tpstart;
  35. struct timeval tpend;
  36. } test_performance_records_t;
  37. static test_performance_records_t perf_records;
  38. #define GET_TIME_BEGIN() do { gettimeofday(&perf_records.tpstart, NULL); } while(0)
  39. #define GET_TIME_RECORD(target) do { gettimeofday(&perf_records.tpend, NULL); \
  40. target = 1000000 * (perf_records.tpend.tv_sec - perf_records.tpstart.tv_sec) \
  41. + perf_records.tpend.tv_usec - perf_records.tpstart.tv_usec; \
  42. } while (0)
  43. static int test_prd_utils_set_kv()
  44. {
  45. printf("Test: prd_utils_set_kv() start\n");
  46. GET_TIME_BEGIN();
  47. PRD_UTILS_ASSERT(prd_utils_set_kv(test_key, test_val) == PRD_UTILS_RESULT_OK);
  48. GET_TIME_RECORD(perf_records.normal_set);
  49. printf("Test: prd_utils_set_kv() pass\n\n");
  50. return 0;
  51. }
  52. static int test_prd_utils_get_kv()
  53. {
  54. char *get_val;
  55. printf("Test: prd_utils_get_kv() start\n");
  56. GET_TIME_BEGIN();
  57. PRD_UTILS_ASSERT(prd_utils_get_kv(test_key, &get_val) == PRD_UTILS_RESULT_OK);
  58. GET_TIME_RECORD(perf_records.normal_get);
  59. PRD_UTILS_ASSERT(strncmp(test_val, get_val, PRD_UTILS_VAL_MAX_LENGTH) == 0);
  60. printf("Test: prd_utils_get_kv() pass\n\n");
  61. return 0;
  62. }
  63. static int test_prd_utils_clean_kv()
  64. {
  65. char *get_val;
  66. printf("Test: prd_utils_clean_kv() start\n");
  67. GET_TIME_BEGIN();
  68. PRD_UTILS_ASSERT(prd_utils_clean_kv(test_key) == PRD_UTILS_RESULT_OK);
  69. GET_TIME_RECORD(perf_records.normal_clean);
  70. PRD_UTILS_ASSERT(prd_utils_get_kv(test_key, &get_val) == PRD_UTILS_RESULT_KV_NOT_EXIST);
  71. printf("Test: prd_utils_clean_kv() pass\n\n");
  72. return 0;
  73. }
  74. static int test_prd_utils_max_kv_value_length()
  75. {
  76. char *get_val;
  77. printf("Test: max value length(%u) start\n", PRD_UTILS_VAL_MAX_LENGTH);
  78. PRD_UTILS_ASSERT(strlen(max_length_val) == PRD_UTILS_VAL_MAX_LENGTH - 1);
  79. PRD_UTILS_ASSERT(prd_utils_set_kv(test_key, max_length_val) == PRD_UTILS_RESULT_OK);
  80. PRD_UTILS_ASSERT(prd_utils_get_kv(test_key, &get_val) == PRD_UTILS_RESULT_OK);
  81. PRD_UTILS_ASSERT(strncmp(max_length_val, get_val, PRD_UTILS_VAL_MAX_LENGTH) == 0);
  82. PRD_UTILS_ASSERT(prd_utils_clean_kv(test_key) == PRD_UTILS_RESULT_OK);
  83. PRD_UTILS_ASSERT(prd_utils_get_kv(test_key, &get_val) == PRD_UTILS_RESULT_KV_NOT_EXIST);
  84. printf("Test: max value length(%u) pass\n\n", PRD_UTILS_VAL_MAX_LENGTH);
  85. return 0;
  86. }
  87. static int test_prd_utils_max_kv_key_length()
  88. {
  89. char *get_val;
  90. printf("Test: max key length(%u) start\n", PRD_UTILS_KEY_MAX_LENGTH);
  91. PRD_UTILS_ASSERT(strlen(max_length_key) == PRD_UTILS_KEY_MAX_LENGTH - 1);
  92. PRD_UTILS_ASSERT(prd_utils_set_kv(max_length_key, test_val) == PRD_UTILS_RESULT_OK);
  93. PRD_UTILS_ASSERT(prd_utils_get_kv(max_length_key, &get_val) == PRD_UTILS_RESULT_OK);
  94. PRD_UTILS_ASSERT(strncmp(test_val, get_val, PRD_UTILS_VAL_MAX_LENGTH) == 0);
  95. PRD_UTILS_ASSERT(prd_utils_clean_kv(max_length_key) == PRD_UTILS_RESULT_OK);
  96. PRD_UTILS_ASSERT(prd_utils_get_kv(max_length_key, &get_val) == PRD_UTILS_RESULT_KV_NOT_EXIST);
  97. printf("Test: max key length(%u) pass\n\n", PRD_UTILS_KEY_MAX_LENGTH);
  98. return 0;
  99. }
  100. static int test_prd_utils_max_kv_capacity()
  101. {
  102. char *get_val;
  103. char test_key[PRD_UTILS_KEY_MAX_LENGTH];
  104. char test_val[PRD_UTILS_VAL_MAX_LENGTH];
  105. printf("Test: max kv capacity(key_len=%u, val_len=%u, count=%u) start\n",
  106. PRD_UTILS_KEY_MAX_LENGTH, PRD_UTILS_VAL_MAX_LENGTH, PRD_UTILS_KV_MAX_COUNT);
  107. const int test_loop_count = PRD_UTILS_KV_MAX_COUNT;
  108. // set KVs
  109. printf(" step1. testing set max kv(loop=%d):\n ", test_loop_count);
  110. for (int i = 0; i < test_loop_count; i++)
  111. {
  112. //snprintf(test_key, PRD_UTILS_KEY_MAX_LENGTH, test_key_long_fmt, i);
  113. snprintf(test_key, PRD_UTILS_KEY_MAX_LENGTH,
  114. "12345678901234567890123456789012345678901234567890123456789_%03d", i);
  115. PRD_UTILS_ASSERT(strlen(test_key) == PRD_UTILS_KEY_MAX_LENGTH - 1);
  116. snprintf(test_val, PRD_UTILS_VAL_MAX_LENGTH,
  117. "12345678901234567890123456789012345678901234567890" \
  118. "12345678901234567890123456789012345678901234567890" \
  119. "12345678901234567890123456789012345678901234567890" \
  120. "12345678901234567890123456789012345678901234567890" \
  121. "12345678901234567890123456789012345678901234567890" \
  122. "1_%03d", i);
  123. PRD_UTILS_ASSERT(strlen(test_val) == PRD_UTILS_VAL_MAX_LENGTH - 1);
  124. if (i == 0) GET_TIME_BEGIN();
  125. PRD_UTILS_ASSERT(prd_utils_set_kv(test_key, test_val) == PRD_UTILS_RESULT_OK);
  126. if (i == 0) GET_TIME_RECORD(perf_records.max_len_set);
  127. printf("."); fflush(stdout); if (i % 64 == 63) printf("\n ");
  128. }
  129. // get KVs and verify, clean
  130. printf("step2. testing get max kv(loop=%d):\n ", test_loop_count);
  131. for (int i = 0; i < test_loop_count; i++)
  132. {
  133. snprintf(test_key, PRD_UTILS_KEY_MAX_LENGTH,
  134. "12345678901234567890123456789012345678901234567890123456789_%03d", i);
  135. snprintf(test_val, PRD_UTILS_VAL_MAX_LENGTH,
  136. "12345678901234567890123456789012345678901234567890" \
  137. "12345678901234567890123456789012345678901234567890" \
  138. "12345678901234567890123456789012345678901234567890" \
  139. "12345678901234567890123456789012345678901234567890" \
  140. "12345678901234567890123456789012345678901234567890" \
  141. "1_%03d", i);
  142. if (i == 0) GET_TIME_BEGIN();
  143. PRD_UTILS_ASSERT(prd_utils_get_kv(test_key, &get_val) == PRD_UTILS_RESULT_OK);
  144. if (i == 0) GET_TIME_RECORD(perf_records.max_len_get);
  145. PRD_UTILS_ASSERT(strncmp(test_val, get_val, PRD_UTILS_VAL_MAX_LENGTH) == 0);
  146. printf("."); fflush(stdout); if (i % 64 == 63) printf("\n ");
  147. }
  148. // clean KVs
  149. printf("step3. testing clean max kv(loop=%d):\n ", test_loop_count);
  150. for (int i = 0; i < test_loop_count; i++)
  151. {
  152. snprintf(test_key, PRD_UTILS_KEY_MAX_LENGTH,
  153. "12345678901234567890123456789012345678901234567890123456789_%03d", i);
  154. if (i == 0) GET_TIME_BEGIN();
  155. PRD_UTILS_ASSERT(prd_utils_clean_kv(test_key) == PRD_UTILS_RESULT_OK);
  156. if (i == 0) GET_TIME_RECORD(perf_records.max_len_clean);
  157. PRD_UTILS_ASSERT(prd_utils_get_kv(test_key, &get_val) == PRD_UTILS_RESULT_KV_NOT_EXIST);
  158. printf("."); fflush(stdout); if (i % 64 == 63) printf("\n ");
  159. }
  160. printf("Test: max kv capacity(key_len=%u, val_len=%u, count=%u) pass\n\n",
  161. PRD_UTILS_KEY_MAX_LENGTH, PRD_UTILS_VAL_MAX_LENGTH, PRD_UTILS_KV_MAX_COUNT);
  162. return 0;
  163. }
  164. int main(int argc, char *argv[])
  165. {
  166. // test normal usage
  167. PRD_UTILS_ASSERT(test_prd_utils_set_kv() == 0);
  168. PRD_UTILS_ASSERT(test_prd_utils_get_kv() == 0);
  169. PRD_UTILS_ASSERT(test_prd_utils_clean_kv() == 0);
  170. // test max length
  171. PRD_UTILS_ASSERT(test_prd_utils_max_kv_value_length() == 0);
  172. PRD_UTILS_ASSERT(test_prd_utils_max_kv_key_length() == 0);
  173. // test max capacity
  174. PRD_UTILS_ASSERT(test_prd_utils_max_kv_capacity() == 0);
  175. // print performance
  176. printf("==== prd_utils performance test results ====================\n");
  177. printf(" normal set_kv(key_len=%zu, val_len=%zu) cost %.3f ms\n",
  178. strlen(test_key), strlen(test_val), (1.0 * perf_records.normal_set) / 1000);
  179. printf(" normal get_kv(key_len=%zu, val_len=%zu) cost %.3f ms\n",
  180. strlen(test_key), strlen(test_val), (1.0 * perf_records.normal_get) / 1000);
  181. printf(" normal clean_kv(key_len=%zu, val_len=%zu) cost %.3f ms\n",
  182. strlen(test_key), strlen(test_val), (1.0 * perf_records.normal_clean) / 1000);
  183. printf(" max_len set_kv(key_len=%u, val_len=%u) cost %.3f ms\n",
  184. PRD_UTILS_KEY_MAX_LENGTH, PRD_UTILS_VAL_MAX_LENGTH, (1.0 * perf_records.max_len_set) / 1000);
  185. printf(" max_len get_kv(key_len=%u, val_len=%u) cost %.3f ms\n",
  186. PRD_UTILS_KEY_MAX_LENGTH, PRD_UTILS_VAL_MAX_LENGTH, (1.0 * perf_records.max_len_get) / 1000);
  187. printf(" max_len clean_kv(key_len=%u, val_len=%u) cost %.3f ms\n",
  188. PRD_UTILS_KEY_MAX_LENGTH, PRD_UTILS_VAL_MAX_LENGTH, (1.0 * perf_records.max_len_clean) / 1000);
  189. printf("============================================================\n");
  190. PRD_UTILS_ASSERT(perf_records.normal_set/1000 < KV_OP_MAX_DURATION &&
  191. perf_records.normal_get/1000 < KV_OP_MAX_DURATION &&
  192. perf_records.normal_clean/1000 < KV_OP_MAX_DURATION &&
  193. perf_records.max_len_set/1000 < KV_OP_MAX_DURATION &&
  194. perf_records.max_len_get/1000 < KV_OP_MAX_DURATION &&
  195. perf_records.max_len_clean/1000 < KV_OP_MAX_DURATION);
  196. printf(LOG_COLOR_GREEN "All KV tests PASSED\n" LOG_COLOR_RESET);
  197. return 0;
  198. }