Skip to content

Commit

Permalink
Add a performance test for OSSL_PROVIDER_do_all()
Browse files Browse the repository at this point in the history
This tests calls the OSSL_PROVIDER_do_all() function repeatedly in a loop.
This function can be called directly by user code, but is also used during
the initialisation of an SSL_CTX to discover TLS capabilities from
providers (e.g. pluggable groups etc).

The underlying internal function ossl_provider_doall_activated() will
also be tested by this. That function is called during algorithm fetching
(if the algorithms have not yet been cached).

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from #154)
  • Loading branch information
mattcaswell committed May 30, 2023
1 parent 1d04612 commit c32b758
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 2 deletions.
7 changes: 5 additions & 2 deletions perf/Makefile
@@ -1,7 +1,7 @@
all: randbytes handshake sslnew newrawkey rsasign x509storeissuer
all: randbytes handshake sslnew newrawkey rsasign x509storeissuer providerdoall

clean:
rm libperf.a *.o randbytes handshake sslnew newrawkey rsasign x509storeissuer
rm libperf.a *.o randbytes handshake sslnew newrawkey rsasign x509storeissuer providerdoall

libperf.a: perflib/*.c perflib/*.h
gcc -I$(TARGET_OSSL_INCLUDE_PATH) -I. -c perflib/*.c
Expand All @@ -24,3 +24,6 @@ rsasign: rsasign.c libperf.a

x509storeissuer: x509storeissuer.c libperf.a
gcc -L$(TARGET_OSSL_LIBRARY_PATH) -L. -I$(TARGET_OSSL_INCLUDE_PATH) -I. -o x509storeissuer x509storeissuer.c -lperf -lcrypto

providerdoall: providerdoall.c libperf.a
gcc -L$(TARGET_OSSL_LIBRARY_PATH) -L. -I$(TARGET_OSSL_INCLUDE_PATH) -I. -o providerdoall providerdoall.c -lperf -lcrypto
8 changes: 8 additions & 0 deletions perf/README
Expand Up @@ -98,3 +98,11 @@ assumes that the default certificates directly exists but is empty. For a
default configuration this is "/usr/local/ssl/certs". The test takes the number
of threads to use as an argument and the test reports the average time take to
execute a block of 100 X509_STORE_CTX_get1_issuer() calls.

providerdoall
-------------

The providerdoall test repeatedly calls the OSSL_PROVIDER_do_all() function in
blocks of 100 calls, and 100 blocks per thread. The number of threads to use is
provided as an argument and the test reports the average time take to execute a
block of 100 OSSL_PROVIDER_do_all() calls.
114 changes: 114 additions & 0 deletions perf/providerdoall.c
@@ -0,0 +1,114 @@
/*
* Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <openssl/rand.h>
#include <openssl/crypto.h>
#include <openssl/provider.h>
#include "perflib/perflib.h"

#define NUM_CALLS_PER_BLOCK 100
#define NUM_CALL_BLOCKS_PER_THREAD 100
#define NUM_CALLS_PER_THREAD (NUM_CALLS_PER_BLOCK * NUM_CALL_BLOCKS_PER_THREAD)

static int err = 0;
OSSL_TIME *times;

static int doit(OSSL_PROVIDER *provider, void *vcount)
{
int *count = vcount;

(*count)++;
return 1;
}

static void do_providerdoall(size_t num)
{
int i;
unsigned char buf[32];
int count;
OSSL_TIME start, end;

start = ossl_time_now();

for (i = 0; i < NUM_CALLS_PER_THREAD; i++) {
count = 0;
if (!OSSL_PROVIDER_do_all(NULL, doit, &count) || count != 1) {
err = 1;
break;
}
}

end = ossl_time_now();

times[num] = ossl_time_divide(ossl_time_subtract(end, start),
NUM_CALL_BLOCKS_PER_THREAD);
}

int main(int argc, char *argv[])
{
int threadcount, i;
OSSL_TIME duration, av;
int terse = 0;
int argnext;
int ret = EXIT_FAILURE;

if ((argc != 2 && argc != 3)
|| (argc == 3 && strcmp("--terse", argv[1]) != 0)) {
printf("Usage: providerdoall [--terse] threadcount\n");
return EXIT_FAILURE;
}

if (argc == 3) {
terse = 1;
argnext = 2;
} else {
argnext = 1;
}

threadcount = atoi(argv[argnext]);
if (threadcount < 1) {
printf("threadcount must be > 0\n");
return EXIT_FAILURE;
}

times = OPENSSL_malloc(sizeof(OSSL_TIME) * threadcount);
if (times == NULL) {
printf("Failed to create times array\n");
goto err;
}

if (!perflib_run_multi_thread_test(do_providerdoall, threadcount, &duration)) {
printf("Failed to run the test\n");
goto err;
}

if (err) {
printf("Error during test\n");
goto err;
}

av = times[0];
for (i = 1; i < threadcount; i++)
av = ossl_time_add(av, times[i]);
av = ossl_time_divide(av, threadcount);

if (terse)
printf("%ld\n", ossl_time2us(av));
else
printf("Average time per %d OSSL_PROVIDER_do_all() calls: %ldus\n",
NUM_CALLS_PER_BLOCK, ossl_time2us(av));

ret = EXIT_SUCCESS;
err:
OPENSSL_free(times);
return ret;
}

0 comments on commit c32b758

Please sign in to comment.