test_log.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <inttypes.h>
  9. #include "unity.h"
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "esp_log.h"
  13. #include "esp_timer.h"
  14. #include "sdkconfig.h"
  15. static const char * TAG = "log_test";
  16. static int calc_time_of_logging(int iterations)
  17. {
  18. int diff;
  19. int64_t start, end;
  20. int attempts = 2;
  21. while (attempts--) {
  22. start = esp_timer_get_time();
  23. for (int i = 0; i < iterations; i++) {
  24. ESP_LOGI(TAG, "some test data, %d, %d, %d", i, iterations - i, 12);
  25. }
  26. end = esp_timer_get_time();
  27. }
  28. diff = (int)(end - start);
  29. printf("%d iterations took %d usec (%.02f usec per invocation)\n",
  30. iterations, diff, (float)diff / iterations);
  31. return diff;
  32. }
  33. TEST_CASE("test master logging level performance", "[log]")
  34. {
  35. const int ITERATIONS = 1000;
  36. ESP_LOGI(TAG, "Start");
  37. #ifdef CONFIG_LOG_MASTER_LEVEL
  38. esp_log_set_level_master(ESP_LOG_NONE);
  39. TEST_ASSERT_INT_WITHIN(100, 150, calc_time_of_logging(ITERATIONS));
  40. #else
  41. esp_log_level_set("*", ESP_LOG_NONE);
  42. TEST_ASSERT_INT_WITHIN(5, 11, calc_time_of_logging(ITERATIONS) / ITERATIONS);
  43. #endif
  44. esp_log_level_set("*", ESP_LOG_NONE);
  45. #ifdef CONFIG_LOG_MASTER_LEVEL
  46. esp_log_set_level_master(ESP_LOG_DEBUG);
  47. #endif
  48. TEST_ASSERT_INT_WITHIN(5, 11, calc_time_of_logging(ITERATIONS) / ITERATIONS);
  49. esp_log_level_set("*", ESP_LOG_INFO);
  50. ESP_LOGI(TAG, "End");
  51. }