bench.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, 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. #include "or.h"
  14. #include "onion_tap.h"
  15. #include "relay.h"
  16. #include <openssl/opensslv.h>
  17. #include <openssl/evp.h>
  18. #include <openssl/ec.h>
  19. #include <openssl/ecdh.h>
  20. #include <openssl/obj_mac.h>
  21. #include "config.h"
  22. #include "crypto_curve25519.h"
  23. #include "onion_ntor.h"
  24. #include "crypto_ed25519.h"
  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. #define MICROCOUNT(start,end,iters) \
  69. ( NANOCOUNT((start), (end), (iters)) / 1000.0 )
  70. /** Run AES performance benchmarks. */
  71. static void
  72. bench_aes(void)
  73. {
  74. int len, i;
  75. char *b1, *b2;
  76. crypto_cipher_t *c;
  77. uint64_t start, end;
  78. const int bytes_per_iter = (1<<24);
  79. reset_perftime();
  80. c = crypto_cipher_new(NULL);
  81. for (len = 1; len <= 8192; len *= 2) {
  82. int iters = bytes_per_iter / len;
  83. b1 = tor_malloc_zero(len);
  84. b2 = tor_malloc_zero(len);
  85. start = perftime();
  86. for (i = 0; i < iters; ++i) {
  87. crypto_cipher_encrypt(c, b1, b2, len);
  88. }
  89. end = perftime();
  90. tor_free(b1);
  91. tor_free(b2);
  92. printf("%d bytes: %.2f nsec per byte\n", len,
  93. NANOCOUNT(start, end, iters*len));
  94. }
  95. crypto_cipher_free(c);
  96. }
  97. static void
  98. bench_onion_TAP(void)
  99. {
  100. const int iters = 1<<9;
  101. int i;
  102. crypto_pk_t *key, *key2;
  103. uint64_t start, end;
  104. char os[TAP_ONIONSKIN_CHALLENGE_LEN];
  105. char or[TAP_ONIONSKIN_REPLY_LEN];
  106. crypto_dh_t *dh_out;
  107. key = crypto_pk_new();
  108. key2 = crypto_pk_new();
  109. if (crypto_pk_generate_key_with_bits(key, 1024) < 0)
  110. goto done;
  111. if (crypto_pk_generate_key_with_bits(key2, 1024) < 0)
  112. goto done;
  113. reset_perftime();
  114. start = perftime();
  115. for (i = 0; i < iters; ++i) {
  116. onion_skin_TAP_create(key, &dh_out, os);
  117. crypto_dh_free(dh_out);
  118. }
  119. end = perftime();
  120. printf("Client-side, part 1: %f usec.\n", NANOCOUNT(start, end, iters)/1e3);
  121. onion_skin_TAP_create(key, &dh_out, os);
  122. start = perftime();
  123. for (i = 0; i < iters; ++i) {
  124. char key_out[CPATH_KEY_MATERIAL_LEN];
  125. onion_skin_TAP_server_handshake(os, key, NULL, or,
  126. key_out, sizeof(key_out));
  127. }
  128. end = perftime();
  129. printf("Server-side, key guessed right: %f usec\n",
  130. NANOCOUNT(start, end, iters)/1e3);
  131. start = perftime();
  132. for (i = 0; i < iters; ++i) {
  133. char key_out[CPATH_KEY_MATERIAL_LEN];
  134. onion_skin_TAP_server_handshake(os, key2, key, or,
  135. key_out, sizeof(key_out));
  136. }
  137. end = perftime();
  138. printf("Server-side, key guessed wrong: %f usec.\n",
  139. NANOCOUNT(start, end, iters)/1e3);
  140. start = perftime();
  141. for (i = 0; i < iters; ++i) {
  142. crypto_dh_t *dh;
  143. char key_out[CPATH_KEY_MATERIAL_LEN];
  144. int s;
  145. dh = crypto_dh_dup(dh_out);
  146. s = onion_skin_TAP_client_handshake(dh, or, key_out, sizeof(key_out),
  147. NULL);
  148. crypto_dh_free(dh);
  149. tor_assert(s == 0);
  150. }
  151. end = perftime();
  152. printf("Client-side, part 2: %f usec.\n",
  153. NANOCOUNT(start, end, iters)/1e3);
  154. done:
  155. crypto_pk_free(key);
  156. crypto_pk_free(key2);
  157. }
  158. static void
  159. bench_onion_ntor_impl(void)
  160. {
  161. const int iters = 1<<10;
  162. int i;
  163. curve25519_keypair_t keypair1, keypair2;
  164. uint64_t start, end;
  165. uint8_t os[NTOR_ONIONSKIN_LEN];
  166. uint8_t or[NTOR_REPLY_LEN];
  167. ntor_handshake_state_t *state = NULL;
  168. uint8_t nodeid[DIGEST_LEN];
  169. di_digest256_map_t *keymap = NULL;
  170. curve25519_secret_key_generate(&keypair1.seckey, 0);
  171. curve25519_public_key_generate(&keypair1.pubkey, &keypair1.seckey);
  172. curve25519_secret_key_generate(&keypair2.seckey, 0);
  173. curve25519_public_key_generate(&keypair2.pubkey, &keypair2.seckey);
  174. dimap_add_entry(&keymap, keypair1.pubkey.public_key, &keypair1);
  175. dimap_add_entry(&keymap, keypair2.pubkey.public_key, &keypair2);
  176. reset_perftime();
  177. start = perftime();
  178. for (i = 0; i < iters; ++i) {
  179. onion_skin_ntor_create(nodeid, &keypair1.pubkey, &state, os);
  180. ntor_handshake_state_free(state);
  181. state = NULL;
  182. }
  183. end = perftime();
  184. printf("Client-side, part 1: %f usec.\n", NANOCOUNT(start, end, iters)/1e3);
  185. state = NULL;
  186. onion_skin_ntor_create(nodeid, &keypair1.pubkey, &state, os);
  187. start = perftime();
  188. for (i = 0; i < iters; ++i) {
  189. uint8_t key_out[CPATH_KEY_MATERIAL_LEN];
  190. onion_skin_ntor_server_handshake(os, keymap, NULL, nodeid, or,
  191. key_out, sizeof(key_out));
  192. }
  193. end = perftime();
  194. printf("Server-side: %f usec\n",
  195. NANOCOUNT(start, end, iters)/1e3);
  196. start = perftime();
  197. for (i = 0; i < iters; ++i) {
  198. uint8_t key_out[CPATH_KEY_MATERIAL_LEN];
  199. int s;
  200. s = onion_skin_ntor_client_handshake(state, or, key_out, sizeof(key_out),
  201. NULL);
  202. tor_assert(s == 0);
  203. }
  204. end = perftime();
  205. printf("Client-side, part 2: %f usec.\n",
  206. NANOCOUNT(start, end, iters)/1e3);
  207. ntor_handshake_state_free(state);
  208. dimap_free(keymap, NULL);
  209. }
  210. static void
  211. bench_onion_ntor(void)
  212. {
  213. int ed;
  214. for (ed = 0; ed <= 1; ++ed) {
  215. printf("Ed25519-based basepoint multiply = %s.\n",
  216. (ed == 0) ? "disabled" : "enabled");
  217. curve25519_set_impl_params(ed);
  218. bench_onion_ntor_impl();
  219. }
  220. }
  221. static void
  222. bench_ed25519(void)
  223. {
  224. uint64_t start, end;
  225. const int iters = 1<<12;
  226. int i;
  227. const uint8_t msg[] = "but leaving, could not tell what they had heard";
  228. ed25519_signature_t sig;
  229. ed25519_keypair_t kp;
  230. curve25519_keypair_t curve_kp;
  231. ed25519_public_key_t pubkey_tmp;
  232. ed25519_secret_key_generate(&kp.seckey, 0);
  233. start = perftime();
  234. for (i = 0; i < iters; ++i) {
  235. ed25519_public_key_generate(&kp.pubkey, &kp.seckey);
  236. }
  237. end = perftime();
  238. printf("Generate public key: %.2f usec\n",
  239. MICROCOUNT(start, end, iters));
  240. start = perftime();
  241. for (i = 0; i < iters; ++i) {
  242. ed25519_sign(&sig, msg, sizeof(msg), &kp);
  243. }
  244. end = perftime();
  245. printf("Sign a short message: %.2f usec\n",
  246. MICROCOUNT(start, end, iters));
  247. start = perftime();
  248. for (i = 0; i < iters; ++i) {
  249. ed25519_checksig(&sig, msg, sizeof(msg), &kp.pubkey);
  250. }
  251. end = perftime();
  252. printf("Verify signature: %.2f usec\n",
  253. MICROCOUNT(start, end, iters));
  254. curve25519_keypair_generate(&curve_kp, 0);
  255. start = perftime();
  256. for (i = 0; i < iters; ++i) {
  257. ed25519_public_key_from_curve25519_public_key(&pubkey_tmp,
  258. &curve_kp.pubkey, 1);
  259. }
  260. end = perftime();
  261. printf("Convert public point from curve25519: %.2f usec\n",
  262. MICROCOUNT(start, end, iters));
  263. curve25519_keypair_generate(&curve_kp, 0);
  264. start = perftime();
  265. for (i = 0; i < iters; ++i) {
  266. ed25519_public_blind(&pubkey_tmp, &kp.pubkey, msg);
  267. }
  268. end = perftime();
  269. printf("Blind a public key: %.2f usec\n",
  270. MICROCOUNT(start, end, iters));
  271. }
  272. static void
  273. bench_cell_aes(void)
  274. {
  275. uint64_t start, end;
  276. const int len = 509;
  277. const int iters = (1<<16);
  278. const int max_misalign = 15;
  279. char *b = tor_malloc(len+max_misalign);
  280. crypto_cipher_t *c;
  281. int i, misalign;
  282. c = crypto_cipher_new(NULL);
  283. reset_perftime();
  284. for (misalign = 0; misalign <= max_misalign; ++misalign) {
  285. start = perftime();
  286. for (i = 0; i < iters; ++i) {
  287. crypto_cipher_crypt_inplace(c, b+misalign, len);
  288. }
  289. end = perftime();
  290. printf("%d bytes, misaligned by %d: %.2f nsec per byte\n", len, misalign,
  291. NANOCOUNT(start, end, iters*len));
  292. }
  293. crypto_cipher_free(c);
  294. tor_free(b);
  295. }
  296. /** Run digestmap_t performance benchmarks. */
  297. static void
  298. bench_dmap(void)
  299. {
  300. smartlist_t *sl = smartlist_new();
  301. smartlist_t *sl2 = smartlist_new();
  302. uint64_t start, end, pt2, pt3, pt4;
  303. int iters = 8192;
  304. const int elts = 4000;
  305. const int fpostests = 100000;
  306. char d[20];
  307. int i,n=0, fp = 0;
  308. digestmap_t *dm = digestmap_new();
  309. digestset_t *ds = digestset_new(elts);
  310. for (i = 0; i < elts; ++i) {
  311. crypto_rand(d, 20);
  312. smartlist_add(sl, tor_memdup(d, 20));
  313. }
  314. for (i = 0; i < elts; ++i) {
  315. crypto_rand(d, 20);
  316. smartlist_add(sl2, tor_memdup(d, 20));
  317. }
  318. printf("nbits=%d\n", ds->mask+1);
  319. reset_perftime();
  320. start = perftime();
  321. for (i = 0; i < iters; ++i) {
  322. SMARTLIST_FOREACH(sl, const char *, cp, digestmap_set(dm, cp, (void*)1));
  323. }
  324. pt2 = perftime();
  325. printf("digestmap_set: %.2f ns per element\n",
  326. NANOCOUNT(start, pt2, iters*elts));
  327. for (i = 0; i < iters; ++i) {
  328. SMARTLIST_FOREACH(sl, const char *, cp, digestmap_get(dm, cp));
  329. SMARTLIST_FOREACH(sl2, const char *, cp, digestmap_get(dm, cp));
  330. }
  331. pt3 = perftime();
  332. printf("digestmap_get: %.2f ns per element\n",
  333. NANOCOUNT(pt2, pt3, iters*elts*2));
  334. for (i = 0; i < iters; ++i) {
  335. SMARTLIST_FOREACH(sl, const char *, cp, digestset_add(ds, cp));
  336. }
  337. pt4 = perftime();
  338. printf("digestset_add: %.2f ns per element\n",
  339. NANOCOUNT(pt3, pt4, iters*elts));
  340. for (i = 0; i < iters; ++i) {
  341. SMARTLIST_FOREACH(sl, const char *, cp, n += digestset_contains(ds, cp));
  342. SMARTLIST_FOREACH(sl2, const char *, cp, n += digestset_contains(ds, cp));
  343. }
  344. end = perftime();
  345. printf("digestset_contains: %.2f ns per element.\n",
  346. NANOCOUNT(pt4, end, iters*elts*2));
  347. /* We need to use this, or else the whole loop gets optimized out. */
  348. printf("Hits == %d\n", n);
  349. for (i = 0; i < fpostests; ++i) {
  350. crypto_rand(d, 20);
  351. if (digestset_contains(ds, d)) ++fp;
  352. }
  353. printf("False positive rate on digestset: %.2f%%\n",
  354. (fp/(double)fpostests)*100);
  355. digestmap_free(dm, NULL);
  356. digestset_free(ds);
  357. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  358. SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp));
  359. smartlist_free(sl);
  360. smartlist_free(sl2);
  361. }
  362. static void
  363. bench_siphash(void)
  364. {
  365. char buf[128];
  366. int lens[] = { 7, 8, 15, 16, 20, 32, 111, 128, -1 };
  367. int i, j;
  368. uint64_t start, end;
  369. const int N = 300000;
  370. crypto_rand(buf, sizeof(buf));
  371. for (i = 0; lens[i] > 0; ++i) {
  372. reset_perftime();
  373. start = perftime();
  374. for (j = 0; j < N; ++j) {
  375. siphash24g(buf, lens[i]);
  376. }
  377. end = perftime();
  378. printf("siphash24g(%d): %.2f ns per call\n",
  379. lens[i], NANOCOUNT(start,end,N));
  380. }
  381. }
  382. static void
  383. bench_cell_ops(void)
  384. {
  385. const int iters = 1<<16;
  386. int i;
  387. /* benchmarks for cell ops at relay. */
  388. or_circuit_t *or_circ = tor_malloc_zero(sizeof(or_circuit_t));
  389. cell_t *cell = tor_malloc(sizeof(cell_t));
  390. int outbound;
  391. uint64_t start, end;
  392. crypto_rand((char*)cell->payload, sizeof(cell->payload));
  393. /* Mock-up or_circuit_t */
  394. or_circ->base_.magic = OR_CIRCUIT_MAGIC;
  395. or_circ->base_.purpose = CIRCUIT_PURPOSE_OR;
  396. /* Initialize crypto */
  397. or_circ->p_crypto = crypto_cipher_new(NULL);
  398. or_circ->n_crypto = crypto_cipher_new(NULL);
  399. or_circ->p_digest = crypto_digest_new();
  400. or_circ->n_digest = crypto_digest_new();
  401. reset_perftime();
  402. for (outbound = 0; outbound <= 1; ++outbound) {
  403. cell_direction_t d = outbound ? CELL_DIRECTION_OUT : CELL_DIRECTION_IN;
  404. start = perftime();
  405. for (i = 0; i < iters; ++i) {
  406. char recognized = 0;
  407. crypt_path_t *layer_hint = NULL;
  408. relay_crypt(TO_CIRCUIT(or_circ), cell, d, &layer_hint, &recognized);
  409. }
  410. end = perftime();
  411. printf("%sbound cells: %.2f ns per cell. (%.2f ns per byte of payload)\n",
  412. outbound?"Out":" In",
  413. NANOCOUNT(start,end,iters),
  414. NANOCOUNT(start,end,iters*CELL_PAYLOAD_SIZE));
  415. }
  416. crypto_digest_free(or_circ->p_digest);
  417. crypto_digest_free(or_circ->n_digest);
  418. crypto_cipher_free(or_circ->p_crypto);
  419. crypto_cipher_free(or_circ->n_crypto);
  420. tor_free(or_circ);
  421. tor_free(cell);
  422. }
  423. static void
  424. bench_dh(void)
  425. {
  426. const int iters = 1<<10;
  427. int i;
  428. uint64_t start, end;
  429. reset_perftime();
  430. start = perftime();
  431. for (i = 0; i < iters; ++i) {
  432. char dh_pubkey_a[DH_BYTES], dh_pubkey_b[DH_BYTES];
  433. char secret_a[DH_BYTES], secret_b[DH_BYTES];
  434. ssize_t slen_a, slen_b;
  435. crypto_dh_t *dh_a = crypto_dh_new(DH_TYPE_TLS);
  436. crypto_dh_t *dh_b = crypto_dh_new(DH_TYPE_TLS);
  437. crypto_dh_generate_public(dh_a);
  438. crypto_dh_generate_public(dh_b);
  439. crypto_dh_get_public(dh_a, dh_pubkey_a, sizeof(dh_pubkey_a));
  440. crypto_dh_get_public(dh_b, dh_pubkey_b, sizeof(dh_pubkey_b));
  441. slen_a = crypto_dh_compute_secret(LOG_NOTICE,
  442. dh_a, dh_pubkey_b, sizeof(dh_pubkey_b),
  443. secret_a, sizeof(secret_a));
  444. slen_b = crypto_dh_compute_secret(LOG_NOTICE,
  445. dh_b, dh_pubkey_a, sizeof(dh_pubkey_a),
  446. secret_b, sizeof(secret_b));
  447. tor_assert(slen_a == slen_b);
  448. tor_assert(!memcmp(secret_a, secret_b, slen_a));
  449. crypto_dh_free(dh_a);
  450. crypto_dh_free(dh_b);
  451. }
  452. end = perftime();
  453. printf("Complete DH handshakes (1024 bit, public and private ops):\n"
  454. " %f millisec each.\n", NANOCOUNT(start, end, iters)/1e6);
  455. }
  456. static void
  457. bench_ecdh_impl(int nid, const char *name)
  458. {
  459. const int iters = 1<<10;
  460. int i;
  461. uint64_t start, end;
  462. reset_perftime();
  463. start = perftime();
  464. for (i = 0; i < iters; ++i) {
  465. char secret_a[DH_BYTES], secret_b[DH_BYTES];
  466. ssize_t slen_a, slen_b;
  467. EC_KEY *dh_a = EC_KEY_new_by_curve_name(nid);
  468. EC_KEY *dh_b = EC_KEY_new_by_curve_name(nid);
  469. if (!dh_a || !dh_b) {
  470. puts("Skipping. (No implementation?)");
  471. return;
  472. }
  473. EC_KEY_generate_key(dh_a);
  474. EC_KEY_generate_key(dh_b);
  475. slen_a = ECDH_compute_key(secret_a, DH_BYTES,
  476. EC_KEY_get0_public_key(dh_b), dh_a,
  477. NULL);
  478. slen_b = ECDH_compute_key(secret_b, DH_BYTES,
  479. EC_KEY_get0_public_key(dh_a), dh_b,
  480. NULL);
  481. tor_assert(slen_a == slen_b);
  482. tor_assert(!memcmp(secret_a, secret_b, slen_a));
  483. EC_KEY_free(dh_a);
  484. EC_KEY_free(dh_b);
  485. }
  486. end = perftime();
  487. printf("Complete ECDH %s handshakes (2 public and 2 private ops):\n"
  488. " %f millisec each.\n", name, NANOCOUNT(start, end, iters)/1e6);
  489. }
  490. static void
  491. bench_ecdh_p256(void)
  492. {
  493. bench_ecdh_impl(NID_X9_62_prime256v1, "P-256");
  494. }
  495. static void
  496. bench_ecdh_p224(void)
  497. {
  498. bench_ecdh_impl(NID_secp224r1, "P-224");
  499. }
  500. typedef void (*bench_fn)(void);
  501. typedef struct benchmark_t {
  502. const char *name;
  503. bench_fn fn;
  504. int enabled;
  505. } benchmark_t;
  506. #define ENT(s) { #s , bench_##s, 0 }
  507. static struct benchmark_t benchmarks[] = {
  508. ENT(dmap),
  509. ENT(siphash),
  510. ENT(aes),
  511. ENT(onion_TAP),
  512. ENT(onion_ntor),
  513. ENT(ed25519),
  514. ENT(cell_aes),
  515. ENT(cell_ops),
  516. ENT(dh),
  517. ENT(ecdh_p256),
  518. ENT(ecdh_p224),
  519. {NULL,NULL,0}
  520. };
  521. static benchmark_t *
  522. find_benchmark(const char *name)
  523. {
  524. benchmark_t *b;
  525. for (b = benchmarks; b->name; ++b) {
  526. if (!strcmp(name, b->name)) {
  527. return b;
  528. }
  529. }
  530. return NULL;
  531. }
  532. /** Main entry point for benchmark code: parse the command line, and run
  533. * some benchmarks. */
  534. int
  535. main(int argc, const char **argv)
  536. {
  537. int i;
  538. int list=0, n_enabled=0;
  539. benchmark_t *b;
  540. char *errmsg;
  541. or_options_t *options;
  542. tor_threads_init();
  543. for (i = 1; i < argc; ++i) {
  544. if (!strcmp(argv[i], "--list")) {
  545. list = 1;
  546. } else {
  547. benchmark_t *b = find_benchmark(argv[i]);
  548. ++n_enabled;
  549. if (b) {
  550. b->enabled = 1;
  551. } else {
  552. printf("No such benchmark as %s\n", argv[i]);
  553. }
  554. }
  555. }
  556. reset_perftime();
  557. crypto_seed_rng();
  558. crypto_init_siphash_key();
  559. options = options_new();
  560. init_logging(1);
  561. options->command = CMD_RUN_UNITTESTS;
  562. options->DataDirectory = tor_strdup("");
  563. options_init(options);
  564. if (set_options(options, &errmsg) < 0) {
  565. printf("Failed to set initial options: %s\n", errmsg);
  566. tor_free(errmsg);
  567. return 1;
  568. }
  569. for (b = benchmarks; b->name; ++b) {
  570. if (b->enabled || n_enabled == 0) {
  571. printf("===== %s =====\n", b->name);
  572. if (!list)
  573. b->fn();
  574. }
  575. }
  576. return 0;
  577. }