bench.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. #define CONFIG_PRIVATE
  15. #include "or.h"
  16. #include "onion_tap.h"
  17. #include "relay.h"
  18. #include "config.h"
  19. #ifdef CURVE25519_ENABLED
  20. #include "crypto_curve25519.h"
  21. #include "onion_ntor.h"
  22. #endif
  23. #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_PROCESS_CPUTIME_ID)
  24. static uint64_t nanostart;
  25. static inline uint64_t
  26. timespec_to_nsec(const struct timespec *ts)
  27. {
  28. return ((uint64_t)ts->tv_sec)*1000000000 + ts->tv_nsec;
  29. }
  30. static void
  31. reset_perftime(void)
  32. {
  33. struct timespec ts;
  34. int r;
  35. r = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
  36. tor_assert(r == 0);
  37. nanostart = timespec_to_nsec(&ts);
  38. }
  39. static uint64_t
  40. perftime(void)
  41. {
  42. struct timespec ts;
  43. int r;
  44. r = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
  45. tor_assert(r == 0);
  46. return timespec_to_nsec(&ts) - nanostart;
  47. }
  48. #else
  49. static struct timeval tv_start = { 0, 0 };
  50. static void
  51. reset_perftime(void)
  52. {
  53. tor_gettimeofday(&tv_start);
  54. }
  55. static uint64_t
  56. perftime(void)
  57. {
  58. struct timeval now, out;
  59. tor_gettimeofday(&now);
  60. timersub(&now, &tv_start, &out);
  61. return ((uint64_t)out.tv_sec)*1000000000 + out.tv_usec*1000;
  62. }
  63. #endif
  64. #define NANOCOUNT(start,end,iters) \
  65. ( ((double)((end)-(start))) / (iters) )
  66. /** Run AES performance benchmarks. */
  67. static void
  68. bench_aes(void)
  69. {
  70. int len, i;
  71. char *b1, *b2;
  72. crypto_cipher_t *c;
  73. uint64_t start, end;
  74. const int bytes_per_iter = (1<<24);
  75. reset_perftime();
  76. c = crypto_cipher_new(NULL);
  77. for (len = 1; len <= 8192; len *= 2) {
  78. int iters = bytes_per_iter / len;
  79. b1 = tor_malloc_zero(len);
  80. b2 = tor_malloc_zero(len);
  81. start = perftime();
  82. for (i = 0; i < iters; ++i) {
  83. crypto_cipher_encrypt(c, b1, b2, len);
  84. }
  85. end = perftime();
  86. tor_free(b1);
  87. tor_free(b2);
  88. printf("%d bytes: %.2f nsec per byte\n", len,
  89. NANOCOUNT(start, end, iters*len));
  90. }
  91. crypto_cipher_free(c);
  92. }
  93. static void
  94. bench_onion_TAP(void)
  95. {
  96. const int iters = 1<<9;
  97. int i;
  98. crypto_pk_t *key, *key2;
  99. uint64_t start, end;
  100. char os[TAP_ONIONSKIN_CHALLENGE_LEN];
  101. char or[TAP_ONIONSKIN_REPLY_LEN];
  102. crypto_dh_t *dh_out;
  103. key = crypto_pk_new();
  104. key2 = crypto_pk_new();
  105. crypto_pk_generate_key_with_bits(key, 1024);
  106. crypto_pk_generate_key_with_bits(key2, 1024);
  107. reset_perftime();
  108. start = perftime();
  109. for (i = 0; i < iters; ++i) {
  110. onion_skin_TAP_create(key, &dh_out, os);
  111. crypto_dh_free(dh_out);
  112. }
  113. end = perftime();
  114. printf("Client-side, part 1: %f usec.\n", NANOCOUNT(start, end, iters)/1e3);
  115. onion_skin_TAP_create(key, &dh_out, os);
  116. start = perftime();
  117. for (i = 0; i < iters; ++i) {
  118. char key_out[CPATH_KEY_MATERIAL_LEN];
  119. onion_skin_TAP_server_handshake(os, key, NULL, or,
  120. key_out, sizeof(key_out));
  121. }
  122. end = perftime();
  123. printf("Server-side, key guessed right: %f usec\n",
  124. NANOCOUNT(start, end, iters)/1e3);
  125. start = perftime();
  126. for (i = 0; i < iters; ++i) {
  127. char key_out[CPATH_KEY_MATERIAL_LEN];
  128. onion_skin_TAP_server_handshake(os, key2, key, or,
  129. key_out, sizeof(key_out));
  130. }
  131. end = perftime();
  132. printf("Server-side, key guessed wrong: %f usec.\n",
  133. NANOCOUNT(start, end, iters)/1e3);
  134. start = perftime();
  135. for (i = 0; i < iters; ++i) {
  136. crypto_dh_t *dh;
  137. char key_out[CPATH_KEY_MATERIAL_LEN];
  138. int s;
  139. dh = crypto_dh_dup(dh_out);
  140. s = onion_skin_TAP_client_handshake(dh, or, key_out, sizeof(key_out));
  141. tor_assert(s == 0);
  142. }
  143. end = perftime();
  144. printf("Client-side, part 2: %f usec.\n",
  145. NANOCOUNT(start, end, iters)/1e3);
  146. crypto_pk_free(key);
  147. }
  148. #ifdef CURVE25519_ENABLED
  149. static void
  150. bench_onion_ntor(void)
  151. {
  152. const int iters = 1<<10;
  153. int i;
  154. curve25519_keypair_t keypair1, keypair2;
  155. uint64_t start, end;
  156. uint8_t os[NTOR_ONIONSKIN_LEN];
  157. uint8_t or[NTOR_REPLY_LEN];
  158. ntor_handshake_state_t *state = NULL;
  159. uint8_t nodeid[DIGEST_LEN];
  160. di_digest256_map_t *keymap = NULL;
  161. curve25519_secret_key_generate(&keypair1.seckey, 0);
  162. curve25519_public_key_generate(&keypair1.pubkey, &keypair1.seckey);
  163. curve25519_secret_key_generate(&keypair2.seckey, 0);
  164. curve25519_public_key_generate(&keypair2.pubkey, &keypair2.seckey);
  165. dimap_add_entry(&keymap, keypair1.pubkey.public_key, &keypair1);
  166. dimap_add_entry(&keymap, keypair2.pubkey.public_key, &keypair2);
  167. reset_perftime();
  168. start = perftime();
  169. for (i = 0; i < iters; ++i) {
  170. onion_skin_ntor_create(nodeid, &keypair1.pubkey, &state, os);
  171. ntor_handshake_state_free(state);
  172. }
  173. end = perftime();
  174. printf("Client-side, part 1: %f usec.\n", NANOCOUNT(start, end, iters)/1e3);
  175. onion_skin_ntor_create(nodeid, &keypair1.pubkey, &state, os);
  176. start = perftime();
  177. for (i = 0; i < iters; ++i) {
  178. uint8_t key_out[CPATH_KEY_MATERIAL_LEN];
  179. onion_skin_ntor_server_handshake(os, keymap, NULL, nodeid, or,
  180. key_out, sizeof(key_out));
  181. }
  182. end = perftime();
  183. printf("Server-side: %f usec\n",
  184. NANOCOUNT(start, end, iters)/1e3);
  185. start = perftime();
  186. for (i = 0; i < iters; ++i) {
  187. uint8_t key_out[CPATH_KEY_MATERIAL_LEN];
  188. int s;
  189. s = onion_skin_ntor_client_handshake(state, or, key_out, sizeof(key_out));
  190. tor_assert(s == 0);
  191. }
  192. end = perftime();
  193. printf("Client-side, part 2: %f usec.\n",
  194. NANOCOUNT(start, end, iters)/1e3);
  195. ntor_handshake_state_free(state);
  196. dimap_free(keymap, NULL);
  197. }
  198. #endif
  199. static void
  200. bench_cell_aes(void)
  201. {
  202. uint64_t start, end;
  203. const int len = 509;
  204. const int iters = (1<<16);
  205. const int max_misalign = 15;
  206. char *b = tor_malloc(len+max_misalign);
  207. crypto_cipher_t *c;
  208. int i, misalign;
  209. c = crypto_cipher_new(NULL);
  210. reset_perftime();
  211. for (misalign = 0; misalign <= max_misalign; ++misalign) {
  212. start = perftime();
  213. for (i = 0; i < iters; ++i) {
  214. crypto_cipher_crypt_inplace(c, b+misalign, len);
  215. }
  216. end = perftime();
  217. printf("%d bytes, misaligned by %d: %.2f nsec per byte\n", len, misalign,
  218. NANOCOUNT(start, end, iters*len));
  219. }
  220. crypto_cipher_free(c);
  221. tor_free(b);
  222. }
  223. /** Run digestmap_t performance benchmarks. */
  224. static void
  225. bench_dmap(void)
  226. {
  227. smartlist_t *sl = smartlist_new();
  228. smartlist_t *sl2 = smartlist_new();
  229. uint64_t start, end, pt2, pt3, pt4;
  230. int iters = 8192;
  231. const int elts = 4000;
  232. const int fpostests = 100000;
  233. char d[20];
  234. int i,n=0, fp = 0;
  235. digestmap_t *dm = digestmap_new();
  236. digestset_t *ds = digestset_new(elts);
  237. for (i = 0; i < elts; ++i) {
  238. crypto_rand(d, 20);
  239. smartlist_add(sl, tor_memdup(d, 20));
  240. }
  241. for (i = 0; i < elts; ++i) {
  242. crypto_rand(d, 20);
  243. smartlist_add(sl2, tor_memdup(d, 20));
  244. }
  245. printf("nbits=%d\n", ds->mask+1);
  246. reset_perftime();
  247. start = perftime();
  248. for (i = 0; i < iters; ++i) {
  249. SMARTLIST_FOREACH(sl, const char *, cp, digestmap_set(dm, cp, (void*)1));
  250. }
  251. pt2 = perftime();
  252. printf("digestmap_set: %.2f ns per element\n",
  253. NANOCOUNT(start, pt2, iters*elts));
  254. for (i = 0; i < iters; ++i) {
  255. SMARTLIST_FOREACH(sl, const char *, cp, digestmap_get(dm, cp));
  256. SMARTLIST_FOREACH(sl2, const char *, cp, digestmap_get(dm, cp));
  257. }
  258. pt3 = perftime();
  259. printf("digestmap_get: %.2f ns per element\n",
  260. NANOCOUNT(pt2, pt3, iters*elts*2));
  261. for (i = 0; i < iters; ++i) {
  262. SMARTLIST_FOREACH(sl, const char *, cp, digestset_add(ds, cp));
  263. }
  264. pt4 = perftime();
  265. printf("digestset_add: %.2f ns per element\n",
  266. NANOCOUNT(pt3, pt4, iters*elts));
  267. for (i = 0; i < iters; ++i) {
  268. SMARTLIST_FOREACH(sl, const char *, cp, n += digestset_isin(ds, cp));
  269. SMARTLIST_FOREACH(sl2, const char *, cp, n += digestset_isin(ds, cp));
  270. }
  271. end = perftime();
  272. printf("digestset_isin: %.2f ns per element.\n",
  273. NANOCOUNT(pt4, end, iters*elts*2));
  274. /* We need to use this, or else the whole loop gets optimized out. */
  275. printf("Hits == %d\n", n);
  276. for (i = 0; i < fpostests; ++i) {
  277. crypto_rand(d, 20);
  278. if (digestset_isin(ds, d)) ++fp;
  279. }
  280. printf("False positive rate on digestset: %.2f%%\n",
  281. (fp/(double)fpostests)*100);
  282. digestmap_free(dm, NULL);
  283. digestset_free(ds);
  284. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  285. SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp));
  286. smartlist_free(sl);
  287. smartlist_free(sl2);
  288. }
  289. static void
  290. bench_cell_ops(void)
  291. {
  292. const int iters = 1<<16;
  293. int i;
  294. /* benchmarks for cell ops at relay. */
  295. or_circuit_t *or_circ = tor_malloc_zero(sizeof(or_circuit_t));
  296. cell_t *cell = tor_malloc(sizeof(cell_t));
  297. int outbound;
  298. uint64_t start, end;
  299. crypto_rand((char*)cell->payload, sizeof(cell->payload));
  300. /* Mock-up or_circuit_t */
  301. or_circ->base_.magic = OR_CIRCUIT_MAGIC;
  302. or_circ->base_.purpose = CIRCUIT_PURPOSE_OR;
  303. /* Initialize crypto */
  304. or_circ->p_crypto = crypto_cipher_new(NULL);
  305. or_circ->n_crypto = crypto_cipher_new(NULL);
  306. or_circ->p_digest = crypto_digest_new();
  307. or_circ->n_digest = crypto_digest_new();
  308. reset_perftime();
  309. for (outbound = 0; outbound <= 1; ++outbound) {
  310. cell_direction_t d = outbound ? CELL_DIRECTION_OUT : CELL_DIRECTION_IN;
  311. start = perftime();
  312. for (i = 0; i < iters; ++i) {
  313. char recognized = 0;
  314. crypt_path_t *layer_hint = NULL;
  315. relay_crypt(TO_CIRCUIT(or_circ), cell, d, &layer_hint, &recognized);
  316. }
  317. end = perftime();
  318. printf("%sbound cells: %.2f ns per cell. (%.2f ns per byte of payload)\n",
  319. outbound?"Out":" In",
  320. NANOCOUNT(start,end,iters),
  321. NANOCOUNT(start,end,iters*CELL_PAYLOAD_SIZE));
  322. }
  323. crypto_digest_free(or_circ->p_digest);
  324. crypto_digest_free(or_circ->n_digest);
  325. crypto_cipher_free(or_circ->p_crypto);
  326. crypto_cipher_free(or_circ->n_crypto);
  327. tor_free(or_circ);
  328. tor_free(cell);
  329. }
  330. typedef void (*bench_fn)(void);
  331. typedef struct benchmark_t {
  332. const char *name;
  333. bench_fn fn;
  334. int enabled;
  335. } benchmark_t;
  336. #define ENT(s) { #s , bench_##s, 0 }
  337. static struct benchmark_t benchmarks[] = {
  338. ENT(dmap),
  339. ENT(aes),
  340. ENT(onion_TAP),
  341. #ifdef CURVE25519_ENABLED
  342. ENT(onion_ntor),
  343. #endif
  344. ENT(cell_aes),
  345. ENT(cell_ops),
  346. {NULL,NULL,0}
  347. };
  348. static benchmark_t *
  349. find_benchmark(const char *name)
  350. {
  351. benchmark_t *b;
  352. for (b = benchmarks; b->name; ++b) {
  353. if (!strcmp(name, b->name)) {
  354. return b;
  355. }
  356. }
  357. return NULL;
  358. }
  359. /** Main entry point for benchmark code: parse the command line, and run
  360. * some benchmarks. */
  361. int
  362. main(int argc, const char **argv)
  363. {
  364. int i;
  365. int list=0, n_enabled=0;
  366. benchmark_t *b;
  367. char *errmsg;
  368. or_options_t *options;
  369. tor_threads_init();
  370. for (i = 1; i < argc; ++i) {
  371. if (!strcmp(argv[i], "--list")) {
  372. list = 1;
  373. } else {
  374. benchmark_t *b = find_benchmark(argv[i]);
  375. ++n_enabled;
  376. if (b) {
  377. b->enabled = 1;
  378. } else {
  379. printf("No such benchmark as %s\n", argv[i]);
  380. }
  381. }
  382. }
  383. reset_perftime();
  384. crypto_seed_rng(1);
  385. options = options_new();
  386. init_logging();
  387. options->command = CMD_RUN_UNITTESTS;
  388. options->DataDirectory = tor_strdup("");
  389. options_init(options);
  390. if (set_options(options, &errmsg) < 0) {
  391. printf("Failed to set initial options: %s\n", errmsg);
  392. tor_free(errmsg);
  393. return 1;
  394. }
  395. for (b = benchmarks; b->name; ++b) {
  396. if (b->enabled || n_enabled == 0) {
  397. printf("===== %s =====\n", b->name);
  398. if (!list)
  399. b->fn();
  400. }
  401. }
  402. return 0;
  403. }