Skip to content

Commit 7388d62

Browse files
andrewkdinhnhorman
authored andcommitted
Set a constant amount of runtime on more perf tests
Update some more perf tests to run for a set amount of time, similar to #30. This PR resolves the first half of the issues in the epic openssl/project#1251 Fixes openssl/project#1252 Fixes openssl/project#1253 Fixes openssl/project#1254 Fixes openssl/project#1255 Fixes openssl/project#1256 Fixes openssl/project#1257 Reviewed-by: Tomas Mraz <[email protected]> Reviewed-by: Saša Nedvědický <[email protected]> Reviewed-by: Neil Horman <[email protected]> (Merged from #33)
1 parent df81d18 commit 7388d62

File tree

6 files changed

+116
-132
lines changed

6 files changed

+116
-132
lines changed

source/evp_fetch.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
# define OPENSSL_DO_PQ
3232
#endif
3333

34-
#define NUM_CALLS_PER_TEST 10000000
34+
#define RUN_TIME 5
3535

3636
/*
3737
* Update the constant numbers below if you add or remove
@@ -58,7 +58,8 @@
5858
#define FETCH_ENTRY_PQ_ALGS_N \
5959
(FETCH_ENTRY_ML_KEM_N + FETCH_ENTRY_ML_DSA_N + FETCH_ENTRY_SLH_DSA_N)
6060

61-
OSSL_TIME *times;
61+
size_t *counts;
62+
OSSL_TIME max_time;
6263

6364
int err = 0;
6465
int pq = 0;
@@ -158,7 +159,7 @@ static struct fetch_data_entry fetch_entries[] = {
158159

159160
void do_fetch(size_t num)
160161
{
161-
OSSL_TIME start, end;
162+
OSSL_TIME time;
162163
size_t i, j;
163164
const char *fetch_alg = NULL;
164165
int array_size = ARRAY_SIZE(fetch_entries);
@@ -171,7 +172,7 @@ void do_fetch(size_t num)
171172
array_size -= FETCH_ENTRY_PQ_ALGS_N;
172173
}
173174

174-
start = ossl_time_now();
175+
counts[num] = 0;
175176

176177
/*
177178
* Going through the fetch entries num_calls / threadcount times.
@@ -180,7 +181,7 @@ void do_fetch(size_t num)
180181
* to be a multiple of the number of fetch entries therefore at the last
181182
* iteration we may not check all the algorithms.
182183
*/
183-
for (i = 0; i < num_calls / threadcount; i++) {
184+
do {
184185
/*
185186
* If we set a fetch type, always use that
186187
*/
@@ -278,15 +279,16 @@ void do_fetch(size_t num)
278279
err = 1;
279280
return;
280281
}
282+
counts[num]++;
283+
time = ossl_time_now();
281284
}
282-
end = ossl_time_now();
283-
times[num] = ossl_time_subtract(end, start);
285+
while (time.t < max_time.t);
284286
}
285287

286288
int main(int argc, char *argv[])
287289
{
288290
OSSL_TIME duration;
289-
OSSL_TIME ttime;
291+
size_t total_count = 0;
290292
double av;
291293
int terse = 0;
292294
size_t i;
@@ -352,17 +354,16 @@ int main(int argc, char *argv[])
352354
printf("threadcount must be > 0\n");
353355
return EXIT_FAILURE;
354356
}
355-
num_calls = NUM_CALLS_PER_TEST;
356-
if (NUM_CALLS_PER_TEST % threadcount > 0) /* round up */
357-
num_calls += threadcount - NUM_CALLS_PER_TEST % threadcount;
357+
358+
max_time = ossl_time_add(ossl_time_now(), ossl_seconds2time(RUN_TIME));
358359

359360
ctx = OSSL_LIB_CTX_new();
360361
if (ctx == NULL)
361362
return EXIT_FAILURE;
362363

363-
times = OPENSSL_malloc(sizeof(OSSL_TIME) * threadcount);
364-
if (times == NULL) {
365-
printf("Failed to create times array\n");
364+
counts = OPENSSL_malloc(sizeof(size_t) * threadcount);
365+
if (counts == NULL) {
366+
printf("Failed to create counts array\n");
366367
return EXIT_FAILURE;
367368
}
368369

@@ -376,9 +377,8 @@ int main(int argc, char *argv[])
376377
goto out;
377378
}
378379

379-
ttime = times[0];
380-
for (i = 1; i < threadcount; i++)
381-
ttime = ossl_time_add(ttime, times[i]);
380+
for (i = 0; i < threadcount; i++)
381+
total_count += counts[i];
382382

383383
/*
384384
* EVP_fetch_* calls are pretty fast, running in
@@ -388,7 +388,7 @@ int main(int argc, char *argv[])
388388
* zero in the math. Instead, manually do the division, casting
389389
* our values as doubles so that we compute the proper time
390390
*/
391-
av = ((double)ossl_time2ticks(ttime) / num_calls) /(double)OSSL_TIME_US;
391+
av = (double)RUN_TIME * 1e6 * threadcount / total_count;
392392

393393
if (terse)
394394
printf("%lf\n", av);
@@ -398,6 +398,6 @@ int main(int argc, char *argv[])
398398
rc = EXIT_SUCCESS;
399399
out:
400400
OSSL_LIB_CTX_free(ctx);
401-
OPENSSL_free(times);
401+
OPENSSL_free(counts);
402402
return rc;
403403
}

source/evp_setpeer.c

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,21 @@
2222
/* run 'make regen_key_samples' if header file is missing */
2323
#include "keys_setpeer.h"
2424

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

2727
int err = 0;
2828

2929
size_t num_calls;
3030
static int threadcount;
31-
static OSSL_TIME *times = NULL;
31+
size_t *counts;
3232

3333
EVP_PKEY *pkey = NULL;
34+
OSSL_TIME max_time;
3435

3536
void do_setpeer(size_t num)
3637
{
3738
size_t i;
38-
OSSL_TIME start, end;
39+
OSSL_TIME time;
3940

4041
EVP_PKEY_CTX *pkey_ctx = NULL;
4142

@@ -53,17 +54,16 @@ void do_setpeer(size_t num)
5354
return;
5455
}
5556

56-
start = ossl_time_now();
57+
counts[num] = 0;
5758

58-
for (i = 0; i < num_calls / threadcount; i++) {
59+
do {
5960
if (EVP_PKEY_derive_set_peer(pkey_ctx, pkey) <= 0) {
6061
err = 1;
6162
break;
6263
}
63-
}
64-
65-
end = ossl_time_now();
66-
times[num] = ossl_time_subtract(end, start);
64+
counts[num]++;
65+
time = ossl_time_now();
66+
} while (time.t < max_time.t);
6767

6868
EVP_PKEY_CTX_free(pkey_ctx);
6969
}
@@ -84,15 +84,13 @@ static int sample_name_to_id(const char *sample_name)
8484
static double get_avcalltime(void)
8585
{
8686
int i;
87-
OSSL_TIME t;
87+
size_t total_count = 0;
8888
double avcalltime;
8989

90-
memset(&t, 0, sizeof(t));
9190
for (i = 0; i < threadcount; i++)
92-
t = ossl_time_add(t, times[i]);
93-
avcalltime = (double)ossl_time2ticks(t) / num_calls;
91+
total_count += counts[i];
9492

95-
avcalltime = avcalltime / (double)OSSL_TIME_US;
93+
avcalltime = (double)RUN_TIME * 1e6 * threadcount / total_count;
9694

9795
return avcalltime;
9896
}
@@ -132,7 +130,7 @@ static void usage(char * const argv[])
132130
int main(int argc, char *argv[])
133131
{
134132
OSSL_TIME duration;
135-
OSSL_TIME ttime;
133+
size_t total_count = 0;
136134
double avcalltime;
137135
int terse = 0;
138136
int rc = EXIT_FAILURE;
@@ -181,13 +179,11 @@ int main(int argc, char *argv[])
181179
return EXIT_FAILURE;
182180
}
183181

184-
num_calls = NUM_CALLS_PER_TEST;
185-
if (NUM_CALLS_PER_TEST % threadcount > 0) /* round up */
186-
num_calls += threadcount - NUM_CALLS_PER_TEST % threadcount;
182+
max_time = ossl_time_add(ossl_time_now(), ossl_seconds2time(RUN_TIME));
187183

188-
times = OPENSSL_malloc(sizeof(OSSL_TIME) * threadcount);
189-
if (times == NULL) {
190-
printf("Failed to create times array\n");
184+
counts = OPENSSL_malloc(sizeof(OSSL_TIME) * threadcount);
185+
if (counts == NULL) {
186+
printf("Failed to create counts array\n");
191187
return EXIT_FAILURE;
192188
}
193189

@@ -241,6 +237,6 @@ int main(int argc, char *argv[])
241237

242238
rc = EXIT_SUCCESS;
243239
out:
244-
OPENSSL_free(times);
240+
OPENSSL_free(counts);
245241
return rc;
246242
}

source/newrawkey.c

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
#include <openssl/evp.h>
2222
#include "perflib/perflib.h"
2323

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

2626
size_t num_calls;
27-
OSSL_TIME *times;
27+
size_t *counts;
28+
OSSL_TIME max_time;
2829

2930
enum {
3031
ALGO_X25519,
@@ -336,7 +337,7 @@ void do_newrawkey(size_t num)
336337
{
337338
size_t i;
338339
EVP_PKEY *pkey;
339-
OSSL_TIME start, end;
340+
OSSL_TIME time;
340341
const unsigned char *key_data = key_x25519;
341342
size_t key_len = sizeof(key_x25519);
342343

@@ -362,9 +363,9 @@ void do_newrawkey(size_t num)
362363
break;
363364
}
364365

365-
start = ossl_time_now();
366+
counts[num] = 0;
366367

367-
for (i = 0; i < num_calls / threadcount; i++) {
368+
do {
368369
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
369370
pkey = EVP_PKEY_new_raw_public_key_ex(NULL, alg_name, NULL, key_data,
370371
key_len);
@@ -379,16 +380,15 @@ void do_newrawkey(size_t num)
379380
err = 1;
380381
else
381382
EVP_PKEY_free(pkey);
382-
}
383-
384-
end = ossl_time_now();
385-
times[num] = ossl_time_subtract(end, start);
383+
counts[num]++;
384+
time = ossl_time_now();
385+
} while (time.t < max_time.t);
386386
}
387387

388388
int main(int argc, char *argv[])
389389
{
390390
OSSL_TIME duration;
391-
OSSL_TIME ttime;
391+
size_t total_count = 0;
392392
double av;
393393
int terse = 0;
394394
size_t i;
@@ -438,13 +438,11 @@ int main(int argc, char *argv[])
438438
printf("threadcount must be > 0\n");
439439
return EXIT_FAILURE;
440440
}
441-
num_calls = NUM_CALLS_PER_TEST;
442-
if (NUM_CALLS_PER_TEST % threadcount > 0) /* round up */
443-
num_calls += threadcount - NUM_CALLS_PER_TEST % threadcount;
441+
max_time = ossl_time_add(ossl_time_now(), ossl_seconds2time(RUN_TIME));
444442

445-
times = OPENSSL_malloc(sizeof(OSSL_TIME) * threadcount);
446-
if (times == NULL) {
447-
printf("Failed to create times array\n");
443+
counts = OPENSSL_malloc(sizeof(size_t) * threadcount);
444+
if (counts == NULL) {
445+
printf("Failed to create counts array\n");
448446
return EXIT_FAILURE;
449447
}
450448

@@ -458,9 +456,8 @@ int main(int argc, char *argv[])
458456
goto out;
459457
}
460458

461-
ttime = times[0];
462-
for (i = 1; i < threadcount; i++)
463-
ttime = ossl_time_add(ttime, times[i]);
459+
for (i = 0; i < threadcount; i++)
460+
total_count += counts[i];
464461

465462
/*
466463
* EVP_PKEY_new_raw_public_key is pretty fast, running in
@@ -470,7 +467,7 @@ int main(int argc, char *argv[])
470467
* zero in the math. Instead, manually do the division, casting
471468
* our values as doubles so that we compute the proper time
472469
*/
473-
av = ((double)ossl_time2ticks(ttime) / num_calls) /(double)OSSL_TIME_US;
470+
av = (double)RUN_TIME * 1e6 * threadcount/ total_count;
474471

475472
if (terse)
476473
printf("%lf\n", av);
@@ -480,6 +477,6 @@ int main(int argc, char *argv[])
480477

481478
rc = EXIT_SUCCESS;
482479
out:
483-
OPENSSL_free(times);
480+
OPENSSL_free(counts);
484481
return rc;
485482
}

0 commit comments

Comments
 (0)