/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. #include <stdio.h>
  44. #include <string.h>
  45. #include "secutil.h"
  46. #include "nspr.h"
  47. #include "prtypes.h"
  48. #include "prtime.h"
  49. #include "prlong.h"
  50. #include "pk11func.h"
  51. #include "secasn1.h"
  52. #include "cert.h"
  53. #include "cryptohi.h"
  54. #include "secoid.h"
  55. #include "certdb.h"
  56. #include "nss.h"
  57. typedef struct ThreadDataStr tData;
  58. struct ThreadDataStr {
  59. CERTCertificate* cert;
  60. PRIntervalTime duration;
  61. PRUint32 iterations;
  62. };
  63. static void ThreadEntry(void* data)
  64. {
  65. tData* tdata = (tData*) data;
  66. PRIntervalTime duration = tdata->duration;
  67. PRTime now = PR_Now();
  68. PRIntervalTime start = PR_IntervalNow();
  69. PR_ASSERT(duration);
  70. if (!duration)
  71. {
  72. return;
  73. }
  74. do {
  75. SECStatus rv = CERT_VerifyCertificate
  76. (CERT_GetDefaultCertDB(),
  77. tdata->cert,
  78. PR_TRUE,
  79. certificateUsageEmailSigner,
  80. now,
  81. NULL,
  82. NULL,
  83. NULL);
  84. if (rv != SECSuccess)
  85. {
  86. (void) fprintf(stderr, "Validation failed.\n");
  87. PORT_Assert(0);
  88. return;
  89. }
  90. tdata->iterations ++;
  91. } while ((PR_IntervalNow() - start) < duration);
  92. }
  93. static void Test(CERTCertificate* cert, PRIntervalTime duration, PRUint32 threads)
  94. {
  95. tData data;
  96. tData** alldata;
  97. PRIntervalTime starttime, endtime, elapsed;
  98. PRUint32 msecs;
  99. float total = 0;
  100. PRThread** pthreads = NULL;
  101. PRUint32 i = 0;
  102. data.duration = duration;
  103. data.cert = cert;
  104. data.iterations = 0;
  105. starttime = PR_IntervalNow();
  106. pthreads = (PRThread**)PR_Malloc(threads*sizeof (PRThread*));
  107. alldata = (tData**)PR_Malloc(threads*sizeof (tData*));
  108. for (i = 0; i < threads; i++)
  109. {
  110. alldata[i] = (tData*)PR_Malloc(sizeof (tData));
  111. *alldata[i] = data;
  112. pthreads[i] =
  113. PR_CreateThread(PR_USER_THREAD,
  114. ThreadEntry,
  115. (void*) alldata[i],
  116. PR_PRIORITY_NORMAL,
  117. PR_GLOBAL_THREAD,
  118. PR_JOINABLE_THREAD,
  119. 0);
  120. }
  121. for (i = 0; i < threads; i++)
  122. {
  123. tData* args = alldata[i];
  124. PR_JoinThread(pthreads[i]);
  125. total += args->iterations;
  126. PR_Free((void*)args);
  127. }
  128. PR_Free((void*) pthreads);
  129. PR_Free((void*) alldata);
  130. endtime = PR_IntervalNow();
  131. endtime = PR_IntervalNow();
  132. elapsed = endtime - starttime;
  133. msecs = PR_IntervalToMilliseconds(elapsed);
  134. total /= msecs;
  135. total *= 1000;
  136. (void) fprintf(stdout, "%f operations per second.\n", total);
  137. }
  138. static void finish(char* message, int code)
  139. {
  140. (void) printf(message);
  141. exit(code);
  142. }
  143. static void usage(char* progname)
  144. {
  145. (void) printf("Usage : %s <duration> <threads> <certnickname>\n\n",
  146. progname);
  147. finish("", 0);
  148. }
  149. int nss_threads(int argc, char** argv)
  150. {
  151. SECStatus rv = SECSuccess;
  152. CERTCertDBHandle *handle = NULL;
  153. CERTCertificate* cert = NULL;
  154. PRIntervalTime duration = PR_SecondsToInterval(1);
  155. PRUint32 threads = 1;
  156. if (argc != 4)
  157. {
  158. usage(argv[0]);
  159. }
  160. if (atoi(argv[1]) > 0)
  161. {
  162. duration = PR_SecondsToInterval(atoi(argv[1]));
  163. }
  164. if (atoi(argv[2]) > 0)
  165. {
  166. threads = atoi(argv[2]);
  167. }
  168. handle = CERT_GetDefaultCertDB();
  169. PR_ASSERT(handle);
  170. cert = CERT_FindCertByNicknameOrEmailAddr(handle, argv[3]);
  171. if (!cert)
  172. {
  173. finish("Unable to find certificate.\n", 1);
  174. }
  175. Test(cert, duration, threads);
  176. CERT_DestroyCertificate(cert);
  177. return (0);
  178. }