2 * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
13 #include <openssl/ssl.h>
14 #include "perflib/perflib.h"
16 #define NUM_HANDSHAKES_PER_THREAD 1000
20 static SSL_CTX *sctx = NULL, *cctx = NULL;
24 static void do_handshake(size_t num)
26 SSL *clientssl = NULL, *serverssl = NULL;
31 start = ossl_time_now();
33 for (i = 0; i < NUM_HANDSHAKES_PER_THREAD; i++) {
34 ret = perflib_create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
36 ret &= perflib_create_ssl_connection(serverssl, clientssl,
38 perflib_shutdown_ssl_connection(serverssl, clientssl);
39 serverssl = clientssl = NULL;
42 end = ossl_time_now();
43 times[num] = ossl_time_subtract(end, start);
49 int main(int argc, char *argv[])
53 OSSL_TIME duration, av;
58 int ret = EXIT_FAILURE;
63 if ((argc != 3 && argc != 4)
64 || (argc == 4 && strcmp("--terse", argv[1]) != 0)) {
65 printf("Usage: handshake [--terse] certsdir threadcount\n");
76 cert = perflib_mk_file_path(argv[argnext], "servercert.pem");
77 privkey = perflib_mk_file_path(argv[argnext], "serverkey.pem");
78 if (cert == NULL || privkey == NULL) {
79 printf("Failed to allocate cert/privkey\n");
83 threadcount = atoi(argv[++argnext]);
84 if (threadcount < 1) {
85 printf("threadcount must be > 0\n");
89 times = OPENSSL_malloc(sizeof(OSSL_TIME) * threadcount);
91 printf("Failed to create times array\n");
95 if (!perflib_create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
96 0, 0, &sctx, &cctx, cert, privkey)) {
97 printf("Failed to create SSL_CTX pair\n");
101 if (!perflib_run_multi_thread_test(do_handshake, threadcount, &duration)) {
102 printf("Failed to run the test\n");
107 printf("Error during test\n");
112 for (i = 1; i < threadcount; i++)
113 av = ossl_time_add(av, times[i]);
114 av = ossl_time_divide(av, NUM_HANDSHAKES_PER_THREAD * threadcount);
116 persec = ((NUM_HANDSHAKES_PER_THREAD * threadcount * OSSL_TIME_SECOND)
117 / (double)ossl_time2ticks(duration));
120 printf("%ld\n", ossl_time2us(av));
121 printf("%lf\n", persec);
123 printf("Average time per handshake: %ldus\n", ossl_time2us(av));
124 printf("Handshakes per second: %lf\n", persec);
130 OPENSSL_free(privkey);