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