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
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.
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)
63 #include "pkix_tools.h"
64 #include "pkix_pl_cert.h"
67 #include "testutil_nss.h"
69 void *plContext
= NULL
;
71 #define PERF_DECREF(obj) \
73 PKIX_Error *pkixTempResult = NULL; \
75 pkixTempResult = PKIX_PL_Object_DecRef \
76 ((PKIX_PL_Object *)(obj), plContext); \
81 void finish(char* message
, int code
);
83 typedef struct ThreadDataStr tData
;
85 struct ThreadDataStr
{
86 CERTCertificate
* anchor
;
88 PRIntervalTime duration
;
89 CERTCertDBHandle
*handle
;
93 #define PKIX_LOGGER_ON 1
106 PKIX_Error
*loggerCallback(
108 PKIX_PL_String
*message
,
109 PKIX_UInt32 logLevel
,
110 PKIX_ERRORNUM logComponent
,
114 static int callCount
= 0;
116 msg
= PKIX_String2ASCII(message
, plContext
);
117 printf("Logging %s (%s): %s\n",
119 PKIX_ERRORNAMES
[logComponent
],
121 PR_Free((void *)msg
);
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 */
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
,
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
);
207 (certStores
, (PKIX_PL_Object
*)certStore
, plContext
);
208 PKIX_ProcessingParams_SetCertStores
209 (procParams
, certStores
, plContext
);
220 * As long as we use only CertStores with blocking I/O, we
221 * know we must be done at this point.
225 (void) fprintf(stderr
, "libpkix BuildChain failed.\n");
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
);
242 } while ((PR_IntervalNow() - start
) < duration
);
249 CERTCertificate
* anchor
,
251 PRIntervalTime duration
,
252 CERTCertDBHandle
*handle
,
257 PRIntervalTime starttime
, endtime
, elapsed
;
260 PRThread
** pthreads
= NULL
;
263 data
.duration
= duration
;
264 data
.anchor
= anchor
;
265 data
.eecertName
= eecertName
;
266 data
.handle
= handle
;
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
));
277 PR_CreateThread(PR_USER_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
);
302 (void) fprintf(stdout
, "%f operations per second.\n", total
);
306 void finish(char* message
, int code
)
308 (void) printf(message
);
312 void usage(char* progname
)
314 (void) printf("Usage : %s <duration> <threads> <anchorNickname> "
315 "<eecertNickname>\n\n", progname
);
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
;
327 PKIX_Logger
*logger
= NULL
;
330 /* if (argc != 5) -- when TrustAnchor used to be on command line */
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 */
354 handle
= CERT_GetDefaultCertDB();
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
);
384 PKIX_Shutdown(plContext
);