PageRenderTime 34ms CodeModel.GetById 18ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/zpao/v8monkey
C | 197 lines | 136 code | 19 blank | 42 comment | 11 complexity | aa6ccc08e74631a67850ad40e01022db MD5 | raw file
  1/* ***** BEGIN LICENSE BLOCK *****
  2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3 *
  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/
  8 *
  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.
 13 *
 14 * The Original Code is the PKIX-C library.
 15 *
 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.
 20 *
 21 * Contributor(s):
 22 *   Sun Microsystems, Inc.
 23 *
 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.
 35 *
 36 * ***** END LICENSE BLOCK ***** */
 37/*
 38 * nssThreads.c
 39 *
 40 * NSS Performance Evaluation application (multi-threaded)
 41 *
 42 */
 43
 44#include <stdio.h>
 45#include <string.h>
 46
 47#include "secutil.h"
 48
 49#include "nspr.h"
 50#include "prtypes.h"
 51#include "prtime.h"
 52#include "prlong.h"
 53
 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"
 61
 62typedef struct ThreadDataStr tData;
 63
 64struct ThreadDataStr {
 65    CERTCertificate* cert;
 66    PRIntervalTime duration;
 67    PRUint32 iterations;
 68};
 69
 70static void ThreadEntry(void* data)
 71{
 72        tData* tdata = (tData*) data;
 73        PRIntervalTime duration = tdata->duration;
 74        PRTime now = PR_Now();
 75        PRIntervalTime start = PR_IntervalNow();
 76
 77        PR_ASSERT(duration);
 78        if (!duration)
 79                {
 80                        return;
 81                }
 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)
 93                        {
 94                                (void) fprintf(stderr, "Validation failed.\n");
 95                                PORT_Assert(0);
 96                                return;
 97                        }
 98                tdata->iterations ++;
 99        } while ((PR_IntervalNow() - start) < duration);
100}
101
102static void Test(CERTCertificate* cert, PRIntervalTime duration, PRUint32 threads)
103{
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;
111
112        data.duration = duration;
113        data.cert = cert;
114        data.iterations = 0;
115
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++)
120                {
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,
130                                                0);
131
132                }
133        for (i = 0; i < threads; i++)
134                {
135                        tData* args = alldata[i];
136                        PR_JoinThread(pthreads[i]);
137                        total += args->iterations;
138                        PR_Free((void*)args);
139                }
140        PR_Free((void*) pthreads);
141        PR_Free((void*) alldata);
142        endtime = PR_IntervalNow();
143
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);
150}
151
152
153static void finish(char* message, int code)
154{
155        (void) printf(message);
156        exit(code);
157}
158
159static void usage(char* progname)
160{
161        (void) printf("Usage : %s <duration> <threads> <certnickname>\n\n",
162                    progname);
163        finish("", 0);
164}
165
166int nss_threads(int argc, char** argv)
167{
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)
174                {
175                        usage(argv[0]);
176                }
177        if (atoi(argv[1]) > 0)
178                {
179                        duration = PR_SecondsToInterval(atoi(argv[1]));
180                }
181        if (atoi(argv[2]) > 0)
182                {
183                        threads = atoi(argv[2]);
184                }
185
186        handle = CERT_GetDefaultCertDB();
187        PR_ASSERT(handle);
188        cert = CERT_FindCertByNicknameOrEmailAddr(handle, argv[3]);
189        if (!cert)
190                {
191                        finish("Unable to find certificate.\n", 1);
192                }
193        Test(cert, duration, threads);
194
195        CERT_DestroyCertificate(cert);
196        return (0);
197}