security/nss/cmd/libpkix/perf/nss_threads.c

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     4 /*
     5  * nssThreads.c
     6  *
     7  * NSS Performance Evaluation application (multi-threaded)
     8  *
     9  */
    11 #include <stdio.h>
    12 #include <string.h>
    14 #include "secutil.h"
    16 #include "nspr.h"
    17 #include "prtypes.h"
    18 #include "prtime.h"
    19 #include "prlong.h"
    21 #include "pk11func.h"
    22 #include "secasn1.h"
    23 #include "cert.h"
    24 #include "cryptohi.h"
    25 #include "secoid.h"
    26 #include "certdb.h"
    27 #include "nss.h"
    29 typedef struct ThreadDataStr tData;
    31 struct ThreadDataStr {
    32     CERTCertificate* cert;
    33     PRIntervalTime duration;
    34     PRUint32 iterations;
    35 };
    37 static void ThreadEntry(void* data)
    38 {
    39         tData* tdata = (tData*) data;
    40         PRIntervalTime duration = tdata->duration;
    41         PRTime now = PR_Now();
    42         PRIntervalTime start = PR_IntervalNow();
    44         PR_ASSERT(duration);
    45         if (!duration)
    46                 {
    47                         return;
    48                 }
    49         do {
    50                 SECStatus rv = CERT_VerifyCertificate
    51                         (CERT_GetDefaultCertDB(),
    52                         tdata->cert,
    53                         PR_TRUE,
    54                         certificateUsageEmailSigner,
    55                         now,
    56                         NULL,
    57                         NULL,
    58                         NULL);
    59                 if (rv != SECSuccess)
    60                         {
    61                                 (void) fprintf(stderr, "Validation failed.\n");
    62                                 PORT_Assert(0);
    63                                 return;
    64                         }
    65                 tdata->iterations ++;
    66         } while ((PR_IntervalNow() - start) < duration);
    67 }
    69 static void Test(CERTCertificate* cert, PRIntervalTime duration, PRUint32 threads)
    70 {
    71         tData data;
    72         tData** alldata;
    73         PRIntervalTime starttime, endtime, elapsed;
    74         PRUint32 msecs;
    75         float total = 0;
    76         PRThread** pthreads = NULL;
    77         PRUint32 i = 0;
    79         data.duration = duration;
    80         data.cert = cert;
    81         data.iterations = 0;
    83         starttime = PR_IntervalNow();
    84         pthreads = (PRThread**)PR_Malloc(threads*sizeof (PRThread*));
    85         alldata = (tData**)PR_Malloc(threads*sizeof (tData*));
    86         for (i = 0; i < threads; i++)
    87                 {
    88                         alldata[i] = (tData*)PR_Malloc(sizeof (tData));
    89                         *alldata[i] = data;
    90                         pthreads[i] =
    91                                 PR_CreateThread(PR_USER_THREAD,
    92                                                 ThreadEntry,
    93                                                 (void*) alldata[i],
    94                                                 PR_PRIORITY_NORMAL,
    95                                                 PR_GLOBAL_THREAD,
    96                                                 PR_JOINABLE_THREAD,
    97                                                 0);
    99                 }
   100         for (i = 0; i < threads; i++)
   101                 {
   102                         tData* args = alldata[i];
   103                         PR_JoinThread(pthreads[i]);
   104                         total += args->iterations;
   105                         PR_Free((void*)args);
   106                 }
   107         PR_Free((void*) pthreads);
   108         PR_Free((void*) alldata);
   109         endtime = PR_IntervalNow();
   111         endtime = PR_IntervalNow();
   112         elapsed = endtime - starttime;
   113         msecs = PR_IntervalToMilliseconds(elapsed);
   114         total /= msecs;
   115         total *= 1000;
   116         (void) fprintf(stdout, "%f operations per second.\n", total);
   117 }
   120 static void finish(char* message, int code)
   121 {
   122         (void) printf(message);
   123         exit(code);
   124 }
   126 static void usage(char* progname)
   127 {
   128         (void) printf("Usage : %s <duration> <threads> <certnickname>\n\n",
   129                     progname);
   130         finish("", 0);
   131 }
   133 int nss_threads(int argc, char** argv)
   134 {
   135         SECStatus rv = SECSuccess;
   136         CERTCertDBHandle *handle = NULL;
   137         CERTCertificate* cert = NULL;
   138         PRIntervalTime duration = PR_SecondsToInterval(1);
   139         PRUint32 threads = 1;
   140         if (argc != 4)
   141                 {
   142                         usage(argv[0]);
   143                 }
   144         if (atoi(argv[1]) > 0)
   145                 {
   146                         duration = PR_SecondsToInterval(atoi(argv[1]));
   147                 }
   148         if (atoi(argv[2]) > 0)
   149                 {
   150                         threads = atoi(argv[2]);
   151                 }
   153         handle = CERT_GetDefaultCertDB();
   154         PR_ASSERT(handle);
   155         cert = CERT_FindCertByNicknameOrEmailAddr(handle, argv[3]);
   156         if (!cert)
   157                 {
   158                         finish("Unable to find certificate.\n", 1);
   159                 }
   160         Test(cert, duration, threads);
   162         CERT_DestroyCertificate(cert);
   163         return (0);
   164 }

mercurial