check_nodestore.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #include "ua_types.h"
  8. #include "server/ua_nodestore.h"
  9. #include "ua_util.h"
  10. #include "check.h"
  11. #ifdef UA_ENABLE_MULTITHREADING
  12. #include <pthread.h>
  13. #include <urcu.h>
  14. #endif
  15. int zeroCnt = 0;
  16. int visitCnt = 0;
  17. static void checkZeroVisitor(const UA_Node* node) {
  18. visitCnt++;
  19. if (node == NULL) zeroCnt++;
  20. }
  21. static void printVisitor(const UA_Node* node) {
  22. printf("%d\n", node->nodeId.identifier.numeric);
  23. }
  24. static UA_Node* createNode(UA_Int16 nsid, UA_Int32 id) {
  25. UA_Node *p = (UA_Node *)UA_NodeStore_newVariableNode();
  26. p->nodeId.identifierType = UA_NODEIDTYPE_NUMERIC;
  27. p->nodeId.namespaceIndex = nsid;
  28. p->nodeId.identifier.numeric = id;
  29. p->nodeClass = UA_NODECLASS_VARIABLE;
  30. return p;
  31. }
  32. START_TEST(replaceExistingNode) {
  33. UA_NodeStore *ns = UA_NodeStore_new();
  34. UA_Node* n1 = createNode(0,2253);
  35. UA_NodeStore_insert(ns, n1);
  36. UA_NodeId in1 = UA_NODEID_NUMERIC(0, 2253);
  37. UA_Node* n2 = UA_NodeStore_getCopy(ns, &in1);
  38. UA_StatusCode retval = UA_NodeStore_replace(ns, n2);
  39. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  40. UA_NodeStore_delete(ns);
  41. }
  42. END_TEST
  43. START_TEST(replaceOldNode) {
  44. UA_NodeStore *ns = UA_NodeStore_new();
  45. UA_Node* n1 = createNode(0,2253);
  46. UA_NodeStore_insert(ns, n1);
  47. UA_NodeId in1 = UA_NODEID_NUMERIC(0,2253);
  48. UA_Node* n2 = UA_NodeStore_getCopy(ns, &in1);
  49. UA_Node* n3 = UA_NodeStore_getCopy(ns, &in1);
  50. /* shall succeed */
  51. UA_StatusCode retval = UA_NodeStore_replace(ns, n2);
  52. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  53. /* shall fail */
  54. retval = UA_NodeStore_replace(ns, n3);
  55. ck_assert_int_ne(retval, UA_STATUSCODE_GOOD);
  56. UA_NodeStore_delete(ns);
  57. }
  58. END_TEST
  59. START_TEST(findNodeInUA_NodeStoreWithSingleEntry) {
  60. #ifdef UA_ENABLE_MULTITHREADING
  61. rcu_register_thread();
  62. #endif
  63. // given
  64. UA_NodeStore *ns = UA_NodeStore_new();
  65. UA_Node* n1 = createNode(0,2253);
  66. UA_NodeStore_insert(ns, n1);
  67. UA_NodeId in1 = UA_NODEID_NUMERIC(0,2253);
  68. const UA_Node* nr = UA_NodeStore_get(ns, &in1);
  69. // then
  70. ck_assert_int_eq((uintptr_t)n1, (uintptr_t)nr);
  71. // finally
  72. UA_NodeStore_delete(ns);
  73. #ifdef UA_ENABLE_MULTITHREADING
  74. rcu_unregister_thread();
  75. #endif
  76. }
  77. END_TEST
  78. START_TEST(failToFindNodeInOtherUA_NodeStore) {
  79. #ifdef UA_ENABLE_MULTITHREADING
  80. rcu_register_thread();
  81. #endif
  82. // given
  83. UA_NodeStore *ns = UA_NodeStore_new();
  84. UA_Node* n1 = createNode(0,2255);
  85. UA_NodeStore_insert(ns, n1);
  86. // when
  87. UA_NodeId in1 = UA_NODEID_NUMERIC(1, 2255);
  88. const UA_Node* nr = UA_NodeStore_get(ns, &in1);
  89. // then
  90. ck_assert_int_eq((uintptr_t)nr, 0);
  91. // finally
  92. UA_NodeStore_delete(ns);
  93. #ifdef UA_ENABLE_MULTITHREADING
  94. rcu_unregister_thread();
  95. #endif
  96. }
  97. END_TEST
  98. START_TEST(findNodeInUA_NodeStoreWithSeveralEntries) {
  99. #ifdef UA_ENABLE_MULTITHREADING
  100. rcu_register_thread();
  101. #endif
  102. // given
  103. UA_NodeStore *ns = UA_NodeStore_new();
  104. UA_Node* n1 = createNode(0,2253);
  105. UA_NodeStore_insert(ns, n1);
  106. UA_Node* n2 = createNode(0,2255);
  107. UA_NodeStore_insert(ns, n2);
  108. UA_Node* n3 = createNode(0,2257);
  109. UA_NodeStore_insert(ns, n3);
  110. UA_Node* n4 = createNode(0,2200);
  111. UA_NodeStore_insert(ns, n4);
  112. UA_Node* n5 = createNode(0,1);
  113. UA_NodeStore_insert(ns, n5);
  114. UA_Node* n6 = createNode(0,12);
  115. UA_NodeStore_insert(ns, n6);
  116. // when
  117. UA_NodeId in3 = UA_NODEID_NUMERIC(0, 2257);
  118. const UA_Node* nr = UA_NodeStore_get(ns, &in3);
  119. // then
  120. ck_assert_int_eq((uintptr_t)nr, (uintptr_t)n3);
  121. // finally
  122. UA_NodeStore_delete(ns);
  123. #ifdef UA_ENABLE_MULTITHREADING
  124. rcu_unregister_thread();
  125. #endif
  126. }
  127. END_TEST
  128. START_TEST(iterateOverUA_NodeStoreShallNotVisitEmptyNodes) {
  129. #ifdef UA_ENABLE_MULTITHREADING
  130. rcu_register_thread();
  131. #endif
  132. // given
  133. UA_NodeStore *ns = UA_NodeStore_new();
  134. UA_Node* n1 = createNode(0,2253);
  135. UA_NodeStore_insert(ns, n1);
  136. UA_Node* n2 = createNode(0,2255);
  137. UA_NodeStore_insert(ns, n2);
  138. UA_Node* n3 = createNode(0,2257);
  139. UA_NodeStore_insert(ns, n3);
  140. UA_Node* n4 = createNode(0,2200);
  141. UA_NodeStore_insert(ns, n4);
  142. UA_Node* n5 = createNode(0,1);
  143. UA_NodeStore_insert(ns, n5);
  144. UA_Node* n6 = createNode(0,12);
  145. UA_NodeStore_insert(ns, n6);
  146. // when
  147. zeroCnt = 0;
  148. visitCnt = 0;
  149. UA_NodeStore_iterate(ns,checkZeroVisitor);
  150. // then
  151. ck_assert_int_eq(zeroCnt, 0);
  152. ck_assert_int_eq(visitCnt, 6);
  153. // finally
  154. UA_NodeStore_delete(ns);
  155. #ifdef UA_ENABLE_MULTITHREADING
  156. rcu_unregister_thread();
  157. #endif
  158. }
  159. END_TEST
  160. START_TEST(findNodeInExpandedNamespace) {
  161. #ifdef UA_ENABLE_MULTITHREADING
  162. rcu_register_thread();
  163. #endif
  164. // given
  165. UA_NodeStore *ns = UA_NodeStore_new();
  166. UA_Node* n;
  167. UA_Int32 i=0;
  168. for (; i<200; i++) {
  169. n = createNode(0,i);
  170. UA_NodeStore_insert(ns, n);
  171. }
  172. // when
  173. UA_Node *n2 = createNode(0,25);
  174. const UA_Node* nr = UA_NodeStore_get(ns,&n2->nodeId);
  175. // then
  176. ck_assert_int_eq(nr->nodeId.identifier.numeric,n2->nodeId.identifier.numeric);
  177. // finally
  178. UA_NodeStore_deleteNode(n2);
  179. UA_NodeStore_delete(ns);
  180. #ifdef UA_ENABLE_MULTITHREADING
  181. rcu_unregister_thread();
  182. #endif
  183. }
  184. END_TEST
  185. START_TEST(iterateOverExpandedNamespaceShallNotVisitEmptyNodes) {
  186. #ifdef UA_ENABLE_MULTITHREADING
  187. rcu_register_thread();
  188. #endif
  189. // given
  190. UA_NodeStore *ns = UA_NodeStore_new();
  191. UA_Node* n;
  192. UA_Int32 i=0;
  193. for (; i<200; i++) {
  194. n = createNode(0,i);
  195. UA_NodeStore_insert(ns, n);
  196. }
  197. // when
  198. zeroCnt = 0;
  199. visitCnt = 0;
  200. UA_NodeStore_iterate(ns,checkZeroVisitor);
  201. // then
  202. ck_assert_int_eq(zeroCnt, 0);
  203. ck_assert_int_eq(visitCnt, 200);
  204. // finally
  205. UA_NodeStore_delete(ns);
  206. #ifdef UA_ENABLE_MULTITHREADING
  207. rcu_unregister_thread();
  208. #endif
  209. }
  210. END_TEST
  211. START_TEST(failToFindNonExistantNodeInUA_NodeStoreWithSeveralEntries) {
  212. #ifdef UA_ENABLE_MULTITHREADING
  213. rcu_register_thread();
  214. #endif
  215. // given
  216. UA_NodeStore *ns = UA_NodeStore_new();
  217. UA_Node* n1 = createNode(0,2253);
  218. UA_NodeStore_insert(ns, n1);
  219. UA_Node* n2 = createNode(0,2255);
  220. UA_NodeStore_insert(ns, n2);
  221. UA_Node* n3 = createNode(0,2257);
  222. UA_NodeStore_insert(ns, n3);
  223. UA_Node* n4 = createNode(0,2200);
  224. UA_NodeStore_insert(ns, n4);
  225. UA_Node* n5 = createNode(0,1);
  226. UA_NodeStore_insert(ns, n5);
  227. UA_NodeId id = UA_NODEID_NUMERIC(0, 12);
  228. // when
  229. const UA_Node* nr = UA_NodeStore_get(ns, &id);
  230. // then
  231. ck_assert_int_eq((uintptr_t)nr, 0);
  232. // finally
  233. UA_NodeStore_delete(ns);
  234. #ifdef UA_ENABLE_MULTITHREADING
  235. rcu_unregister_thread();
  236. #endif
  237. }
  238. END_TEST
  239. /************************************/
  240. /* Performance Profiling Test Cases */
  241. /************************************/
  242. #ifdef UA_ENABLE_MULTITHREADING
  243. struct UA_NodeStoreProfileTest {
  244. UA_NodeStore *ns;
  245. UA_Int32 min_val;
  246. UA_Int32 max_val;
  247. UA_Int32 rounds;
  248. };
  249. static void *profileGetThread(void *arg) {
  250. rcu_register_thread();
  251. struct UA_NodeStoreProfileTest *test = (struct UA_NodeStoreProfileTest*) arg;
  252. UA_NodeId id;
  253. UA_NodeId_init(&id);
  254. UA_Int32 max_val = test->max_val;
  255. UA_NodeStore *ns = test->ns;
  256. for(UA_Int32 x = 0; x<test->rounds; x++) {
  257. for(UA_Int32 i=test->min_val; i<max_val; i++) {
  258. id.identifier.numeric = i;
  259. UA_NodeStore_get(ns,&id);
  260. }
  261. }
  262. rcu_unregister_thread();
  263. return NULL;
  264. }
  265. #endif
  266. START_TEST(profileGetDelete) {
  267. #ifdef UA_ENABLE_MULTITHREADING
  268. rcu_register_thread();
  269. #endif
  270. #define N 1000000
  271. UA_NodeStore *ns = UA_NodeStore_new();
  272. UA_Node *n;
  273. for (int i=0; i<N; i++) {
  274. n = createNode(0,i);
  275. UA_NodeStore_insert(ns, n);
  276. }
  277. clock_t begin, end;
  278. begin = clock();
  279. #ifdef UA_ENABLE_MULTITHREADING
  280. #define THREADS 4
  281. pthread_t t[THREADS];
  282. struct UA_NodeStoreProfileTest p[THREADS];
  283. for (int i = 0; i < THREADS; i++) {
  284. p[i] = (struct UA_NodeStoreProfileTest){ns, i*(N/THREADS), (i+1)*(N/THREADS), 50};
  285. pthread_create(&t[i], NULL, profileGetThread, &p[i]);
  286. }
  287. for (int i = 0; i < THREADS; i++)
  288. pthread_join(t[i], NULL);
  289. end = clock();
  290. printf("Time for %d create/get/delete on %d threads in a namespace: %fs.\n", N, THREADS, (double)(end - begin) / CLOCKS_PER_SEC);
  291. #else
  292. UA_NodeId id;
  293. UA_NodeId_init(&id);
  294. for(UA_Int32 x = 0; x<50; x++) {
  295. for(int i=0; i<N; i++) {
  296. id.identifier.numeric = i;
  297. UA_NodeStore_get(ns,&id);
  298. }
  299. }
  300. end = clock();
  301. printf("Time for single-threaded %d create/get/delete in a namespace: %fs.\n", N, (double)(end - begin) / CLOCKS_PER_SEC);
  302. #endif
  303. UA_NodeStore_delete(ns);
  304. #ifdef UA_ENABLE_MULTITHREADING
  305. rcu_unregister_thread();
  306. #endif
  307. }
  308. END_TEST
  309. static Suite * namespace_suite (void) {
  310. Suite *s = suite_create ("UA_NodeStore");
  311. TCase* tc_find = tcase_create ("Find");
  312. tcase_add_test (tc_find, findNodeInUA_NodeStoreWithSingleEntry);
  313. tcase_add_test (tc_find, findNodeInUA_NodeStoreWithSeveralEntries);
  314. tcase_add_test (tc_find, findNodeInExpandedNamespace);
  315. tcase_add_test (tc_find, failToFindNonExistantNodeInUA_NodeStoreWithSeveralEntries);
  316. tcase_add_test (tc_find, failToFindNodeInOtherUA_NodeStore);
  317. suite_add_tcase (s, tc_find);
  318. TCase *tc_replace = tcase_create("Replace");
  319. tcase_add_test (tc_replace, replaceExistingNode);
  320. tcase_add_test (tc_replace, replaceOldNode);
  321. suite_add_tcase (s, tc_replace);
  322. TCase* tc_iterate = tcase_create ("Iterate");
  323. tcase_add_test (tc_iterate, iterateOverUA_NodeStoreShallNotVisitEmptyNodes);
  324. tcase_add_test (tc_iterate, iterateOverExpandedNamespaceShallNotVisitEmptyNodes);
  325. suite_add_tcase (s, tc_iterate);
  326. /* TCase* tc_profile = tcase_create ("Profile"); */
  327. /* tcase_add_test (tc_profile, profileGetDelete); */
  328. /* suite_add_tcase (s, tc_profile); */
  329. return s;
  330. }
  331. int main (void) {
  332. int number_failed = 0;
  333. Suite *s = namespace_suite();
  334. SRunner *sr = srunner_create(s);
  335. srunner_set_fork_status(sr,CK_NOFORK);
  336. srunner_run_all(sr, CK_NORMAL);
  337. number_failed += srunner_ntests_failed (sr);
  338. srunner_free(sr);
  339. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  340. }