summaryrefslogtreecommitdiffstats
path: root/security/nss/cmd/libpkix/perf/nss_threads.c
blob: 202f128b616bc72599affc4504164f0b7c5d9b04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
 * nssThreads.c
 *
 * NSS Performance Evaluation application (multi-threaded)
 *
 */

#include <stdio.h>
#include <string.h>

#include "secutil.h"

#include "nspr.h"
#include "prtypes.h"
#include "prtime.h"
#include "prlong.h"

#include "pk11func.h"
#include "secasn1.h"
#include "cert.h"
#include "cryptohi.h"
#include "secoid.h"
#include "certdb.h"
#include "nss.h"

typedef struct ThreadDataStr tData;

struct ThreadDataStr {
    CERTCertificate* cert;
    PRIntervalTime duration;
    PRUint32 iterations;
};

static void
ThreadEntry(void* data)
{
    tData* tdata = (tData*)data;
    PRIntervalTime duration = tdata->duration;
    PRTime now = PR_Now();
    PRIntervalTime start = PR_IntervalNow();

    PR_ASSERT(duration);
    if (!duration) {
        return;
    }
    do {
        SECStatus rv = CERT_VerifyCertificate(CERT_GetDefaultCertDB(),
                                              tdata->cert,
                                              PR_TRUE,
                                              certificateUsageEmailSigner,
                                              now,
                                              NULL,
                                              NULL,
                                              NULL);
        if (rv != SECSuccess) {
            (void)fprintf(stderr, "Validation failed.\n");
            PORT_Assert(0);
            return;
        }
        tdata->iterations++;
    } while ((PR_IntervalNow() - start) < duration);
}

static void
Test(CERTCertificate* cert, PRIntervalTime duration, PRUint32 threads)
{
    tData data;
    tData** alldata;
    PRIntervalTime starttime, endtime, elapsed;
    PRUint32 msecs;
    float total = 0;
    PRThread** pthreads = NULL;
    PRUint32 i = 0;

    data.duration = duration;
    data.cert = cert;
    data.iterations = 0;

    starttime = PR_IntervalNow();
    pthreads = (PRThread**)PR_Malloc(threads * sizeof(PRThread*));
    alldata = (tData**)PR_Malloc(threads * sizeof(tData*));
    for (i = 0; i < threads; i++) {
        alldata[i] = (tData*)PR_Malloc(sizeof(tData));
        *alldata[i] = data;
        pthreads[i] =
            PR_CreateThread(PR_USER_THREAD,
                            ThreadEntry,
                            (void*)alldata[i],
                            PR_PRIORITY_NORMAL,
                            PR_GLOBAL_THREAD,
                            PR_JOINABLE_THREAD,
                            0);
    }
    for (i = 0; i < threads; i++) {
        tData* args = alldata[i];
        PR_JoinThread(pthreads[i]);
        total += args->iterations;
        PR_Free((void*)args);
    }
    PR_Free((void*)pthreads);
    PR_Free((void*)alldata);
    endtime = PR_IntervalNow();

    endtime = PR_IntervalNow();
    elapsed = endtime - starttime;
    msecs = PR_IntervalToMilliseconds(elapsed);
    total /= msecs;
    total *= 1000;
    (void)fprintf(stdout, "%f operations per second.\n", total);
}

static void
finish(char* message, int code)
{
    (void)printf(message);
    exit(code);
}

static void
usage(char* progname)
{
    (void)printf("Usage : %s <duration> <threads> <certnickname>\n\n",
                 progname);
    finish("", 0);
}

int
nss_threads(int argc, char** argv)
{
    SECStatus rv = SECSuccess;
    CERTCertDBHandle* handle = NULL;
    CERTCertificate* cert = NULL;
    PRIntervalTime duration = PR_SecondsToInterval(1);
    PRUint32 threads = 1;
    if (argc != 4) {
        usage(argv[0]);
    }
    if (atoi(argv[1]) > 0) {
        duration = PR_SecondsToInterval(atoi(argv[1]));
    }
    if (atoi(argv[2]) > 0) {
        threads = atoi(argv[2]);
    }

    handle = CERT_GetDefaultCertDB();
    PR_ASSERT(handle);
    cert = CERT_FindCertByNicknameOrEmailAddr(handle, argv[3]);
    if (!cert) {
        finish("Unable to find certificate.\n", 1);
    }
    Test(cert, duration, threads);

    CERT_DestroyCertificate(cert);
    return (0);
}