check_nodestore.c 9.9 KB

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