bench.c 17 KB

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