check_nodestore.c 9.3 KB

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