check_timer.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 "ua_timer.h"
  5. #include "check.h"
  6. #include <time.h>
  7. #include <stdio.h>
  8. #define N_EVENTS 10000
  9. size_t count = 0;
  10. static void
  11. timerCallback(void *application, void *data) {
  12. count++;
  13. }
  14. static void
  15. executionCallback(void *executionApplication, UA_ApplicationCallback cb,
  16. void *callbackApplication, void *data) {
  17. cb(callbackApplication, data);
  18. }
  19. /* Create empty events with different callback intervals */
  20. static void
  21. createEvents(UA_Timer *t, UA_UInt32 events) {
  22. for(size_t i = 0; i < events; i++) {
  23. UA_Double interval = (UA_Double)i+1;
  24. UA_StatusCode retval =
  25. UA_Timer_addRepeatedCallback(t, timerCallback, NULL, NULL, interval, NULL);
  26. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  27. }
  28. }
  29. START_TEST(benchmarkTimer) {
  30. UA_Timer timer;
  31. UA_Timer_init(&timer);
  32. createEvents(&timer, N_EVENTS);
  33. clock_t begin = clock();
  34. UA_DateTime now = 0;
  35. for(size_t i = 0; i < 1000; i++) {
  36. UA_DateTime next = UA_Timer_process(&timer, now, executionCallback, NULL);
  37. /* At least 100 msec distance between _process */
  38. now = next + (UA_DATETIME_MSEC * 100);
  39. if(next > now)
  40. now = next;
  41. }
  42. clock_t finish = clock();
  43. double time_spent = (double)(finish - begin) / CLOCKS_PER_SEC;
  44. printf("duration was %f s\n", time_spent);
  45. printf("%lu callbacks\n", (unsigned long)count);
  46. UA_Timer_deleteMembers(&timer);
  47. } END_TEST
  48. int main(void) {
  49. Suite *s = suite_create("Test Event Timer");
  50. TCase *tc = tcase_create("test cases");
  51. tcase_add_test(tc, benchmarkTimer);
  52. suite_add_tcase(s, tc);
  53. SRunner *sr = srunner_create(s);
  54. srunner_set_fork_status(sr, CK_NOFORK);
  55. srunner_run_all (sr, CK_NORMAL);
  56. int number_failed = srunner_ntests_failed(sr);
  57. srunner_free(sr);
  58. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  59. }