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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial