bench.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2012, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /* Ordinarily defined in tor_main.c; this bit is just here to provide one
  6. * since we're not linking to tor_main.c */
  7. const char tor_git_revision[] = "";
  8. /**
  9. * \file bench.c
  10. * \brief Benchmarks for lower level Tor modules.
  11. **/
  12. #include "orconfig.h"
  13. #define RELAY_PRIVATE
  14. #include "or.h"
  15. #include "relay.h"
  16. #include <openssl/opensslv.h>
  17. #include <openssl/evp.h>
  18. #if OPENSSL_VERSION_NUMBER >= OPENSSL_V_SERIES(1,0,0)
  19. #ifndef OPENSSL_NO_EC
  20. #include <openssl/ec.h>
  21. #include <openssl/ecdh.h>
  22. #include <openssl/obj_mac.h>
  23. #endif
  24. #endif
  25. #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_PROCESS_CPUTIME_ID)
  26. static uint64_t nanostart;
  27. static inline uint64_t
  28. timespec_to_nsec(const struct timespec *ts)
  29. {
  30. return ((uint64_t)ts->tv_sec)*1000000000 + ts->tv_nsec;
  31. }
  32. static void
  33. reset_perftime(void)
  34. {
  35. struct timespec ts;
  36. int r;
  37. r = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
  38. tor_assert(r == 0);
  39. nanostart = timespec_to_nsec(&ts);
  40. }
  41. static uint64_t
  42. perftime(void)
  43. {
  44. struct timespec ts;
  45. int r;
  46. r = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
  47. tor_assert(r == 0);
  48. return timespec_to_nsec(&ts) - nanostart;
  49. }
  50. #else
  51. static struct timeval tv_start = { 0, 0 };
  52. static void
  53. reset_perftime(void)
  54. {
  55. tor_gettimeofday(&tv_start);
  56. }
  57. static uint64_t
  58. perftime(void)
  59. {
  60. struct timeval now, out;
  61. tor_gettimeofday(&now);
  62. timersub(&now, &tv_start, &out);
  63. return ((uint64_t)out.tv_sec)*1000000000 + out.tv_usec*1000;
  64. }
  65. #endif
  66. #define NANOCOUNT(start,end,iters) \
  67. ( ((double)((end)-(start))) / (iters) )
  68. /** Run AES performance benchmarks. */
  69. static void
  70. bench_aes(void)
  71. {
  72. int len, i;
  73. char *b1, *b2;
  74. crypto_cipher_t *c;
  75. uint64_t start, end;
  76. const int bytes_per_iter = (1<<24);
  77. reset_perftime();
  78. c = crypto_cipher_new(NULL);
  79. for (len = 1; len <= 8192; len *= 2) {
  80. int iters = bytes_per_iter / len;
  81. b1 = tor_malloc_zero(len);
  82. b2 = tor_malloc_zero(len);
  83. start = perftime();
  84. for (i = 0; i < iters; ++i) {
  85. crypto_cipher_encrypt(c, b1, b2, len);
  86. }
  87. end = perftime();
  88. tor_free(b1);
  89. tor_free(b2);
  90. printf("%d bytes: %.2f nsec per byte\n", len,
  91. NANOCOUNT(start, end, iters*len));
  92. }
  93. crypto_cipher_free(c);
  94. }
  95. static void
  96. bench_cell_aes(void)
  97. {
  98. uint64_t start, end;
  99. const int len = 509;
  100. const int iters = (1<<16);
  101. const int max_misalign = 15;
  102. char *b = tor_malloc(len+max_misalign);
  103. crypto_cipher_t *c;
  104. int i, misalign;
  105. c = crypto_cipher_new(NULL);
  106. reset_perftime();
  107. for (misalign = 0; misalign <= max_misalign; ++misalign) {
  108. start = perftime();
  109. for (i = 0; i < iters; ++i) {
  110. crypto_cipher_crypt_inplace(c, b+misalign, len);
  111. }
  112. end = perftime();
  113. printf("%d bytes, misaligned by %d: %.2f nsec per byte\n", len, misalign,
  114. NANOCOUNT(start, end, iters*len));
  115. }
  116. crypto_cipher_free(c);
  117. tor_free(b);
  118. }
  119. /** Run digestmap_t performance benchmarks. */
  120. static void
  121. bench_dmap(void)
  122. {
  123. smartlist_t *sl = smartlist_new();
  124. smartlist_t *sl2 = smartlist_new();
  125. uint64_t start, end, pt2, pt3, pt4;
  126. int iters = 8192;
  127. const int elts = 4000;
  128. const int fpostests = 100000;
  129. char d[20];
  130. int i,n=0, fp = 0;
  131. digestmap_t *dm = digestmap_new();
  132. digestset_t *ds = digestset_new(elts);
  133. for (i = 0; i < elts; ++i) {
  134. crypto_rand(d, 20);
  135. smartlist_add(sl, tor_memdup(d, 20));
  136. }
  137. for (i = 0; i < elts; ++i) {
  138. crypto_rand(d, 20);
  139. smartlist_add(sl2, tor_memdup(d, 20));
  140. }
  141. printf("nbits=%d\n", ds->mask+1);
  142. reset_perftime();
  143. start = perftime();
  144. for (i = 0; i < iters; ++i) {
  145. SMARTLIST_FOREACH(sl, const char *, cp, digestmap_set(dm, cp, (void*)1));
  146. }
  147. pt2 = perftime();
  148. printf("digestmap_set: %.2f ns per element\n",
  149. NANOCOUNT(start, pt2, iters*elts));
  150. for (i = 0; i < iters; ++i) {
  151. SMARTLIST_FOREACH(sl, const char *, cp, digestmap_get(dm, cp));
  152. SMARTLIST_FOREACH(sl2, const char *, cp, digestmap_get(dm, cp));
  153. }
  154. pt3 = perftime();
  155. printf("digestmap_get: %.2f ns per element\n",
  156. NANOCOUNT(pt2, pt3, iters*elts*2));
  157. for (i = 0; i < iters; ++i) {
  158. SMARTLIST_FOREACH(sl, const char *, cp, digestset_add(ds, cp));
  159. }
  160. pt4 = perftime();
  161. printf("digestset_add: %.2f ns per element\n",
  162. NANOCOUNT(pt3, pt4, iters*elts));
  163. for (i = 0; i < iters; ++i) {
  164. SMARTLIST_FOREACH(sl, const char *, cp, n += digestset_isin(ds, cp));
  165. SMARTLIST_FOREACH(sl2, const char *, cp, n += digestset_isin(ds, cp));
  166. }
  167. end = perftime();
  168. printf("digestset_isin: %.2f ns per element.\n",
  169. NANOCOUNT(pt4, end, iters*elts*2));
  170. /* We need to use this, or else the whole loop gets optimized out. */
  171. printf("Hits == %d\n", n);
  172. for (i = 0; i < fpostests; ++i) {
  173. crypto_rand(d, 20);
  174. if (digestset_isin(ds, d)) ++fp;
  175. }
  176. printf("False positive rate on digestset: %.2f%%\n",
  177. (fp/(double)fpostests)*100);
  178. digestmap_free(dm, NULL);
  179. digestset_free(ds);
  180. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  181. SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp));
  182. smartlist_free(sl);
  183. smartlist_free(sl2);
  184. }
  185. static void
  186. bench_cell_ops(void)
  187. {
  188. const int iters = 1<<16;
  189. int i;
  190. /* benchmarks for cell ops at relay. */
  191. or_circuit_t *or_circ = tor_malloc_zero(sizeof(or_circuit_t));
  192. cell_t *cell = tor_malloc(sizeof(cell_t));
  193. int outbound;
  194. uint64_t start, end;
  195. crypto_rand((char*)cell->payload, sizeof(cell->payload));
  196. /* Mock-up or_circuit_t */
  197. or_circ->base_.magic = OR_CIRCUIT_MAGIC;
  198. or_circ->base_.purpose = CIRCUIT_PURPOSE_OR;
  199. /* Initialize crypto */
  200. or_circ->p_crypto = crypto_cipher_new(NULL);
  201. or_circ->n_crypto = crypto_cipher_new(NULL);
  202. or_circ->p_digest = crypto_digest_new();
  203. or_circ->n_digest = crypto_digest_new();
  204. reset_perftime();
  205. for (outbound = 0; outbound <= 1; ++outbound) {
  206. cell_direction_t d = outbound ? CELL_DIRECTION_OUT : CELL_DIRECTION_IN;
  207. start = perftime();
  208. for (i = 0; i < iters; ++i) {
  209. char recognized = 0;
  210. crypt_path_t *layer_hint = NULL;
  211. relay_crypt(TO_CIRCUIT(or_circ), cell, d, &layer_hint, &recognized);
  212. }
  213. end = perftime();
  214. printf("%sbound cells: %.2f ns per cell. (%.2f ns per byte of payload)\n",
  215. outbound?"Out":" In",
  216. NANOCOUNT(start,end,iters),
  217. NANOCOUNT(start,end,iters*CELL_PAYLOAD_SIZE));
  218. }
  219. crypto_digest_free(or_circ->p_digest);
  220. crypto_digest_free(or_circ->n_digest);
  221. crypto_cipher_free(or_circ->p_crypto);
  222. crypto_cipher_free(or_circ->n_crypto);
  223. tor_free(or_circ);
  224. tor_free(cell);
  225. }
  226. static void
  227. bench_dh(void)
  228. {
  229. const int iters = 1<<10;
  230. int i;
  231. uint64_t start, end;
  232. reset_perftime();
  233. start = perftime();
  234. for (i = 0; i < iters; ++i) {
  235. char dh_pubkey_a[DH_BYTES], dh_pubkey_b[DH_BYTES];
  236. char secret_a[DH_BYTES], secret_b[DH_BYTES];
  237. ssize_t slen_a, slen_b;
  238. crypto_dh_t *dh_a = crypto_dh_new(DH_TYPE_TLS);
  239. crypto_dh_t *dh_b = crypto_dh_new(DH_TYPE_TLS);
  240. crypto_dh_generate_public(dh_a);
  241. crypto_dh_generate_public(dh_b);
  242. crypto_dh_get_public(dh_a, dh_pubkey_a, sizeof(dh_pubkey_a));
  243. crypto_dh_get_public(dh_b, dh_pubkey_b, sizeof(dh_pubkey_b));
  244. slen_a = crypto_dh_compute_secret(LOG_NOTICE,
  245. dh_a, dh_pubkey_b, sizeof(dh_pubkey_b),
  246. secret_a, sizeof(secret_a));
  247. slen_b = crypto_dh_compute_secret(LOG_NOTICE,
  248. dh_b, dh_pubkey_a, sizeof(dh_pubkey_a),
  249. secret_b, sizeof(secret_b));
  250. tor_assert(slen_a == slen_b);
  251. tor_assert(!memcmp(secret_a, secret_b, slen_a));
  252. crypto_dh_free(dh_a);
  253. crypto_dh_free(dh_b);
  254. }
  255. end = perftime();
  256. printf("Complete DH handshakes (1024 bit, public and private ops):\n"
  257. " %f millisec each.\n", NANOCOUNT(start, end, iters)/1e6);
  258. }
  259. #if (!defined(OPENSSL_NO_EC) \
  260. && OPENSSL_VERSION_NUMBER >= OPENSSL_V_SERIES(1,0,0))
  261. #define HAVE_EC_BENCHMARKS
  262. static void
  263. bench_ecdh_impl(int nid, const char *name)
  264. {
  265. const int iters = 1<<10;
  266. int i;
  267. uint64_t start, end;
  268. reset_perftime();
  269. start = perftime();
  270. for (i = 0; i < iters; ++i) {
  271. char secret_a[DH_BYTES], secret_b[DH_BYTES];
  272. ssize_t slen_a, slen_b;
  273. EC_KEY *dh_a = EC_KEY_new_by_curve_name(nid);
  274. EC_KEY *dh_b = EC_KEY_new_by_curve_name(nid);
  275. EC_KEY_generate_key(dh_a);
  276. EC_KEY_generate_key(dh_b);
  277. slen_a = ECDH_compute_key(secret_a, DH_BYTES,
  278. EC_KEY_get0_public_key(dh_b), dh_a,
  279. NULL);
  280. slen_b = ECDH_compute_key(secret_b, DH_BYTES,
  281. EC_KEY_get0_public_key(dh_a), dh_b,
  282. NULL);
  283. tor_assert(slen_a == slen_b);
  284. tor_assert(!memcmp(secret_a, secret_b, slen_a));
  285. EC_KEY_free(dh_a);
  286. EC_KEY_free(dh_b);
  287. }
  288. end = perftime();
  289. printf("Complete ECDH %s handshakes (2 public and 2 private ops):\n"
  290. " %f millisec each.\n", name, NANOCOUNT(start, end, iters)/1e6);
  291. }
  292. static void
  293. bench_ecdh_p256(void)
  294. {
  295. bench_ecdh_impl(NID_X9_62_prime256v1, "P-256");
  296. }
  297. static void
  298. bench_ecdh_p224(void)
  299. {
  300. bench_ecdh_impl(NID_secp224r1, "P-224");
  301. }
  302. #endif
  303. typedef void (*bench_fn)(void);
  304. typedef struct benchmark_t {
  305. const char *name;
  306. bench_fn fn;
  307. int enabled;
  308. } benchmark_t;
  309. #define ENT(s) { #s , bench_##s, 0 }
  310. static struct benchmark_t benchmarks[] = {
  311. ENT(dmap),
  312. ENT(aes),
  313. ENT(cell_aes),
  314. ENT(cell_ops),
  315. ENT(dh),
  316. #ifdef HAVE_EC_BENCHMARKS
  317. ENT(ecdh_p256),
  318. ENT(ecdh_p224),
  319. #endif
  320. {NULL,NULL,0}
  321. };
  322. static benchmark_t *
  323. find_benchmark(const char *name)
  324. {
  325. benchmark_t *b;
  326. for (b = benchmarks; b->name; ++b) {
  327. if (!strcmp(name, b->name)) {
  328. return b;
  329. }
  330. }
  331. return NULL;
  332. }
  333. /** Main entry point for benchmark code: parse the command line, and run
  334. * some benchmarks. */
  335. int
  336. main(int argc, const char **argv)
  337. {
  338. int i;
  339. int list=0, n_enabled=0;
  340. benchmark_t *b;
  341. tor_threads_init();
  342. for (i = 1; i < argc; ++i) {
  343. if (!strcmp(argv[i], "--list")) {
  344. list = 1;
  345. } else {
  346. benchmark_t *b = find_benchmark(argv[i]);
  347. ++n_enabled;
  348. if (b) {
  349. b->enabled = 1;
  350. } else {
  351. printf("No such benchmark as %s\n", argv[i]);
  352. }
  353. }
  354. }
  355. reset_perftime();
  356. crypto_seed_rng(1);
  357. for (b = benchmarks; b->name; ++b) {
  358. if (b->enabled || n_enabled == 0) {
  359. printf("===== %s =====\n", b->name);
  360. if (!list)
  361. b->fn();
  362. }
  363. }
  364. return 0;
  365. }