Skip to content

Commit cf05192

Browse files
jogmenhorman
authored andcommitted
writeread: run threads for a constant time
Fixes: openssl/project#1261 Signed-off-by: Norbert Pocs <[email protected]> Reviewed-by: Saša Nedvědický <[email protected]> Reviewed-by: Neil Horman <[email protected]> (Merged from #32)
1 parent 24f0557 commit cf05192

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed

source/writeread.c

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <openssl/ssl.h>
2222
#include "perflib/perflib.h"
2323

24-
#define NUM_CALLS_PER_TEST 1000000
24+
#define RUN_TIME 5
2525

2626
int err = 0;
2727

@@ -31,19 +31,18 @@ static char *cert = NULL;
3131
static char *privkey = NULL;
3232
static const SSL_METHOD *smethod, *cmethod;
3333

34-
OSSL_TIME *times;
35-
3634
static int threadcount;
37-
size_t num_calls;
3835
int buf_size = 1024;
3936

37+
size_t *counts;
38+
OSSL_TIME max_time;
39+
4040
static void do_writeread(size_t num)
4141
{
4242
SSL *clientssl = NULL, *serverssl = NULL;
4343
SSL_CTX *lsctx = NULL, *lcctx = NULL;
4444
int ret = 1;
45-
size_t i;
46-
OSSL_TIME start, end;
45+
OSSL_TIME time;
4746
char *sbuf, *cbuf;
4847

4948
/* Prepare client and server buffers. */
@@ -73,9 +72,7 @@ static void do_writeread(size_t num)
7372
return;
7473
}
7574

76-
start = ossl_time_now();
77-
78-
for (i = 0; i < num_calls / threadcount; i++) {
75+
do {
7976
size_t written = 0;
8077
if (SSL_write_ex(clientssl, cbuf, buf_size, &written) <= 0) {
8178
fprintf(stderr, "Failed to write data\n");
@@ -93,10 +90,9 @@ static void do_writeread(size_t num)
9390
err = 1;
9491
return;
9592
}
96-
}
97-
98-
end = ossl_time_now();
99-
times[num] = ossl_time_subtract(end, start);
93+
counts[num]++;
94+
time = ossl_time_now();
95+
} while (time.t < max_time.t);
10096

10197
perflib_shutdown_ssl_connection(serverssl, clientssl);
10298
if (share_ctx == 0) {
@@ -109,7 +105,8 @@ static void do_writeread(size_t num)
109105

110106
int main(int argc, char * const argv[])
111107
{
112-
OSSL_TIME duration, ttime;
108+
OSSL_TIME duration;
109+
size_t total_count = 0;
113110
double avcalltime;
114111
int ret = EXIT_FAILURE;
115112
int i;
@@ -171,15 +168,14 @@ int main(int argc, char * const argv[])
171168
fprintf(stderr, "threadcount must be > 0\n");
172169
goto err;
173170
}
174-
times = OPENSSL_malloc(sizeof(OSSL_TIME) * threadcount);
175-
if (times == NULL) {
176-
fprintf(stderr, "Failed to create times array\n");
171+
172+
counts = OPENSSL_malloc(sizeof(size_t) * threadcount);
173+
if (counts == NULL) {
174+
fprintf(stderr, "Failed to create counts array\n");
177175
goto err;
178176
}
179177

180-
num_calls = NUM_CALLS_PER_TEST;
181-
if (NUM_CALLS_PER_TEST % threadcount > 0) /* round up */
182-
num_calls += threadcount - NUM_CALLS_PER_TEST % threadcount;
178+
max_time = ossl_time_add(ossl_time_now(), ossl_seconds2time(RUN_TIME));
183179

184180
if (share_ctx == 1) {
185181
if (!perflib_create_ssl_ctx_pair(smethod, cmethod, 0, 0,
@@ -199,11 +195,10 @@ int main(int argc, char * const argv[])
199195
goto err;
200196
}
201197

202-
ttime = times[0];
203-
for (i = 1; i < threadcount; i++)
204-
ttime = ossl_time_add(ttime, times[i]);
198+
for (i = 0; i < threadcount; i++)
199+
total_count += counts[i];
205200

206-
avcalltime = ((double)ossl_time2ticks(ttime) / num_calls) / (double)OSSL_TIME_US;
201+
avcalltime = (double)RUN_TIME * 1e6 * threadcount / total_count;
207202

208203
if (terse) {
209204
printf("%lf\n", avcalltime);
@@ -215,7 +210,7 @@ int main(int argc, char * const argv[])
215210
err:
216211
OPENSSL_free(cert);
217212
OPENSSL_free(privkey);
218-
OPENSSL_free(times);
213+
OPENSSL_free(counts);
219214
if (share_ctx == 1) {
220215
SSL_CTX_free(sctx);
221216
SSL_CTX_free(cctx);

0 commit comments

Comments
 (0)