Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / cmd / libpkix / perf / libpkix_buildthreads.c
blob2c28d228b87434f31ee12a267dec5d33daa9b964
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 Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Sun Microsystems
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 * libpkixBuildThreads.c
40 * libpkix Builder 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 #include "pkix.h"
63 #include "pkix_tools.h"
64 #include "pkix_pl_cert.h"
66 #include "testutil.h"
67 #include "testutil_nss.h"
69 void *plContext = NULL;
71 #define PERF_DECREF(obj) \
72 { \
73 PKIX_Error *pkixTempResult = NULL; \
74 if (obj){ \
75 pkixTempResult = PKIX_PL_Object_DecRef \
76 ((PKIX_PL_Object *)(obj), plContext); \
77 obj = NULL; \
78 } \
81 void finish(char* message, int code);
83 typedef struct ThreadDataStr tData;
85 struct ThreadDataStr {
86 CERTCertificate* anchor;
87 char* eecertName;
88 PRIntervalTime duration;
89 CERTCertDBHandle *handle;
90 PRUint32 iterations;
93 #define PKIX_LOGGER_ON 1
95 #ifdef PKIX_LOGGER_ON
97 char *logLevels[] = {
98 "None",
99 "Fatal Error",
100 "Error",
101 "Warning",
102 "Debug",
103 "Trace"
106 PKIX_Error *loggerCallback(
107 PKIX_Logger *logger,
108 PKIX_PL_String *message,
109 PKIX_UInt32 logLevel,
110 PKIX_ERRORNUM logComponent,
111 void *plContext)
113 char *msg = NULL;
114 static int callCount = 0;
116 msg = PKIX_String2ASCII(message, plContext);
117 printf("Logging %s (%s): %s\n",
118 logLevels[logLevel],
119 PKIX_ERRORNAMES[logComponent],
120 msg);
121 PR_Free((void *)msg);
123 return(NULL);
126 #endif /* PKIX_LOGGER_ON */
128 void ThreadEntry(void* data)
130 tData* tdata = (tData*) data;
131 PRIntervalTime duration = tdata->duration;
132 PRIntervalTime start = PR_IntervalNow();
134 PKIX_List *anchors = NULL;
135 PKIX_ProcessingParams *procParams = NULL;
136 PKIX_BuildResult *buildResult = NULL;
137 CERTCertificate* nsseecert;
138 PKIX_PL_Cert *eeCert = NULL;
139 PKIX_CertStore *certStore = NULL;
140 PKIX_List *certStores = NULL;
141 PKIX_ComCertSelParams *certSelParams = NULL;
142 PKIX_CertSelector *certSelector = NULL;
143 PKIX_PL_Date *nowDate = NULL;
144 void *state = NULL; /* only relevant with non-blocking I/O */
145 void *nbioContext = NULL; /* only relevant with non-blocking I/O */
147 PR_ASSERT(duration);
148 if (!duration){
149 return;
152 do {
154 /* libpkix code */
156 /* keep more update time, testing cache */
157 PKIX_PL_Date_Create_UTCTime(NULL, &nowDate, plContext);
159 /* CertUsage is 0x10 and no NSS arena */
160 /* We haven't determined how we obtain the value of wincx */
162 nsseecert = CERT_FindCertByNicknameOrEmailAddr(tdata->handle,
163 tdata->eecertName);
164 if (!nsseecert) finish("Unable to find eecert.\n", 1);
166 pkix_pl_Cert_CreateWithNSSCert
167 (nsseecert, &eeCert, plContext);
169 PKIX_List_Create(&anchors, plContext);
172 * This code is retired.
173 * pkix_pl_Cert_CreateWithNSSCert
174 * (tdata->anchor, &anchorCert, NULL);
175 * PKIX_TrustAnchor_CreateWithCert(anchorCert, &anchor, NULL);
176 * PKIX_List_AppendItem(anchors, (PKIX_PL_Object *)anchor, NULL);
179 PKIX_ProcessingParams_Create(anchors, &procParams, plContext);
181 PKIX_ProcessingParams_SetRevocationEnabled
182 (procParams, PKIX_TRUE, plContext);
184 PKIX_ProcessingParams_SetDate
185 (procParams, nowDate, plContext);
187 /* create CertSelector with target certificate in params */
189 PKIX_ComCertSelParams_Create(&certSelParams, plContext);
191 PKIX_ComCertSelParams_SetCertificate
192 (certSelParams, eeCert, plContext);
194 PKIX_CertSelector_Create
195 (NULL, NULL, &certSelector, plContext);
197 PKIX_CertSelector_SetCommonCertSelectorParams
198 (certSelector, certSelParams, plContext);
200 PKIX_ProcessingParams_SetTargetCertConstraints
201 (procParams, certSelector, plContext);
203 PKIX_PL_Pk11CertStore_Create(&certStore, plContext);
205 PKIX_List_Create(&certStores, plContext);
206 PKIX_List_AppendItem
207 (certStores, (PKIX_PL_Object *)certStore, plContext);
208 PKIX_ProcessingParams_SetCertStores
209 (procParams, certStores, plContext);
211 PKIX_BuildChain
212 (procParams,
213 &nbioContext,
214 &state,
215 &buildResult,
216 NULL,
217 plContext);
220 * As long as we use only CertStores with blocking I/O, we
221 * know we must be done at this point.
224 if (!buildResult){
225 (void) fprintf(stderr, "libpkix BuildChain failed.\n");
226 PORT_Assert(0);
227 return;
230 tdata->iterations ++;
232 PERF_DECREF(nowDate);
233 PERF_DECREF(anchors);
234 PERF_DECREF(procParams);
235 PERF_DECREF(buildResult);
236 PERF_DECREF(certStore);
237 PERF_DECREF(certStores);
238 PERF_DECREF(certSelParams);
239 PERF_DECREF(certSelector);
240 PERF_DECREF(eeCert);
242 } while ((PR_IntervalNow() - start) < duration);
247 void
248 Test(
249 CERTCertificate* anchor,
250 char* eecertName,
251 PRIntervalTime duration,
252 CERTCertDBHandle *handle,
253 PRUint32 threads)
255 tData data;
256 tData** alldata;
257 PRIntervalTime starttime, endtime, elapsed;
258 PRUint32 msecs;
259 float total = 0;
260 PRThread** pthreads = NULL;
261 PRUint32 i = 0;
263 data.duration = duration;
264 data.anchor = anchor;
265 data.eecertName = eecertName;
266 data.handle = handle;
268 data.iterations = 0;
270 starttime = PR_IntervalNow();
271 pthreads = (PRThread**)PR_Malloc(threads*sizeof (PRThread*));
272 alldata = (tData**)PR_Malloc(threads*sizeof (tData*));
273 for (i = 0; i < threads; i++){
274 alldata[i] = (tData*)PR_Malloc(sizeof (tData));
275 *alldata[i] = data;
276 pthreads[i] =
277 PR_CreateThread(PR_USER_THREAD,
278 ThreadEntry,
279 (void*) alldata[i],
280 PR_PRIORITY_NORMAL,
281 PR_GLOBAL_THREAD,
282 PR_JOINABLE_THREAD,
286 for (i = 0; i < threads; i++) {
287 tData* args = alldata[i];
288 PR_JoinThread(pthreads[i]);
289 total += args->iterations;
290 PR_Free((void*)args);
293 PR_Free((void*) pthreads);
294 PR_Free((void*) alldata);
295 endtime = PR_IntervalNow();
297 endtime = PR_IntervalNow();
298 elapsed = endtime - starttime;
299 msecs = PR_IntervalToMilliseconds(elapsed);
300 total /= msecs;
301 total *= 1000;
302 (void) fprintf(stdout, "%f operations per second.\n", total);
306 void finish(char* message, int code)
308 (void) printf(message);
309 exit(code);
312 void usage(char* progname)
314 (void) printf("Usage : %s <duration> <threads> <anchorNickname> "
315 "<eecertNickname>\n\n", progname);
316 finish("", 0);
319 int main(int argc, char** argv)
321 CERTCertDBHandle *handle = NULL;
322 CERTCertificate* eecert = NULL;
323 PRIntervalTime duration = PR_SecondsToInterval(1);
324 PRUint32 threads = 1;
325 PKIX_UInt32 actualMinorVersion;
326 PKIX_UInt32 j = 0;
327 PKIX_Logger *logger = NULL;
328 void *wincx = NULL;
330 /* if (argc != 5) -- when TrustAnchor used to be on command line */
331 if (argc != 4)
333 usage(argv[0]);
335 if (atoi(argv[1]) > 0)
337 duration = PR_SecondsToInterval(atoi(argv[1]));
339 if (atoi(argv[2]) > 0)
341 threads = atoi(argv[2]);
344 PKIX_Initialize_SetConfigDir(PKIX_STORE_TYPE_PK11, ".", plContext);
346 PKIX_Initialize(PKIX_TRUE, /* nssInitNeeded */
347 PKIX_FALSE, /* useArenas */
348 PKIX_MAJOR_VERSION,
349 PKIX_MINOR_VERSION,
350 PKIX_MINOR_VERSION,
351 &actualMinorVersion,
352 &plContext);
354 handle = CERT_GetDefaultCertDB();
355 PR_ASSERT(handle);
357 #ifdef PKIX_LOGGER_ON
359 /* set logger to log trace and up */
360 PKIX_SetLoggers(NULL, plContext);
361 PKIX_Logger_Create(loggerCallback, NULL, &logger, plContext);
362 PKIX_Logger_SetMaxLoggingLevel
363 (logger, PKIX_LOGGER_LEVEL_WARNING, plContext);
364 PKIX_AddLogger(logger, plContext);
366 #endif /* PKIX_LOGGER_ON */
369 * This code is retired
370 * anchor = CERT_FindCertByNicknameOrEmailAddr(handle, argv[3]);
371 * if (!anchor) finish("Unable to find anchor.\n", 1);
373 * eecert = CERT_FindCertByNicknameOrEmailAddr(handle, argv[4]);
375 * if (!eecert) finish("Unable to find eecert.\n", 1);
377 * Test(anchor, eecert, duration, threads);
380 Test(NULL, argv[3], duration, handle, threads);
382 PERF_DECREF(logger);
384 PKIX_Shutdown(plContext);
386 return (0);