bench.c 17 KB

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