Skip to content

Commit

Permalink
Various perf tools improvements
Browse files Browse the repository at this point in the history
- make it easier to build against OpenSSL 1.1.1
- adjust the test iterations to even out the test runtimes
- fix bug in evp_fetch test when running with explicit type
  • Loading branch information
t8m committed Jun 10, 2024
1 parent f8f26f9 commit 096f3a8
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 46 deletions.
10 changes: 7 additions & 3 deletions perf/Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
all: randbytes handshake sslnew newrawkey rsasign x509storeissuer providerdoall rwlocks pkeyread evp_fetch
# Build target for OpenSSL 1.1.1 builds
all111: randbytes handshake sslnew newrawkey rsasign x509storeissuer rwlocks pkeyread

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

#-Wl,-rpath,$(TARGET_OSSL_LIBRARY_PATH)
rm -f libperf.a *.o randbytes handshake sslnew newrawkey rsasign x509storeissuer providerdoall rwlocks pkeyread evp_fetch

CPPFLAGS += -I$(TARGET_OSSL_INCLUDE_PATH) -I.
# For second include path, i.e. out of tree build of OpenSSL uncomment this:
# CPPFLAGS += -I$(TARGET_OSSL_INCLUDE_PATH2)
CFLAGS += -pthread
LDFLAGS += -L$(TARGET_OSSL_LIBRARY_PATH) -L.
# For setting RUNPATH on built executables uncomment this:
# LDFLAGS += -Wl,-rpath,$(TARGET_OSSL_LIBRARY_PATH)

libperf.a: perflib/*.c perflib/*.h
$(CC) $(CPPFLAGS) $(CFLAGS) -c perflib/*.c
Expand Down
15 changes: 9 additions & 6 deletions perf/evp_fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
#include <openssl/core_names.h>
#include "perflib/perflib.h"

#define NUM_CALLS_PER_TEST 1000000
#define NUM_CALLS_PER_TEST 10000000

OSSL_TIME *times;

int err = 0;

static int threadcount;
size_t num_calls;

static OSSL_LIB_CTX *ctx = NULL;

Expand Down Expand Up @@ -96,13 +97,14 @@ void do_fetch(size_t num)

start = ossl_time_now();

for (i = 0; i < NUM_CALLS_PER_TEST / threadcount; i++) {
for (i = 0; i < num_calls / threadcount; i++) {
/*
* If we set a fetch type, always use that
*/
if (exclusive_fetch_type == FETCH_END) {
j = i % ARRAY_SIZE(fetch_entries);
fetch_alg = fetch_entries[j].alg;
j = fetch_entries[j].ftype;
} else {
j = exclusive_fetch_type;
fetch_alg = exclusive_fetch_alg;
Expand All @@ -111,7 +113,7 @@ void do_fetch(size_t num)
if (err == 1)
return;

switch (fetch_entries[j].ftype) {
switch (j) {
case FETCH_MD: {
EVP_MD *md = EVP_MD_fetch(ctx, fetch_alg,
fetch_entries[j].propq);
Expand Down Expand Up @@ -180,7 +182,6 @@ int main(int argc, char *argv[])
{
OSSL_TIME duration;
OSSL_TIME ttime;
double real_num_calls;
double av;
int terse = 0;
int argnext;
Expand Down Expand Up @@ -227,6 +228,9 @@ int main(int argc, char *argv[])
printf("threadcount must be > 0\n");
return EXIT_FAILURE;
}
num_calls = NUM_CALLS_PER_TEST;
if (NUM_CALLS_PER_TEST % threadcount > 0) /* round up */
num_calls += threadcount - NUM_CALLS_PER_TEST % threadcount;

ctx = OSSL_LIB_CTX_new();
if (ctx == NULL)
Expand Down Expand Up @@ -260,8 +264,7 @@ int main(int argc, char *argv[])
* zero in the math. Instead, manually do the division, casting
* our values as doubles so that we compute the proper time
*/
real_num_calls = ((double)NUM_CALLS_PER_TEST / threadcount) * threadcount;
av = ((double)ossl_time2ticks(ttime) / real_num_calls) /(double)OSSL_TIME_US;
av = ((double)ossl_time2ticks(ttime) / num_calls) /(double)OSSL_TIME_US;

if (terse)
printf("%lf\n", av);
Expand Down
14 changes: 9 additions & 5 deletions perf/handshake.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <openssl/ssl.h>
#include "perflib/perflib.h"

#define NUM_HANDSHAKES_PER_RUN 100000
#define NUM_CALLS_PER_TEST 10000

int err = 0;

Expand All @@ -22,17 +22,18 @@ static SSL_CTX *sctx = NULL, *cctx = NULL;
OSSL_TIME *times;

static int threadcount;
size_t num_calls;

static void do_handshake(size_t num)
{
SSL *clientssl = NULL, *serverssl = NULL;
int ret = 1;
int i;
size_t i;
OSSL_TIME start, end;

start = ossl_time_now();

for (i = 0; i < NUM_HANDSHAKES_PER_RUN / threadcount; i++) {
for (i = 0; i < num_calls / threadcount; i++) {
ret = perflib_create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
NULL, NULL);
ret &= perflib_create_ssl_connection(serverssl, clientssl,
Expand Down Expand Up @@ -86,6 +87,9 @@ int main(int argc, char *argv[])
printf("threadcount must be > 0\n");
goto err;
}
num_calls = NUM_CALLS_PER_TEST;
if (NUM_CALLS_PER_TEST % threadcount > 0) /* round up */
num_calls += threadcount - NUM_CALLS_PER_TEST % threadcount;

times = OPENSSL_malloc(sizeof(OSSL_TIME) * threadcount);
if (times == NULL) {
Expand Down Expand Up @@ -113,8 +117,8 @@ int main(int argc, char *argv[])
for (i = 1; i < threadcount; i++)
ttime = ossl_time_add(ttime, times[i]);

avcalltime = ((double)ossl_time2ticks(ttime) / (double)NUM_HANDSHAKES_PER_RUN) / (double)OSSL_TIME_US;
persec = ((NUM_HANDSHAKES_PER_RUN * OSSL_TIME_SECOND)
avcalltime = ((double)ossl_time2ticks(ttime) / num_calls) / (double)OSSL_TIME_US;
persec = ((num_calls * OSSL_TIME_SECOND)
/ (double)ossl_time2ticks(duration));

if (terse) {
Expand Down
17 changes: 13 additions & 4 deletions perf/newrawkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
#include <openssl/evp.h>
#include "perflib/perflib.h"

#define NUM_CALLS_PER_TEST 100000
#define NUM_CALLS_PER_TEST 1000000

size_t num_calls;
OSSL_TIME *times;

int err = 0;
Expand All @@ -29,15 +30,20 @@ static int threadcount;

void do_newrawkey(size_t num)
{
int i;
size_t i;
EVP_PKEY *pkey;
OSSL_TIME start, end;

start = ossl_time_now();

for (i = 0; i < NUM_CALLS_PER_TEST / threadcount; i++) {
for (i = 0; i < num_calls / threadcount; i++) {
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
pkey = EVP_PKEY_new_raw_public_key_ex(NULL, "X25519", NULL, buf,
sizeof(buf));
#else
pkey = EVP_PKEY_new_raw_public_key(EVP_PKEY_X25519, NULL, buf,
sizeof(buf));
#endif
if (pkey == NULL)
err = 1;
else
Expand Down Expand Up @@ -76,6 +82,9 @@ int main(int argc, char *argv[])
printf("threadcount must be > 0\n");
return EXIT_FAILURE;
}
num_calls = NUM_CALLS_PER_TEST;
if (NUM_CALLS_PER_TEST % threadcount > 0) /* round up */
num_calls += threadcount - NUM_CALLS_PER_TEST % threadcount;

times = OPENSSL_malloc(sizeof(OSSL_TIME) * threadcount);
if (times == NULL) {
Expand Down Expand Up @@ -105,7 +114,7 @@ int main(int argc, char *argv[])
* zero in the math. Instead, manually do the division, casting
* our values as doubles so that we compute the proper time
*/
av = ((double)ossl_time2ticks(ttime) / (double)NUM_CALLS_PER_TEST) /(double)OSSL_TIME_US;
av = ((double)ossl_time2ticks(ttime) / num_calls) /(double)OSSL_TIME_US;

if (terse)
printf("%lf\n", av);
Expand Down
20 changes: 12 additions & 8 deletions perf/pkeyread.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
/* run 'make regen_key_samples' if header file is missing */
#include "keys.h"

#define NUM_CALLS_PER_TEST 100000
#define NUM_CALLS_PER_TEST 10000

size_t num_calls;
static OSSL_TIME *times = NULL;

int err = 0;
Expand All @@ -36,7 +37,7 @@ static void do_pemread(size_t num)
size_t keydata_sz;
EVP_PKEY *key;
BIO *pem;
int i;
size_t i;
size_t len;
OSSL_TIME start, end;

Expand All @@ -63,7 +64,7 @@ static void do_pemread(size_t num)
* Technically this includes the EVP_PKEY_free() in the timing - but I
* think we can live with that
*/
for (i = 0; i < NUM_CALLS_PER_TEST / threadcount; i++) {
for (i = 0; i < num_calls / threadcount; i++) {
key = PEM_read_bio_PrivateKey(pem, NULL, NULL, NULL);
if (key == NULL) {
fprintf(stderr, "Failed to create key: %d [%s PEM]\n", i,
Expand Down Expand Up @@ -111,13 +112,13 @@ static void do_derread(size_t num)

start = ossl_time_now();

for (i = 0; i < NUM_CALLS_PER_TEST / threadcount && err == 0; i++) {
for (i = 0; i < num_calls / threadcount && err == 0; i++) {
keydata = (const unsigned char *)sample_keys[sample_id][FORMAT_DER];
keydata_sz = sample_key_sizes[sample_id][FORMAT_DER];
pkey = d2i_PrivateKey_ex(sample_id_to_evp(sample_id), NULL,
&keydata, keydata_sz, NULL, NULL);
pkey = d2i_PrivateKey(sample_id_to_evp(sample_id), NULL,
&keydata, keydata_sz);
if (pkey == NULL) {
fprintf(stderr, "%s pkey is NULL [%s BER]\n",
fprintf(stderr, "%s pkey is NULL [%s DER]\n",
__func__, sample_names[sample_id]);
err = 1;
goto error;
Expand Down Expand Up @@ -166,7 +167,7 @@ static double get_avcalltime(void)
memset(&t, 0, sizeof(t));
for (i = 0; i < threadcount; i++)
t = ossl_time_add(t, times[i]);
avcalltime = (double)ossl_time2ticks(t) / (double)NUM_CALLS_PER_TEST;
avcalltime = (double)ossl_time2ticks(t) / num_calls;

avcalltime = avcalltime / (double)OSSL_TIME_US;

Expand Down Expand Up @@ -264,6 +265,9 @@ int main(int argc, char *argv[])
fprintf(stderr, "threadcount must be > 0, use option -t 1\n");
return EXIT_FAILURE;
}
num_calls = NUM_CALLS_PER_TEST;
if (NUM_CALLS_PER_TEST % threadcount > 0) /* round up */
num_calls += threadcount - NUM_CALLS_PER_TEST % threadcount;

times = OPENSSL_malloc(sizeof(OSSL_TIME) * threadcount);
if (times == NULL) {
Expand Down
10 changes: 7 additions & 3 deletions perf/providerdoall.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#define NUM_CALLS_PER_TEST 100000

size_t num_calls;
static int err = 0;
OSSL_TIME *times;

Expand All @@ -32,14 +33,14 @@ static int threadcount;

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

start = ossl_time_now();

for (i = 0; i < NUM_CALLS_PER_TEST / threadcount; i++) {
for (i = 0; i < num_calls / threadcount; i++) {
count = 0;
if (!OSSL_PROVIDER_do_all(NULL, doit, &count) || count != 1) {
err = 1;
Expand Down Expand Up @@ -78,6 +79,9 @@ int main(int argc, char *argv[])
printf("threadcount must be > 0\n");
return EXIT_FAILURE;
}
num_calls = NUM_CALLS_PER_TEST;
if (NUM_CALLS_PER_TEST % threadcount > 0) /* round up */
num_calls += threadcount - NUM_CALLS_PER_TEST % threadcount;

times = OPENSSL_malloc(sizeof(OSSL_TIME) * threadcount);
if (times == NULL) {
Expand All @@ -99,7 +103,7 @@ int main(int argc, char *argv[])
for (i = 1; i < threadcount; i++)
ttime = ossl_time_add(ttime, times[i]);

av = ((double)ossl_time2ticks(ttime) / (double)NUM_CALLS_PER_TEST) / (double)OSSL_TIME_US;
av = ((double)ossl_time2ticks(ttime) / num_calls) / (double)OSSL_TIME_US;

if (terse)
printf("%lf\n", av);
Expand Down
12 changes: 8 additions & 4 deletions perf/randbytes.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
#include <openssl/crypto.h>
#include "perflib/perflib.h"

#define NUM_CALLS_PER_TEST 100000
#define NUM_CALLS_PER_TEST 1000000

size_t num_calls;
OSSL_TIME *times = NULL;

int err = 0;
Expand All @@ -24,13 +25,13 @@ static int threadcount;

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

start = ossl_time_now();

for (i = 0; i < NUM_CALLS_PER_TEST / threadcount; i++)
for (i = 0; i < num_calls / threadcount; i++)
if (!RAND_bytes(buf, sizeof(buf)))
err = 1;

Expand Down Expand Up @@ -66,6 +67,9 @@ int main(int argc, char *argv[])
printf("threadcount must be > 0\n");
return EXIT_FAILURE;
}
num_calls = NUM_CALLS_PER_TEST;
if (NUM_CALLS_PER_TEST % threadcount > 0) /* round up */
num_calls += threadcount - NUM_CALLS_PER_TEST % threadcount;

times = OPENSSL_malloc(sizeof(OSSL_TIME) * threadcount);
if (times == NULL) {
Expand All @@ -87,7 +91,7 @@ int main(int argc, char *argv[])
for (i = 1; i < threadcount; i++)
ttime = ossl_time_add(ttime, times[i]);

avcalltime = ((double)ossl_time2ticks(ttime) / (double)NUM_CALLS_PER_TEST) /(double)OSSL_TIME_US;
avcalltime = ((double)ossl_time2ticks(ttime) / num_calls) /(double)OSSL_TIME_US;

if (terse)
printf("%lf\n", avcalltime);
Expand Down
10 changes: 7 additions & 3 deletions perf/rsasign.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#define NUM_CALLS_PER_TEST 100000

size_t num_calls;
int err = 0;
EVP_PKEY *rsakey = NULL;

Expand All @@ -41,7 +42,7 @@ static OSSL_TIME *times = NULL;

void do_rsasign(size_t num)
{
int i;
size_t i;
unsigned char buf[32];
unsigned char sig[64];
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(rsakey, NULL);
Expand All @@ -50,7 +51,7 @@ void do_rsasign(size_t num)

start = ossl_time_now();

for (i = 0; i < NUM_CALLS_PER_TEST / threadcount; i++) {
for (i = 0; i < num_calls / threadcount; i++) {
if (EVP_PKEY_sign_init(ctx) <= 0
|| EVP_PKEY_sign(ctx, sig, &siglen, tbs, SHA_DIGEST_LENGTH) <= 0) {
err = 1;
Expand Down Expand Up @@ -93,6 +94,9 @@ int main(int argc, char *argv[])
printf("threadcount must be > 0\n");
return EXIT_FAILURE;
}
num_calls = NUM_CALLS_PER_TEST;
if (NUM_CALLS_PER_TEST % threadcount > 0) /* round up */
num_calls += threadcount - NUM_CALLS_PER_TEST % threadcount;

assert(strlen(tbs) == SHA_DIGEST_LENGTH);
membio = BIO_new_mem_buf(rsakeypem, strlen(rsakeypem));
Expand Down Expand Up @@ -127,7 +131,7 @@ int main(int argc, char *argv[])
for (i = 1; i < threadcount; i++)
ttime = ossl_time_add(ttime, times[i]);

avcalltime = ((double)ossl_time2ticks(ttime) / (double)NUM_CALLS_PER_TEST) / (double)OSSL_TIME_US;
avcalltime = ((double)ossl_time2ticks(ttime) / num_calls) / (double)OSSL_TIME_US;

if (terse)
printf("%lf\n", avcalltime);
Expand Down
Loading

0 comments on commit 096f3a8

Please sign in to comment.