bench.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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[ONIONSKIN_CHALLENGE_LEN];
  101. char or[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_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_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_server_handshake(os, key, NULL, or, key_out, sizeof(key_out));
  120. }
  121. end = perftime();
  122. printf("Server-side, key guessed right: %f usec\n",
  123. NANOCOUNT(start, end, iters)/1e3);
  124. start = perftime();
  125. for (i = 0; i < iters; ++i) {
  126. char key_out[CPATH_KEY_MATERIAL_LEN];
  127. onion_skin_server_handshake(os, key2, key, or, key_out, sizeof(key_out));
  128. }
  129. end = perftime();
  130. printf("Server-side, key guessed wrong: %f usec.\n",
  131. NANOCOUNT(start, end, iters)/1e3);
  132. start = perftime();
  133. for (i = 0; i < iters; ++i) {
  134. crypto_dh_t *dh;
  135. char key_out[CPATH_KEY_MATERIAL_LEN];
  136. int s;
  137. dh = crypto_dh_dup(dh_out);
  138. s = onion_skin_client_handshake(dh, or, key_out, sizeof(key_out));
  139. tor_assert(s == 0);
  140. }
  141. end = perftime();
  142. printf("Client-side, part 2: %f usec.\n",
  143. NANOCOUNT(start, end, iters)/1e3);
  144. crypto_pk_free(key);
  145. }
  146. #ifdef CURVE25519_ENABLED
  147. static void
  148. bench_onion_ntor(void)
  149. {
  150. const int iters = 1<<10;
  151. int i;
  152. curve25519_keypair_t keypair1, keypair2;
  153. uint64_t start, end;
  154. uint8_t os[NTOR_ONIONSKIN_LEN];
  155. uint8_t or[NTOR_REPLY_LEN];
  156. ntor_handshake_state_t *state = NULL;
  157. uint8_t nodeid[DIGEST_LEN];
  158. di_digest256_map_t *keymap = NULL;
  159. curve25519_secret_key_generate(&keypair1.seckey, 0);
  160. curve25519_public_key_generate(&keypair1.pubkey, &keypair1.seckey);
  161. curve25519_secret_key_generate(&keypair2.seckey, 0);
  162. curve25519_public_key_generate(&keypair2.pubkey, &keypair2.seckey);
  163. dimap_add_entry(&keymap, keypair1.pubkey.public_key, &keypair1);
  164. dimap_add_entry(&keymap, keypair2.pubkey.public_key, &keypair2);
  165. reset_perftime();
  166. start = perftime();
  167. for (i = 0; i < iters; ++i) {
  168. onion_skin_ntor_create(nodeid, &keypair1.pubkey, &state, os);
  169. ntor_handshake_state_free(state);
  170. }
  171. end = perftime();
  172. printf("Client-side, part 1: %f usec.\n", NANOCOUNT(start, end, iters)/1e3);
  173. onion_skin_ntor_create(nodeid, &keypair1.pubkey, &state, os);
  174. start = perftime();
  175. for (i = 0; i < iters; ++i) {
  176. uint8_t key_out[CPATH_KEY_MATERIAL_LEN];
  177. onion_skin_ntor_server_handshake(os, keymap, nodeid, or,
  178. key_out, sizeof(key_out));
  179. }
  180. end = perftime();
  181. printf("Server-side: %f usec\n",
  182. NANOCOUNT(start, end, iters)/1e3);
  183. start = perftime();
  184. for (i = 0; i < iters; ++i) {
  185. uint8_t key_out[CPATH_KEY_MATERIAL_LEN];
  186. int s;
  187. s = onion_skin_ntor_client_handshake(state, or, key_out, sizeof(key_out));
  188. tor_assert(s == 0);
  189. }
  190. end = perftime();
  191. printf("Client-side, part 2: %f usec.\n",
  192. NANOCOUNT(start, end, iters)/1e3);
  193. ntor_handshake_state_free(state);
  194. dimap_free(keymap, NULL);
  195. }
  196. #endif
  197. static void
  198. bench_cell_aes(void)
  199. {
  200. uint64_t start, end;
  201. const int len = 509;
  202. const int iters = (1<<16);
  203. const int max_misalign = 15;
  204. char *b = tor_malloc(len+max_misalign);
  205. crypto_cipher_t *c;
  206. int i, misalign;
  207. c = crypto_cipher_new(NULL);
  208. reset_perftime();
  209. for (misalign = 0; misalign <= max_misalign; ++misalign) {
  210. start = perftime();
  211. for (i = 0; i < iters; ++i) {
  212. crypto_cipher_crypt_inplace(c, b+misalign, len);
  213. }
  214. end = perftime();
  215. printf("%d bytes, misaligned by %d: %.2f nsec per byte\n", len, misalign,
  216. NANOCOUNT(start, end, iters*len));
  217. }
  218. crypto_cipher_free(c);
  219. tor_free(b);
  220. }
  221. /** Run digestmap_t performance benchmarks. */
  222. static void
  223. bench_dmap(void)
  224. {
  225. smartlist_t *sl = smartlist_new();
  226. smartlist_t *sl2 = smartlist_new();
  227. uint64_t start, end, pt2, pt3, pt4;
  228. int iters = 8192;
  229. const int elts = 4000;
  230. const int fpostests = 100000;
  231. char d[20];
  232. int i,n=0, fp = 0;
  233. digestmap_t *dm = digestmap_new();
  234. digestset_t *ds = digestset_new(elts);
  235. for (i = 0; i < elts; ++i) {
  236. crypto_rand(d, 20);
  237. smartlist_add(sl, tor_memdup(d, 20));
  238. }
  239. for (i = 0; i < elts; ++i) {
  240. crypto_rand(d, 20);
  241. smartlist_add(sl2, tor_memdup(d, 20));
  242. }
  243. printf("nbits=%d\n", ds->mask+1);
  244. reset_perftime();
  245. start = perftime();
  246. for (i = 0; i < iters; ++i) {
  247. SMARTLIST_FOREACH(sl, const char *, cp, digestmap_set(dm, cp, (void*)1));
  248. }
  249. pt2 = perftime();
  250. printf("digestmap_set: %.2f ns per element\n",
  251. NANOCOUNT(start, pt2, iters*elts));
  252. for (i = 0; i < iters; ++i) {
  253. SMARTLIST_FOREACH(sl, const char *, cp, digestmap_get(dm, cp));
  254. SMARTLIST_FOREACH(sl2, const char *, cp, digestmap_get(dm, cp));
  255. }
  256. pt3 = perftime();
  257. printf("digestmap_get: %.2f ns per element\n",
  258. NANOCOUNT(pt2, pt3, iters*elts*2));
  259. for (i = 0; i < iters; ++i) {
  260. SMARTLIST_FOREACH(sl, const char *, cp, digestset_add(ds, cp));
  261. }
  262. pt4 = perftime();
  263. printf("digestset_add: %.2f ns per element\n",
  264. NANOCOUNT(pt3, pt4, iters*elts));
  265. for (i = 0; i < iters; ++i) {
  266. SMARTLIST_FOREACH(sl, const char *, cp, n += digestset_isin(ds, cp));
  267. SMARTLIST_FOREACH(sl2, const char *, cp, n += digestset_isin(ds, cp));
  268. }
  269. end = perftime();
  270. printf("digestset_isin: %.2f ns per element.\n",
  271. NANOCOUNT(pt4, end, iters*elts*2));
  272. /* We need to use this, or else the whole loop gets optimized out. */
  273. printf("Hits == %d\n", n);
  274. for (i = 0; i < fpostests; ++i) {
  275. crypto_rand(d, 20);
  276. if (digestset_isin(ds, d)) ++fp;
  277. }
  278. printf("False positive rate on digestset: %.2f%%\n",
  279. (fp/(double)fpostests)*100);
  280. digestmap_free(dm, NULL);
  281. digestset_free(ds);
  282. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  283. SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp));
  284. smartlist_free(sl);
  285. smartlist_free(sl2);
  286. }
  287. static void
  288. bench_cell_ops(void)
  289. {
  290. const int iters = 1<<16;
  291. int i;
  292. /* benchmarks for cell ops at relay. */
  293. or_circuit_t *or_circ = tor_malloc_zero(sizeof(or_circuit_t));
  294. cell_t *cell = tor_malloc(sizeof(cell_t));
  295. int outbound;
  296. uint64_t start, end;
  297. crypto_rand((char*)cell->payload, sizeof(cell->payload));
  298. /* Mock-up or_circuit_t */
  299. or_circ->base_.magic = OR_CIRCUIT_MAGIC;
  300. or_circ->base_.purpose = CIRCUIT_PURPOSE_OR;
  301. /* Initialize crypto */
  302. or_circ->p_crypto = crypto_cipher_new(NULL);
  303. or_circ->n_crypto = crypto_cipher_new(NULL);
  304. or_circ->p_digest = crypto_digest_new();
  305. or_circ->n_digest = crypto_digest_new();
  306. reset_perftime();
  307. for (outbound = 0; outbound <= 1; ++outbound) {
  308. cell_direction_t d = outbound ? CELL_DIRECTION_OUT : CELL_DIRECTION_IN;
  309. start = perftime();
  310. for (i = 0; i < iters; ++i) {
  311. char recognized = 0;
  312. crypt_path_t *layer_hint = NULL;
  313. relay_crypt(TO_CIRCUIT(or_circ), cell, d, &layer_hint, &recognized);
  314. }
  315. end = perftime();
  316. printf("%sbound cells: %.2f ns per cell. (%.2f ns per byte of payload)\n",
  317. outbound?"Out":" In",
  318. NANOCOUNT(start,end,iters),
  319. NANOCOUNT(start,end,iters*CELL_PAYLOAD_SIZE));
  320. }
  321. crypto_digest_free(or_circ->p_digest);
  322. crypto_digest_free(or_circ->n_digest);
  323. crypto_cipher_free(or_circ->p_crypto);
  324. crypto_cipher_free(or_circ->n_crypto);
  325. tor_free(or_circ);
  326. tor_free(cell);
  327. }
  328. typedef void (*bench_fn)(void);
  329. typedef struct benchmark_t {
  330. const char *name;
  331. bench_fn fn;
  332. int enabled;
  333. } benchmark_t;
  334. #define ENT(s) { #s , bench_##s, 0 }
  335. static struct benchmark_t benchmarks[] = {
  336. ENT(dmap),
  337. ENT(aes),
  338. ENT(onion_TAP),
  339. #ifdef CURVE25519_ENABLED
  340. ENT(onion_ntor),
  341. #endif
  342. ENT(cell_aes),
  343. ENT(cell_ops),
  344. {NULL,NULL,0}
  345. };
  346. static benchmark_t *
  347. find_benchmark(const char *name)
  348. {
  349. benchmark_t *b;
  350. for (b = benchmarks; b->name; ++b) {
  351. if (!strcmp(name, b->name)) {
  352. return b;
  353. }
  354. }
  355. return NULL;
  356. }
  357. /** Main entry point for benchmark code: parse the command line, and run
  358. * some benchmarks. */
  359. int
  360. main(int argc, const char **argv)
  361. {
  362. int i;
  363. int list=0, n_enabled=0;
  364. benchmark_t *b;
  365. char *errmsg;
  366. or_options_t *options;
  367. tor_threads_init();
  368. for (i = 1; i < argc; ++i) {
  369. if (!strcmp(argv[i], "--list")) {
  370. list = 1;
  371. } else {
  372. benchmark_t *b = find_benchmark(argv[i]);
  373. ++n_enabled;
  374. if (b) {
  375. b->enabled = 1;
  376. } else {
  377. printf("No such benchmark as %s\n", argv[i]);
  378. }
  379. }
  380. }
  381. reset_perftime();
  382. crypto_seed_rng(1);
  383. options = options_new();
  384. init_logging();
  385. options->command = CMD_RUN_UNITTESTS;
  386. options->DataDirectory = tor_strdup("");
  387. options_init(options);
  388. if (set_options(options, &errmsg) < 0) {
  389. printf("Failed to set initial options: %s\n", errmsg);
  390. tor_free(errmsg);
  391. return 1;
  392. }
  393. for (b = benchmarks; b->name; ++b) {
  394. if (b->enabled || n_enabled == 0) {
  395. printf("===== %s =====\n", b->name);
  396. if (!list)
  397. b->fn();
  398. }
  399. }
  400. return 0;
  401. }