1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/cmd/libpkix/perf/nss_threads.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,164 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 +/* 1.8 + * nssThreads.c 1.9 + * 1.10 + * NSS Performance Evaluation application (multi-threaded) 1.11 + * 1.12 + */ 1.13 + 1.14 +#include <stdio.h> 1.15 +#include <string.h> 1.16 + 1.17 +#include "secutil.h" 1.18 + 1.19 +#include "nspr.h" 1.20 +#include "prtypes.h" 1.21 +#include "prtime.h" 1.22 +#include "prlong.h" 1.23 + 1.24 +#include "pk11func.h" 1.25 +#include "secasn1.h" 1.26 +#include "cert.h" 1.27 +#include "cryptohi.h" 1.28 +#include "secoid.h" 1.29 +#include "certdb.h" 1.30 +#include "nss.h" 1.31 + 1.32 +typedef struct ThreadDataStr tData; 1.33 + 1.34 +struct ThreadDataStr { 1.35 + CERTCertificate* cert; 1.36 + PRIntervalTime duration; 1.37 + PRUint32 iterations; 1.38 +}; 1.39 + 1.40 +static void ThreadEntry(void* data) 1.41 +{ 1.42 + tData* tdata = (tData*) data; 1.43 + PRIntervalTime duration = tdata->duration; 1.44 + PRTime now = PR_Now(); 1.45 + PRIntervalTime start = PR_IntervalNow(); 1.46 + 1.47 + PR_ASSERT(duration); 1.48 + if (!duration) 1.49 + { 1.50 + return; 1.51 + } 1.52 + do { 1.53 + SECStatus rv = CERT_VerifyCertificate 1.54 + (CERT_GetDefaultCertDB(), 1.55 + tdata->cert, 1.56 + PR_TRUE, 1.57 + certificateUsageEmailSigner, 1.58 + now, 1.59 + NULL, 1.60 + NULL, 1.61 + NULL); 1.62 + if (rv != SECSuccess) 1.63 + { 1.64 + (void) fprintf(stderr, "Validation failed.\n"); 1.65 + PORT_Assert(0); 1.66 + return; 1.67 + } 1.68 + tdata->iterations ++; 1.69 + } while ((PR_IntervalNow() - start) < duration); 1.70 +} 1.71 + 1.72 +static void Test(CERTCertificate* cert, PRIntervalTime duration, PRUint32 threads) 1.73 +{ 1.74 + tData data; 1.75 + tData** alldata; 1.76 + PRIntervalTime starttime, endtime, elapsed; 1.77 + PRUint32 msecs; 1.78 + float total = 0; 1.79 + PRThread** pthreads = NULL; 1.80 + PRUint32 i = 0; 1.81 + 1.82 + data.duration = duration; 1.83 + data.cert = cert; 1.84 + data.iterations = 0; 1.85 + 1.86 + starttime = PR_IntervalNow(); 1.87 + pthreads = (PRThread**)PR_Malloc(threads*sizeof (PRThread*)); 1.88 + alldata = (tData**)PR_Malloc(threads*sizeof (tData*)); 1.89 + for (i = 0; i < threads; i++) 1.90 + { 1.91 + alldata[i] = (tData*)PR_Malloc(sizeof (tData)); 1.92 + *alldata[i] = data; 1.93 + pthreads[i] = 1.94 + PR_CreateThread(PR_USER_THREAD, 1.95 + ThreadEntry, 1.96 + (void*) alldata[i], 1.97 + PR_PRIORITY_NORMAL, 1.98 + PR_GLOBAL_THREAD, 1.99 + PR_JOINABLE_THREAD, 1.100 + 0); 1.101 + 1.102 + } 1.103 + for (i = 0; i < threads; i++) 1.104 + { 1.105 + tData* args = alldata[i]; 1.106 + PR_JoinThread(pthreads[i]); 1.107 + total += args->iterations; 1.108 + PR_Free((void*)args); 1.109 + } 1.110 + PR_Free((void*) pthreads); 1.111 + PR_Free((void*) alldata); 1.112 + endtime = PR_IntervalNow(); 1.113 + 1.114 + endtime = PR_IntervalNow(); 1.115 + elapsed = endtime - starttime; 1.116 + msecs = PR_IntervalToMilliseconds(elapsed); 1.117 + total /= msecs; 1.118 + total *= 1000; 1.119 + (void) fprintf(stdout, "%f operations per second.\n", total); 1.120 +} 1.121 + 1.122 + 1.123 +static void finish(char* message, int code) 1.124 +{ 1.125 + (void) printf(message); 1.126 + exit(code); 1.127 +} 1.128 + 1.129 +static void usage(char* progname) 1.130 +{ 1.131 + (void) printf("Usage : %s <duration> <threads> <certnickname>\n\n", 1.132 + progname); 1.133 + finish("", 0); 1.134 +} 1.135 + 1.136 +int nss_threads(int argc, char** argv) 1.137 +{ 1.138 + SECStatus rv = SECSuccess; 1.139 + CERTCertDBHandle *handle = NULL; 1.140 + CERTCertificate* cert = NULL; 1.141 + PRIntervalTime duration = PR_SecondsToInterval(1); 1.142 + PRUint32 threads = 1; 1.143 + if (argc != 4) 1.144 + { 1.145 + usage(argv[0]); 1.146 + } 1.147 + if (atoi(argv[1]) > 0) 1.148 + { 1.149 + duration = PR_SecondsToInterval(atoi(argv[1])); 1.150 + } 1.151 + if (atoi(argv[2]) > 0) 1.152 + { 1.153 + threads = atoi(argv[2]); 1.154 + } 1.155 + 1.156 + handle = CERT_GetDefaultCertDB(); 1.157 + PR_ASSERT(handle); 1.158 + cert = CERT_FindCertByNicknameOrEmailAddr(handle, argv[3]); 1.159 + if (!cert) 1.160 + { 1.161 + finish("Unable to find certificate.\n", 1); 1.162 + } 1.163 + Test(cert, duration, threads); 1.164 + 1.165 + CERT_DestroyCertificate(cert); 1.166 + return (0); 1.167 +}