benchmark.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 = \
  32. GNUNET_TIME_absolute_get ()
  33. #define BENCHMARK_END(opname) do { \
  34. { \
  35. struct GNUNET_TIME_Absolute _benchmark_ ## opname ## _end = \
  36. GNUNET_TIME_absolute_get (); \
  37. struct BenchmarkData *bd = get_benchmark_data (); \
  38. bd->opname ## _count++; \
  39. bd->opname ## _time = \
  40. GNUNET_TIME_relative_add (bd->opname ## _time, \
  41. GNUNET_TIME_absolute_get_difference ( \
  42. _benchmark_ ## opname ## _start, \
  43. _benchmark_ \
  44. ## opname ## _end)); \
  45. } \
  46. } while (0)
  47. #else
  48. #define BENCHMARK_START(opname) do { } while (0)
  49. #define BENCHMARK_END(opname) do { } while (0)
  50. #endif
  51. /**
  52. * Struct for benchmark data for one URL.
  53. */
  54. struct UrlRequestData
  55. {
  56. /**
  57. * Request URL, truncated (but 0-terminated).
  58. */
  59. char request_url[MAX_BENCHMARK_URL_LEN];
  60. /**
  61. * HTTP status code.
  62. */
  63. unsigned int status;
  64. /**
  65. * How often was the URL requested?
  66. */
  67. uint64_t count;
  68. /**
  69. * How many bytes were sent in total to request the URL.
  70. */
  71. uint64_t bytes_sent;
  72. /**
  73. * How many bytes were received in total as response to requesting this URL.
  74. */
  75. uint64_t bytes_received;
  76. /**
  77. * Total time spent requesting this URL.
  78. */
  79. struct GNUNET_TIME_Relative time;
  80. /**
  81. * Slowest time to response.
  82. */
  83. struct GNUNET_TIME_Relative time_max;
  84. /**
  85. * Fastest time to response.
  86. */
  87. struct GNUNET_TIME_Relative time_min;
  88. };
  89. #define GNUNET_DECLARE_BENCHMARK_OP(opname) \
  90. uint64_t opname ## _count; \
  91. struct GNUNET_TIME_Relative opname ## _time
  92. /**
  93. * Thread-local struct for benchmarking data.
  94. */
  95. struct BenchmarkData
  96. {
  97. GNUNET_DECLARE_BENCHMARK_OP (ecc_ecdh);
  98. GNUNET_DECLARE_BENCHMARK_OP (ecdh_eddsa);
  99. GNUNET_DECLARE_BENCHMARK_OP (ecdhe_key_create);
  100. GNUNET_DECLARE_BENCHMARK_OP (ecdhe_key_get_public);
  101. GNUNET_DECLARE_BENCHMARK_OP (ecdsa_ecdh);
  102. GNUNET_DECLARE_BENCHMARK_OP (ecdsa_key_create);
  103. GNUNET_DECLARE_BENCHMARK_OP (ecdsa_key_get_public);
  104. GNUNET_DECLARE_BENCHMARK_OP (ecdsa_sign);
  105. GNUNET_DECLARE_BENCHMARK_OP (ecdsa_verify);
  106. GNUNET_DECLARE_BENCHMARK_OP (eddsa_ecdh);
  107. GNUNET_DECLARE_BENCHMARK_OP (eddsa_key_create);
  108. GNUNET_DECLARE_BENCHMARK_OP (eddsa_key_get_public);
  109. GNUNET_DECLARE_BENCHMARK_OP (eddsa_sign);
  110. GNUNET_DECLARE_BENCHMARK_OP (eddsa_verify);
  111. GNUNET_DECLARE_BENCHMARK_OP (hash);
  112. GNUNET_DECLARE_BENCHMARK_OP (hash_context_finish);
  113. GNUNET_DECLARE_BENCHMARK_OP (hash_context_read);
  114. GNUNET_DECLARE_BENCHMARK_OP (hash_context_start);
  115. GNUNET_DECLARE_BENCHMARK_OP (hkdf);
  116. GNUNET_DECLARE_BENCHMARK_OP (rsa_blind);
  117. GNUNET_DECLARE_BENCHMARK_OP (rsa_private_key_create);
  118. GNUNET_DECLARE_BENCHMARK_OP (rsa_private_key_get_public);
  119. GNUNET_DECLARE_BENCHMARK_OP (rsa_sign_blinded);
  120. GNUNET_DECLARE_BENCHMARK_OP (rsa_unblind);
  121. GNUNET_DECLARE_BENCHMARK_OP (rsa_verify);
  122. struct UrlRequestData *urd;
  123. unsigned int urd_len;
  124. unsigned int urd_capacity;
  125. };
  126. #undef GNUNET_DECLARE_BENCHMARK_OP
  127. /**
  128. * Acquire the benchmark data for the current thread, allocate if necessary.
  129. * Installs handler to collect the benchmark data on thread termination.
  130. *
  131. * @return benchmark data for the current thread
  132. */
  133. struct BenchmarkData *
  134. get_benchmark_data (void);
  135. /**
  136. * Get benchmark data for a URL. If the URL is too long, it's truncated
  137. * before looking up the correspoding benchmark data.
  138. *
  139. * Statistics are bucketed by URL and status code.
  140. *
  141. * @param url url to get request data for
  142. * @param status http status code
  143. */
  144. struct UrlRequestData *
  145. get_url_benchmark_data (char *url, unsigned int status);
  146. #endif /* BENCHMARK_H_ */