check_nodestore.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include <open62541/types.h>
  5. #include <open62541/util.h>
  6. #include <open62541/plugin/nodestore_default.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #include "check.h"
  11. #if UA_MULTITHREADING >= 200
  12. #include <pthread.h>
  13. #endif
  14. UA_Nodestore ns;
  15. static void setup(void) {
  16. UA_Nodestore_ZipTree(&ns);
  17. }
  18. static void teardown(void) {
  19. ns.clear(ns.context);
  20. }
  21. static int zeroCnt = 0;
  22. static int visitCnt = 0;
  23. static void checkZeroVisitor(void *context, const UA_Node* node) {
  24. visitCnt++;
  25. if (node == NULL) zeroCnt++;
  26. }
  27. static UA_Node* createNode(UA_Int16 nsid, UA_Int32 id) {
  28. UA_Node *p = ns.newNode(&ns.context, UA_NODECLASS_VARIABLE);
  29. p->nodeId.identifierType = UA_NODEIDTYPE_NUMERIC;
  30. p->nodeId.namespaceIndex = nsid;
  31. p->nodeId.identifier.numeric = id;
  32. p->nodeClass = UA_NODECLASS_VARIABLE;
  33. return p;
  34. }
  35. START_TEST(replaceExistingNode) {
  36. UA_Node* n1 = createNode(0,2253);
  37. ns.insertNode(ns.context, n1, NULL);
  38. UA_NodeId in1 = UA_NODEID_NUMERIC(0, 2253);
  39. UA_Node* n2;
  40. ns.getNodeCopy(ns.context, &in1, &n2);
  41. UA_StatusCode retval = ns.replaceNode(ns.context, n2);
  42. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  43. }
  44. END_TEST
  45. START_TEST(replaceOldNode) {
  46. UA_Node* n1 = createNode(0,2253);
  47. ns.insertNode(ns.context, n1, NULL);
  48. UA_NodeId in1 = UA_NODEID_NUMERIC(0,2253);
  49. UA_Node* n2;
  50. UA_Node* n3;
  51. ns.getNodeCopy(ns.context, &in1, &n2);
  52. ns.getNodeCopy(ns.context, &in1, &n3);
  53. /* shall succeed */
  54. UA_StatusCode retval = ns.replaceNode(ns.context, n2);
  55. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  56. /* shall fail */
  57. retval = ns.replaceNode(ns.context, n3);
  58. ck_assert_int_ne(retval, UA_STATUSCODE_GOOD);
  59. }
  60. END_TEST
  61. START_TEST(findNodeInUA_NodeStoreWithSingleEntry) {
  62. UA_Node* n1 = createNode(0,2253);
  63. ns.insertNode(ns.context, n1, NULL);
  64. UA_NodeId in1 = UA_NODEID_NUMERIC(0,2253);
  65. const UA_Node* nr = ns.getNode(ns.context, &in1);
  66. ck_assert_int_eq((uintptr_t)n1, (uintptr_t)nr);
  67. ns.releaseNode(ns.context, nr);
  68. }
  69. END_TEST
  70. START_TEST(failToFindNodeInOtherUA_NodeStore) {
  71. UA_Node* n1 = createNode(0,2255);
  72. ns.insertNode(ns.context, n1, NULL);
  73. UA_NodeId in1 = UA_NODEID_NUMERIC(1, 2255);
  74. const UA_Node* nr = ns.getNode(ns.context, &in1);
  75. ck_assert_int_eq((uintptr_t)nr, 0);
  76. }
  77. END_TEST
  78. START_TEST(findNodeInUA_NodeStoreWithSeveralEntries) {
  79. UA_Node* n1 = createNode(0,2253);
  80. ns.insertNode(ns.context, n1, NULL);
  81. UA_Node* n2 = createNode(0,2255);
  82. ns.insertNode(ns.context, n2, NULL);
  83. UA_Node* n3 = createNode(0,2257);
  84. ns.insertNode(ns.context, n3, NULL);
  85. UA_Node* n4 = createNode(0,2200);
  86. ns.insertNode(ns.context, n4, NULL);
  87. UA_Node* n5 = createNode(0,1);
  88. ns.insertNode(ns.context, n5, NULL);
  89. UA_Node* n6 = createNode(0,12);
  90. ns.insertNode(ns.context, n6, NULL);
  91. UA_NodeId in3 = UA_NODEID_NUMERIC(0, 2257);
  92. const UA_Node* nr = ns.getNode(ns.context, &in3);
  93. ck_assert_int_eq((uintptr_t)nr, (uintptr_t)n3);
  94. ns.releaseNode(ns.context, nr);
  95. }
  96. END_TEST
  97. START_TEST(iterateOverUA_NodeStoreShallNotVisitEmptyNodes) {
  98. UA_Node* n1 = createNode(0,2253);
  99. ns.insertNode(ns.context, n1, NULL);
  100. UA_Node* n2 = createNode(0,2255);
  101. ns.insertNode(ns.context, n2, NULL);
  102. UA_Node* n3 = createNode(0,2257);
  103. ns.insertNode(ns.context, n3, NULL);
  104. UA_Node* n4 = createNode(0,2200);
  105. ns.insertNode(ns.context, n4, NULL);
  106. UA_Node* n5 = createNode(0,1);
  107. ns.insertNode(ns.context, n5, NULL);
  108. UA_Node* n6 = createNode(0,12);
  109. ns.insertNode(ns.context, n6, NULL);
  110. zeroCnt = 0;
  111. visitCnt = 0;
  112. ns.iterate(ns.context, checkZeroVisitor, NULL);
  113. ck_assert_int_eq(zeroCnt, 0);
  114. ck_assert_int_eq(visitCnt, 6);
  115. }
  116. END_TEST
  117. START_TEST(findNodeInExpandedNamespace) {
  118. for(UA_UInt32 i = 0; i < 200; i++) {
  119. UA_Node* n = createNode(0,i);
  120. ns.insertNode(ns.context, n, NULL);
  121. }
  122. // when
  123. UA_Node *n2 = createNode(0,25);
  124. const UA_Node* nr = ns.getNode(ns.context, &n2->nodeId);
  125. ck_assert_int_eq(nr->nodeId.identifier.numeric, n2->nodeId.identifier.numeric);
  126. ns.releaseNode(ns.context, nr);
  127. ns.deleteNode(ns.context, n2);
  128. }
  129. END_TEST
  130. START_TEST(iterateOverExpandedNamespaceShallNotVisitEmptyNodes) {
  131. for(UA_UInt32 i = 0; i < 200; i++) {
  132. UA_Node* n = createNode(0,i+1);
  133. ns.insertNode(ns.context, n, NULL);
  134. }
  135. // when
  136. zeroCnt = 0;
  137. visitCnt = 0;
  138. ns.iterate(ns.context, checkZeroVisitor, NULL);
  139. // then
  140. ck_assert_int_eq(zeroCnt, 0);
  141. ck_assert_int_eq(visitCnt, 200);
  142. }
  143. END_TEST
  144. START_TEST(failToFindNonExistentNodeInUA_NodeStoreWithSeveralEntries) {
  145. UA_Node* n1 = createNode(0,2253);
  146. ns.insertNode(ns.context, n1, NULL);
  147. UA_Node* n2 = createNode(0,2255);
  148. ns.insertNode(ns.context, n2, NULL);
  149. UA_Node* n3 = createNode(0,2257);
  150. ns.insertNode(ns.context, n3, NULL);
  151. UA_Node* n4 = createNode(0,2200);
  152. ns.insertNode(ns.context, n4, NULL);
  153. UA_Node* n5 = createNode(0,1);
  154. ns.insertNode(ns.context, n5, NULL);
  155. UA_NodeId id = UA_NODEID_NUMERIC(0, 12);
  156. const UA_Node* nr = ns.getNode(ns.context, &id);
  157. ck_assert_int_eq((uintptr_t)nr, 0);
  158. }
  159. END_TEST
  160. /************************************/
  161. /* Performance Profiling Test Cases */
  162. /************************************/
  163. #if UA_MULTITHREADING >= 200
  164. struct UA_NodeStoreProfileTest {
  165. UA_Int32 min_val;
  166. UA_Int32 max_val;
  167. UA_Int32 rounds;
  168. };
  169. static void *profileGetThread(void *arg) {
  170. struct UA_NodeStoreProfileTest *test = (struct UA_NodeStoreProfileTest*) arg;
  171. UA_NodeId id;
  172. UA_NodeId_init(&id);
  173. UA_Int32 max_val = test->max_val;
  174. for(UA_Int32 x = 0; x<test->rounds; x++) {
  175. for(UA_Int32 i=test->min_val; i<max_val; i++) {
  176. id.identifier.numeric = i+1;
  177. const UA_Node *n = ns.getNode(ns.context, &id);
  178. ns.releaseNode(ns.context, n);
  179. }
  180. }
  181. return NULL;
  182. }
  183. #endif
  184. #define N 1000 /* make bigger to test */
  185. START_TEST(profileGetDelete) {
  186. clock_t begin, end;
  187. begin = clock();
  188. for(UA_UInt32 i = 0; i < N; i++) {
  189. UA_Node *n = createNode(0,i+1);
  190. ns.insertNode(ns.context, n, NULL);
  191. }
  192. #if UA_MULTITHREADING >= 200
  193. #define THREADS 4
  194. pthread_t t[THREADS];
  195. struct UA_NodeStoreProfileTest p[THREADS];
  196. for (int i = 0; i < THREADS; i++) {
  197. p[i] = (struct UA_NodeStoreProfileTest){i*(N/THREADS), (i+1)*(N/THREADS), 50};
  198. pthread_create(&t[i], NULL, profileGetThread, &p[i]);
  199. }
  200. for (int i = 0; i < THREADS; i++)
  201. pthread_join(t[i], NULL);
  202. end = clock();
  203. printf("Time for %d create/get/delete on %d threads in a namespace: %fs.\n", N, THREADS, (double)(end - begin) / CLOCKS_PER_SEC);
  204. #else
  205. UA_NodeId id = UA_NODEID_NULL;
  206. for(size_t i = 0; i < N; i++) {
  207. id.identifier.numeric = (UA_UInt32)i+1;
  208. const UA_Node *node = ns.getNode(ns.context, &id);
  209. ns.releaseNode(ns.context, node);
  210. }
  211. end = clock();
  212. printf("Time for single-threaded %d create/get/delete in a namespace: %fs.\n", N,
  213. (double)(end - begin) / CLOCKS_PER_SEC);
  214. #endif
  215. }
  216. END_TEST
  217. static Suite * namespace_suite (void) {
  218. Suite *s = suite_create ("UA_NodeStore");
  219. TCase* tc_find = tcase_create ("Find");
  220. tcase_add_checked_fixture(tc_find, setup, teardown);
  221. tcase_add_test (tc_find, findNodeInUA_NodeStoreWithSingleEntry);
  222. tcase_add_test (tc_find, findNodeInUA_NodeStoreWithSeveralEntries);
  223. tcase_add_test (tc_find, findNodeInExpandedNamespace);
  224. tcase_add_test (tc_find, failToFindNonExistentNodeInUA_NodeStoreWithSeveralEntries);
  225. tcase_add_test (tc_find, failToFindNodeInOtherUA_NodeStore);
  226. suite_add_tcase (s, tc_find);
  227. TCase *tc_replace = tcase_create("Replace");
  228. tcase_add_checked_fixture(tc_replace, setup, teardown);
  229. tcase_add_test (tc_replace, replaceExistingNode);
  230. tcase_add_test (tc_replace, replaceOldNode);
  231. suite_add_tcase (s, tc_replace);
  232. TCase* tc_iterate = tcase_create ("Iterate");
  233. tcase_add_checked_fixture(tc_iterate, setup, teardown);
  234. tcase_add_test (tc_iterate, iterateOverUA_NodeStoreShallNotVisitEmptyNodes);
  235. tcase_add_test (tc_iterate, iterateOverExpandedNamespaceShallNotVisitEmptyNodes);
  236. suite_add_tcase (s, tc_iterate);
  237. TCase* tc_profile = tcase_create ("Profile");
  238. tcase_add_checked_fixture(tc_profile, setup, teardown);
  239. tcase_add_test (tc_profile, profileGetDelete);
  240. suite_add_tcase (s, tc_profile);
  241. return s;
  242. }
  243. int main (void) {
  244. int number_failed = 0;
  245. Suite *s = namespace_suite();
  246. SRunner *sr = srunner_create(s);
  247. srunner_set_fork_status(sr,CK_NOFORK);
  248. srunner_run_all(sr, CK_NORMAL);
  249. number_failed += srunner_ntests_failed (sr);
  250. srunner_free(sr);
  251. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  252. }