benchmark.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2018 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/benchmark.h
  18. * @brief benchmarking for various operations
  19. * @author Florian Dold <flo@dold.me>
  20. */
  21. #ifndef BENCHMARK_H_
  22. #define BENCHMARK_H_
  23. #include "gnunet_time_lib.h"
  24. /**
  25. * Maximum length of URLs considered for benchmarking.
  26. * Shorter URLs are simply truncated.
  27. */
  28. #define MAX_BENCHMARK_URL_LEN 128
  29. #if ENABLE_BENCHMARK
  30. #define BENCHMARK_START(opname) \
  31. struct GNUNET_TIME_Absolute _benchmark_##opname##_start = GNUNET_TIME_absolute_get ()
  32. #define BENCHMARK_END(opname) do { \
  33. { \
  34. struct GNUNET_TIME_Absolute _benchmark_##opname##_end = GNUNET_TIME_absolute_get (); \
  35. struct BenchmarkData *bd = get_benchmark_data (); \
  36. bd->opname##_count++; \
  37. bd->opname##_time = \
  38. GNUNET_TIME_relative_add (bd->opname##_time, \
  39. GNUNET_TIME_absolute_get_difference (_benchmark_##opname##_start, \
  40. _benchmark_##opname##_end)); \
  41. } \
  42. } while (0)
  43. #else
  44. #define BENCHMARK_START(opname) do { } while (0)
  45. #define BENCHMARK_END(opname) do { } while (0)
  46. #endif
  47. /**
  48. * Struct for benchmark data for one URL.
  49. */
  50. struct UrlRequestData
  51. {
  52. /**
  53. * Request URL, truncated (but 0-terminated).
  54. */
  55. char request_url[MAX_BENCHMARK_URL_LEN];
  56. /**
  57. * HTTP status code.
  58. */
  59. unsigned int status;
  60. /**
  61. * How often was the URL requested?
  62. */
  63. uint64_t count;
  64. /**
  65. * Total time spent requesting this URL.
  66. */
  67. struct GNUNET_TIME_Relative time;
  68. /**
  69. * Slowest time to response.
  70. */
  71. struct GNUNET_TIME_Relative time_max;
  72. /**
  73. * Fastest time to response.
  74. */
  75. struct GNUNET_TIME_Relative time_min;
  76. };
  77. #define GNUNET_DECLARE_BENCHMARK_OP(opname) \
  78. uint64_t opname##_count; \
  79. struct GNUNET_TIME_Relative opname##_time
  80. /**
  81. * Thread-local struct for benchmarking data.
  82. */
  83. struct BenchmarkData
  84. {
  85. GNUNET_DECLARE_BENCHMARK_OP (ecc_ecdh);
  86. GNUNET_DECLARE_BENCHMARK_OP (ecdh_eddsa);
  87. GNUNET_DECLARE_BENCHMARK_OP (ecdhe_key_create);
  88. GNUNET_DECLARE_BENCHMARK_OP (ecdhe_key_get_public);
  89. GNUNET_DECLARE_BENCHMARK_OP (ecdsa_ecdh);
  90. GNUNET_DECLARE_BENCHMARK_OP (ecdsa_key_create);
  91. GNUNET_DECLARE_BENCHMARK_OP (ecdsa_key_get_public);
  92. GNUNET_DECLARE_BENCHMARK_OP (ecdsa_sign);
  93. GNUNET_DECLARE_BENCHMARK_OP (ecdsa_verify);
  94. GNUNET_DECLARE_BENCHMARK_OP (eddsa_ecdh);
  95. GNUNET_DECLARE_BENCHMARK_OP (eddsa_key_create);
  96. GNUNET_DECLARE_BENCHMARK_OP (eddsa_key_get_public);
  97. GNUNET_DECLARE_BENCHMARK_OP (eddsa_sign);
  98. GNUNET_DECLARE_BENCHMARK_OP (eddsa_verify);
  99. GNUNET_DECLARE_BENCHMARK_OP (hash);
  100. GNUNET_DECLARE_BENCHMARK_OP (hash_context_finish);
  101. GNUNET_DECLARE_BENCHMARK_OP (hash_context_read);
  102. GNUNET_DECLARE_BENCHMARK_OP (hash_context_start);
  103. GNUNET_DECLARE_BENCHMARK_OP (hkdf);
  104. GNUNET_DECLARE_BENCHMARK_OP (rsa_blind);
  105. GNUNET_DECLARE_BENCHMARK_OP (rsa_private_key_create);
  106. GNUNET_DECLARE_BENCHMARK_OP (rsa_private_key_get_public);
  107. GNUNET_DECLARE_BENCHMARK_OP (rsa_sign_blinded);
  108. GNUNET_DECLARE_BENCHMARK_OP (rsa_unblind);
  109. GNUNET_DECLARE_BENCHMARK_OP (rsa_verify);
  110. struct UrlRequestData *urd;
  111. unsigned int urd_len;
  112. unsigned int urd_capacity;
  113. };
  114. #undef GNUNET_DECLARE_BENCHMARK_OP
  115. /**
  116. * Acquire the benchmark data for the current thread, allocate if necessary.
  117. * Installs handler to collect the benchmark data on thread termination.
  118. *
  119. * @return benchmark data for the current thread
  120. */
  121. struct BenchmarkData *
  122. get_benchmark_data (void);
  123. /**
  124. * Get benchmark data for a URL. If the URL is too long, it's truncated
  125. * before looking up the correspoding benchmark data.
  126. *
  127. * Statistics are bucketed by URL and status code.
  128. *
  129. * @param url url to get request data for
  130. * @param status http status code
  131. */
  132. struct UrlRequestData *
  133. get_url_benchmark_data (char *url, unsigned int status);
  134. #endif /* BENCHMARK_H_ */