Skip to content

Commit b84edfe

Browse files
kroeckxt8m
authored andcommitted
Let all threads run for the same amount of time
Reviewed-by: Neil Horman <[email protected]> Reviewed-by: Tomas Mraz <[email protected]> (Merged from #30)
1 parent f297ba6 commit b84edfe

File tree

1 file changed

+34
-38
lines changed

1 file changed

+34
-38
lines changed

source/handshake.c

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
# include <windows.h>
1919
# include "perflib/getopt.h"
2020
# include "perflib/basename.h"
21-
#endif /* _WIN32 */
21+
#endif /* _WIN32 */
2222
#include <openssl/ssl.h>
2323
#include "perflib/perflib.h"
2424

25-
#define NUM_CALLS_PER_TEST 10000
25+
#define RUN_TIME 5
2626

2727
int err = 0;
2828

@@ -33,7 +33,7 @@ static char *privkey = NULL;
3333
static OSSL_LIB_CTX **libctx_pool = NULL;
3434

3535

36-
OSSL_TIME *times;
36+
size_t *counts;
3737

3838
static int threadcount;
3939
size_t num_calls;
@@ -44,13 +44,14 @@ typedef enum {
4444
TC_OSSL_LIB_CTX_PER_THREAD,
4545
TC_OSSL_LIB_CTX_POOL,
4646
} test_case_t;
47+
OSSL_TIME max_time;
4748

4849
static void do_handshake(size_t num)
4950
{
5051
SSL *clientssl = NULL, *serverssl = NULL;
5152
int ret = 1;
5253
size_t i;
53-
OSSL_TIME start, end;
54+
OSSL_TIME time;
5455
SSL_CTX *lsctx = NULL;
5556
SSL_CTX *lcctx = NULL;
5657

@@ -59,9 +60,9 @@ static void do_handshake(size_t num)
5960
lcctx = cctx;
6061
}
6162

62-
start = ossl_time_now();
63+
counts[num] = 0;
6364

64-
for (i = 0; i < num_calls / threadcount; i++) {
65+
do {
6566
if (share_ctx == 0) {
6667
if (!perflib_create_ssl_ctx_pair(TLS_server_method(),
6768
TLS_client_method(),
@@ -83,10 +84,9 @@ static void do_handshake(size_t num)
8384
SSL_CTX_free(lcctx);
8485
lsctx = lcctx = NULL;
8586
}
86-
}
87-
88-
end = ossl_time_now();
89-
times[num] = ossl_time_subtract(end, start);
87+
counts[num]++;
88+
time = ossl_time_now();
89+
} while (time.t < max_time.t);
9090

9191
if (!ret)
9292
err = 1;
@@ -97,21 +97,21 @@ static void do_handshake_ossl_lib_ctx_per_thread(size_t num)
9797
SSL *clientssl = NULL, *serverssl = NULL;
9898
int ret = 1;
9999
size_t i;
100-
OSSL_TIME start, end;
100+
OSSL_TIME time;
101101
OSSL_LIB_CTX *libctx = NULL;
102102
SSL_CTX *lsctx = NULL;
103103
SSL_CTX *lcctx = NULL;
104104

105-
start = ossl_time_now();
106-
107105
libctx = OSSL_LIB_CTX_new();
108106
if (libctx == NULL) {
109107
fprintf(stderr, "%s:%d: Failed to create ossl lib context\n", __FILE__, __LINE__);
110108
err = 1;
111109
return;
112110
}
113111

114-
for (i = 0; i < num_calls / threadcount; i++) {
112+
counts[num] = 0;
113+
114+
do {
115115
if (!perflib_create_ossl_lib_ctx_pair(libctx,
116116
TLS_server_method(),
117117
TLS_client_method(),
@@ -132,10 +132,9 @@ static void do_handshake_ossl_lib_ctx_per_thread(size_t num)
132132
SSL_CTX_free(lsctx);
133133
SSL_CTX_free(lcctx);
134134
lsctx = lcctx = NULL;
135-
}
136-
137-
end = ossl_time_now();
138-
times[num] = ossl_time_subtract(end, start);
135+
counts[num]++;
136+
time = ossl_time_now();
137+
} while (time.t < max_time.t);
139138

140139
if (!ret)
141140
err = 1;
@@ -148,13 +147,11 @@ static void do_handshake_ossl_lib_ctx_pool(size_t num)
148147
SSL *clientssl = NULL, *serverssl = NULL;
149148
int ret = 1;
150149
size_t i;
151-
OSSL_TIME start, end;
150+
OSSL_TIME time;
152151
OSSL_LIB_CTX *libctx = NULL;
153152
SSL_CTX *lsctx = NULL;
154153
SSL_CTX *lcctx = NULL;
155154

156-
start = ossl_time_now();
157-
158155
libctx = libctx_pool[num % ossl_lib_ctx_pool_size];
159156
if (share_ctx == 1) {
160157
if (!perflib_create_ossl_lib_ctx_pair(libctx,
@@ -168,7 +165,9 @@ static void do_handshake_ossl_lib_ctx_pool(size_t num)
168165
}
169166
}
170167

171-
for (i = 0; i < num_calls / threadcount; ++i) {
168+
counts[num] = 0;
169+
170+
do {
172171
if (share_ctx == 0) {
173172
if (!perflib_create_ossl_lib_ctx_pair(libctx,
174173
TLS_server_method(),
@@ -192,16 +191,16 @@ static void do_handshake_ossl_lib_ctx_pool(size_t num)
192191
SSL_CTX_free(lcctx);
193192
lsctx = lcctx = NULL;
194193
}
194+
counts[num]++;
195+
time = ossl_time_now();
195196
}
197+
while (time.t < max_time.t);
196198

197199
if (share_ctx == 1) {
198200
SSL_CTX_free(lsctx);
199201
SSL_CTX_free(lcctx);
200202
}
201203

202-
end = ossl_time_now();
203-
times[num] = ossl_time_subtract(end, start);
204-
205204
if (!ret)
206205
err = 1;
207206
}
@@ -263,7 +262,8 @@ void usage(const char *progname)
263262
int main(int argc, char * const argv[])
264263
{
265264
double persec;
266-
OSSL_TIME duration, ttime;
265+
OSSL_TIME duration;
266+
size_t total_count = 0;
267267
double avcalltime;
268268
int ret = EXIT_FAILURE;
269269
int i;
@@ -342,15 +342,13 @@ int main(int argc, char * const argv[])
342342
printf("threadcount must be > 0\n");
343343
goto err;
344344
}
345-
times = OPENSSL_malloc(sizeof(OSSL_TIME) * threadcount);
346-
if (times == NULL) {
345+
counts = OPENSSL_malloc(sizeof(size_t) * threadcount);
346+
if (counts == NULL) {
347347
printf("Failed to create times array\n");
348348
goto err;
349349
}
350350

351-
num_calls = NUM_CALLS_PER_TEST;
352-
if (NUM_CALLS_PER_TEST % threadcount > 0) /* round up */
353-
num_calls += threadcount - NUM_CALLS_PER_TEST % threadcount;
351+
max_time = ossl_time_add(ossl_time_now(), ossl_seconds2time(RUN_TIME));
354352

355353
switch (test_case) {
356354
case TC_SSL_CTX: {
@@ -396,13 +394,11 @@ int main(int argc, char * const argv[])
396394
goto err;
397395
}
398396

399-
ttime = times[0];
400-
for (i = 1; i < threadcount; i++)
401-
ttime = ossl_time_add(ttime, times[i]);
397+
for (i = 0; i < threadcount; i++)
398+
total_count += counts[i];
402399

403-
avcalltime = ((double)ossl_time2ticks(ttime) / num_calls) / (double)OSSL_TIME_US;
404-
persec = ((num_calls * OSSL_TIME_SECOND)
405-
/ (double)ossl_time2ticks(duration));
400+
avcalltime = (double)RUN_TIME * 1e6 * threadcount / total_count;
401+
persec = (double)total_count / RUN_TIME;
406402

407403
if (terse) {
408404
printf("%lf\n", avcalltime);
@@ -415,7 +411,7 @@ int main(int argc, char * const argv[])
415411
err:
416412
OPENSSL_free(cert);
417413
OPENSSL_free(privkey);
418-
OPENSSL_free(times);
414+
OPENSSL_free(counts);
419415
if (share_ctx == 1) {
420416
SSL_CTX_free(sctx);
421417
SSL_CTX_free(cctx);

0 commit comments

Comments
 (0)