ccomp_timer_test_api.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include "esp_timer.h"
  4. #include "esp_log.h"
  5. #include "esp_attr.h"
  6. #include "ccomp_timer.h"
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #ifndef CONFIG_FREERTOS_UNICORE
  10. #include "esp_ipc.h"
  11. #endif
  12. #include "unity.h"
  13. #ifndef CONFIG_FREERTOS_UNICORE
  14. static void start_timer(void *param)
  15. {
  16. esp_err_t *err = (esp_err_t *)param;
  17. *err = ccomp_timer_start();
  18. }
  19. static void stop_timer(void *param)
  20. {
  21. int64_t *t = (int64_t *)param;
  22. *t = ccomp_timer_stop();
  23. }
  24. #endif
  25. static void computation(void *param)
  26. {
  27. int *l = (int *)param;
  28. for (volatile int i = 0, a = 0; i < *l; i++)
  29. {
  30. a += i;
  31. }
  32. }
  33. TEST_CASE("starting and stopping works", "[test_utils][ccomp_timer]")
  34. {
  35. esp_err_t err;
  36. int64_t t;
  37. /*
  38. * Test on the same task
  39. */
  40. err = ccomp_timer_start();
  41. TEST_ASSERT_EQUAL(ESP_OK, err);
  42. // Start an already started timer
  43. err = ccomp_timer_start();
  44. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, err);
  45. t = ccomp_timer_stop();
  46. TEST_ASSERT_GREATER_OR_EQUAL(0, t);
  47. // Stopping a non started timer
  48. t = ccomp_timer_stop();
  49. TEST_ASSERT_EQUAL(-1, t);
  50. #ifndef CONFIG_FREERTOS_UNICORE
  51. /*
  52. * Test on different task on same core
  53. */
  54. err = ccomp_timer_start();
  55. TEST_ASSERT_EQUAL(ESP_OK, err);
  56. esp_ipc_call_blocking(xPortGetCoreID(), start_timer, &err);
  57. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, err);
  58. t = ccomp_timer_stop();
  59. TEST_ASSERT_GREATER_OR_EQUAL(0, t);
  60. esp_ipc_call_blocking(xPortGetCoreID(), stop_timer, &t);
  61. TEST_ASSERT_EQUAL(-1, t);
  62. /*
  63. * Timer being stopped from another task on the same core
  64. */
  65. err = ccomp_timer_start();
  66. TEST_ASSERT_EQUAL(ESP_OK, err);
  67. esp_ipc_call_blocking(xPortGetCoreID(), stop_timer, &t);
  68. TEST_ASSERT_GREATER_OR_EQUAL(0, t);
  69. /*
  70. * Test on different task on same core
  71. */
  72. err = ccomp_timer_start();
  73. TEST_ASSERT_EQUAL(ESP_OK, err);
  74. esp_ipc_call_blocking(xPortGetCoreID() == 0 ? 1 : 0, start_timer, &err);
  75. TEST_ASSERT_EQUAL(ESP_OK, err);
  76. t = ccomp_timer_stop();
  77. TEST_ASSERT_GREATER_OR_EQUAL(0, t);
  78. esp_ipc_call_blocking(xPortGetCoreID() == 0 ? 1 : 0, stop_timer, &t);
  79. TEST_ASSERT_GREATER_OR_EQUAL(0, t);
  80. #endif
  81. }
  82. TEST_CASE("getting the time works", "[test_utils][ccomp_timer]")
  83. {
  84. // Get wall time and start ccomp timer
  85. int64_t start = esp_timer_get_time();
  86. ccomp_timer_start();
  87. int64_t t_a = ccomp_timer_get_time();
  88. int temp = 10000;
  89. computation(&temp);
  90. int64_t t_b = ccomp_timer_get_time();
  91. // Check that ccomp time after computation is more than
  92. // ccomp time before computation.
  93. TEST_ASSERT_LESS_THAN(t_b, t_a);
  94. // Get time diff between wall time and ccomp time
  95. int64_t t_1 = ccomp_timer_stop();
  96. int64_t t_2 = esp_timer_get_time() - start;
  97. // The times should at least be in the same ballpark (at least within 10%)
  98. float diff = (abs(t_1 - t_2)) / ((float)t_2);
  99. TEST_ASSERT(diff <= 10.0f);
  100. // Since the timer was already stopped, test that ccomp_timer_get_time
  101. // returns the same time as ccomp_timer_stop
  102. int64_t t_c = ccomp_timer_get_time();
  103. TEST_ASSERT_EQUAL(t_1, t_c);
  104. }
  105. #ifndef CONFIG_FREERTOS_UNICORE
  106. TEST_CASE("timers for each core counts independently", "[test_utils][ccomp_timer]")
  107. {
  108. esp_err_t err;
  109. // Start a timer on this core
  110. err = ccomp_timer_start();
  111. TEST_ASSERT_EQUAL(ESP_OK, err);
  112. // Do some work on this core
  113. int temp = 10000;
  114. computation(&temp);
  115. // Start a timer on the other core
  116. esp_ipc_call_blocking(xPortGetCoreID() == 0 ? 1 : 0, start_timer, &err);
  117. TEST_ASSERT_EQUAL(ESP_OK, err);
  118. // Do some work on other core (less work than this core did)
  119. temp = 5000;
  120. esp_ipc_call_blocking(xPortGetCoreID() == 0 ? 1 : 0, computation, &temp);
  121. // Stop timers from both cores
  122. int64_t t_1 = ccomp_timer_stop();
  123. TEST_ASSERT_GREATER_OR_EQUAL(0, t_1);
  124. int64_t t_2;
  125. esp_ipc_call_blocking(xPortGetCoreID() == 0 ? 1 : 0, stop_timer, &t_2);
  126. TEST_ASSERT_GREATER_OR_EQUAL(0, t_2);
  127. // Since this core did more work, it probably has longer measured time
  128. TEST_ASSERT_GREATER_THAN(t_2, t_1);
  129. }
  130. #endif