nss: import at 3.0.1 beta 1
[mozilla-nss.git] / security / nss / cmd / libpkix / perf / nss_threads.c
blob7f1a16eeec1c65e6fe6d00c48e65327aeb573e7b
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is the PKIX-C library.
16 * The Initial Developer of the Original Code is
17 * Sun Microsystems, Inc.
18 * Portions created by the Initial Developer are
19 * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
21 * Contributor(s):
22 * Sun Microsystems, Inc.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 * nssThreads.c
40 * NSS Performance Evaluation application (multi-threaded)
44 #include <stdio.h>
45 #include <string.h>
47 #include "secutil.h"
49 #include "nspr.h"
50 #include "prtypes.h"
51 #include "prtime.h"
52 #include "prlong.h"
54 #include "pk11func.h"
55 #include "secasn1.h"
56 #include "cert.h"
57 #include "cryptohi.h"
58 #include "secoid.h"
59 #include "certdb.h"
60 #include "nss.h"
62 typedef struct ThreadDataStr tData;
64 struct ThreadDataStr {
65 CERTCertificate* cert;
66 PRIntervalTime duration;
67 PRUint32 iterations;
70 static void ThreadEntry(void* data)
72 tData* tdata = (tData*) data;
73 PRIntervalTime duration = tdata->duration;
74 PRTime now = PR_Now();
75 PRIntervalTime start = PR_IntervalNow();
77 PR_ASSERT(duration);
78 if (!duration)
80 return;
82 do {
83 SECStatus rv = CERT_VerifyCertificate
84 (CERT_GetDefaultCertDB(),
85 tdata->cert,
86 PR_TRUE,
87 certificateUsageEmailSigner,
88 now,
89 NULL,
90 NULL,
91 NULL);
92 if (rv != SECSuccess)
94 (void) fprintf(stderr, "Validation failed.\n");
95 PORT_Assert(0);
96 return;
98 tdata->iterations ++;
99 } while ((PR_IntervalNow() - start) < duration);
102 static void Test(CERTCertificate* cert, PRIntervalTime duration, PRUint32 threads)
104 tData data;
105 tData** alldata;
106 PRIntervalTime starttime, endtime, elapsed;
107 PRUint32 msecs;
108 float total = 0;
109 PRThread** pthreads = NULL;
110 PRUint32 i = 0;
112 data.duration = duration;
113 data.cert = cert;
114 data.iterations = 0;
116 starttime = PR_IntervalNow();
117 pthreads = (PRThread**)PR_Malloc(threads*sizeof (PRThread*));
118 alldata = (tData**)PR_Malloc(threads*sizeof (tData*));
119 for (i = 0; i < threads; i++)
121 alldata[i] = (tData*)PR_Malloc(sizeof (tData));
122 *alldata[i] = data;
123 pthreads[i] =
124 PR_CreateThread(PR_USER_THREAD,
125 ThreadEntry,
126 (void*) alldata[i],
127 PR_PRIORITY_NORMAL,
128 PR_GLOBAL_THREAD,
129 PR_JOINABLE_THREAD,
133 for (i = 0; i < threads; i++)
135 tData* args = alldata[i];
136 PR_JoinThread(pthreads[i]);
137 total += args->iterations;
138 PR_Free((void*)args);
140 PR_Free((void*) pthreads);
141 PR_Free((void*) alldata);
142 endtime = PR_IntervalNow();
144 endtime = PR_IntervalNow();
145 elapsed = endtime - starttime;
146 msecs = PR_IntervalToMilliseconds(elapsed);
147 total /= msecs;
148 total *= 1000;
149 (void) fprintf(stdout, "%f operations per second.\n", total);
153 static void finish(char* message, int code)
155 (void) printf(message);
156 exit(code);
159 static void usage(char* progname)
161 (void) printf("Usage : %s <duration> <threads> <certnickname>\n\n",
162 progname);
163 finish("", 0);
166 int nss_threads(int argc, char** argv)
168 SECStatus rv = SECSuccess;
169 CERTCertDBHandle *handle = NULL;
170 CERTCertificate* cert = NULL;
171 PRIntervalTime duration = PR_SecondsToInterval(1);
172 PRUint32 threads = 1;
173 if (argc != 4)
175 usage(argv[0]);
177 if (atoi(argv[1]) > 0)
179 duration = PR_SecondsToInterval(atoi(argv[1]));
181 if (atoi(argv[2]) > 0)
183 threads = atoi(argv[2]);
186 handle = CERT_GetDefaultCertDB();
187 PR_ASSERT(handle);
188 cert = CERT_FindCertByNicknameOrEmailAddr(handle, argv[3]);
189 if (!cert)
191 finish("Unable to find certificate.\n", 1);
193 Test(cert, duration, threads);
195 CERT_DestroyCertificate(cert);
196 return (0);