2222#include <openssl/x509.h>
2323#include "perflib/perflib.h"
2424
25- #define NUM_CALLS_PER_TEST 1000000
25+ #define RUN_TIME 5
2626
2727static int err = 0 ;
2828static X509_STORE * store = NULL ;
2929static X509 * x509 = NULL ;
30- size_t num_calls ;
31- OSSL_TIME * times ;
3230
3331static int threadcount ;
3432
33+ size_t * counts ;
34+ OSSL_TIME max_time ;
35+
3536static void do_x509storeissuer (size_t num )
3637{
37- size_t i ;
3838 X509_STORE_CTX * ctx = X509_STORE_CTX_new ();
3939 X509 * issuer = NULL ;
40- OSSL_TIME start , end ;
40+ OSSL_TIME time ;
4141
4242 if (ctx == NULL || !X509_STORE_CTX_init (ctx , store , x509 , NULL )) {
4343 printf ("Failed to initialise X509_STORE_CTX\n" );
4444 err = 1 ;
4545 goto err ;
4646 }
4747
48- start = ossl_time_now () ;
48+ counts [ num ] = 0 ;
4949
50- for ( i = 0 ; i < num_calls / threadcount ; i ++ ) {
50+ do {
5151 /*
5252 * We actually expect this to fail. We've not configured any
5353 * certificates inside our store. We're just testing calling this
@@ -60,10 +60,9 @@ static void do_x509storeissuer(size_t num)
6060 goto err ;
6161 }
6262 issuer = NULL ;
63- }
64-
65- end = ossl_time_now ();
66- times [num ] = ossl_time_subtract (end , start );
63+ counts [num ]++ ;
64+ time = ossl_time_now ();
65+ } while (time .t < max_time .t );
6766
6867 err :
6968 X509_STORE_CTX_free (ctx );
@@ -72,7 +71,8 @@ static void do_x509storeissuer(size_t num)
7271int main (int argc , char * argv [])
7372{
7473 int i ;
75- OSSL_TIME duration , ttime ;
74+ OSSL_TIME duration ;
75+ size_t total_count = 0 ;
7676 double avcalltime ;
7777 int terse = 0 ;
7878 char * cert = NULL ;
@@ -112,9 +112,6 @@ int main(int argc, char *argv[])
112112 printf ("threadcount must be > 0\n" );
113113 goto err ;
114114 }
115- num_calls = NUM_CALLS_PER_TEST ;
116- if (NUM_CALLS_PER_TEST % threadcount > 0 ) /* round up */
117- num_calls += threadcount - NUM_CALLS_PER_TEST % threadcount ;
118115
119116 store = X509_STORE_new ();
120117 if (store == NULL || !X509_STORE_set_default_paths (store )) {
@@ -135,12 +132,14 @@ int main(int argc, char *argv[])
135132 BIO_free (bio );
136133 bio = NULL ;
137134
138- times = OPENSSL_malloc (sizeof (OSSL_TIME ) * threadcount );
139- if (times == NULL ) {
140- printf ("Failed to create times array\n" );
135+ counts = OPENSSL_malloc (sizeof (size_t ) * threadcount );
136+ if (counts == NULL ) {
137+ printf ("Failed to create counts array\n" );
141138 goto err ;
142139 }
143140
141+ max_time = ossl_time_add (ossl_time_now (), ossl_seconds2time (RUN_TIME ));
142+
144143 if (!perflib_run_multi_thread_test (do_x509storeissuer , threadcount , & duration )) {
145144 printf ("Failed to run the test\n" );
146145 goto err ;
@@ -151,11 +150,10 @@ int main(int argc, char *argv[])
151150 goto err ;
152151 }
153152
154- ttime = times [0 ];
155- for (i = 1 ; i < threadcount ; i ++ )
156- ttime = ossl_time_add (ttime , times [i ]);
153+ for (i = 0 ; i < threadcount ; i ++ )
154+ total_count += counts [i ];
157155
158- avcalltime = (( double )ossl_time2ticks ( ttime ) / num_calls ) /( double ) OSSL_TIME_US ;
156+ avcalltime = (double )RUN_TIME * 1e6 * threadcount / total_count ;
159157
160158 if (terse )
161159 printf ("%lf\n" , avcalltime );
@@ -170,6 +168,6 @@ int main(int argc, char *argv[])
170168 X509_free (x509 );
171169 BIO_free (bio );
172170 OPENSSL_free (cert );
173- OPENSSL_free (times );
171+ OPENSSL_free (counts );
174172 return ret ;
175173}
0 commit comments