bench.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2011, 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. #include "or.h"
  15. #include "relay.h"
  16. #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_PROCESS_CPUTIME_ID)
  17. static uint64_t nanostart;
  18. static inline uint64_t
  19. timespec_to_nsec(const struct timespec *ts)
  20. {
  21. return ((uint64_t)ts->tv_sec)*1000000000 + ts->tv_nsec;
  22. }
  23. static void
  24. reset_perftime(void)
  25. {
  26. struct timespec ts;
  27. int r;
  28. r = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
  29. tor_assert(r == 0);
  30. nanostart = timespec_to_nsec(&ts);
  31. }
  32. static uint64_t
  33. 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. return timespec_to_nsec(&ts) - nanostart;
  40. }
  41. #else
  42. static struct timeval tv_start = { 0, 0 };
  43. static void
  44. reset_perftime(void)
  45. {
  46. tor_gettimeofday(&tv_start);
  47. }
  48. static uint64_t
  49. perftime(void)
  50. {
  51. struct timeval now, out;
  52. tor_gettimeofday(&now);
  53. timersub(&now, &tv_start, &out);
  54. return ((uint64_t)out.tv_sec)*1000000000 + out.tv_usec*1000;
  55. }
  56. #endif
  57. #define NANOCOUNT(start,end,iters) \
  58. ( ((double)((end)-(start))) / (iters) )
  59. /** Run AES performance benchmarks. */
  60. static void
  61. bench_aes(void)
  62. {
  63. int len, i;
  64. char *b1, *b2;
  65. crypto_cipher_t *c;
  66. uint64_t start, end;
  67. const int bytes_per_iter = (1<<24);
  68. reset_perftime();
  69. c = crypto_cipher_new();
  70. crypto_cipher_generate_key(c);
  71. crypto_cipher_encrypt_init_cipher(c);
  72. for (len = 1; len <= 8192; len *= 2) {
  73. int iters = bytes_per_iter / len;
  74. b1 = tor_malloc_zero(len);
  75. b2 = tor_malloc_zero(len);
  76. start = perftime();
  77. for (i = 0; i < iters; ++i) {
  78. crypto_cipher_encrypt(c, b1, b2, len);
  79. }
  80. end = perftime();
  81. tor_free(b1);
  82. tor_free(b2);
  83. printf("%d bytes: %.2f nsec per byte\n", len,
  84. NANOCOUNT(start, end, iters*len));
  85. }
  86. crypto_cipher_free(c);
  87. }
  88. static void
  89. bench_cell_aes(void)
  90. {
  91. uint64_t start, end;
  92. const int len = 509;
  93. const int iters = (1<<16);
  94. const int max_misalign = 15;
  95. char *b = tor_malloc(len+max_misalign);
  96. crypto_cipher_t *c;
  97. int i, misalign;
  98. c = crypto_cipher_new();
  99. crypto_cipher_generate_key(c);
  100. crypto_cipher_encrypt_init_cipher(c);
  101. reset_perftime();
  102. for (misalign = 0; misalign <= max_misalign; ++misalign) {
  103. start = perftime();
  104. for (i = 0; i < iters; ++i) {
  105. crypto_cipher_crypt_inplace(c, b+misalign, len);
  106. }
  107. end = perftime();
  108. printf("%d bytes, misaligned by %d: %.2f nsec per byte\n", len, misalign,
  109. NANOCOUNT(start, end, iters*len));
  110. }
  111. crypto_cipher_free(c);
  112. tor_free(b);
  113. }
  114. /** Run digestmap_t performance benchmarks. */
  115. static void
  116. bench_dmap(void)
  117. {
  118. smartlist_t *sl = smartlist_new();
  119. smartlist_t *sl2 = smartlist_new();
  120. uint64_t start, end, pt2, pt3, pt4;
  121. int iters = 8192;
  122. const int elts = 4000;
  123. const int fpostests = 100000;
  124. char d[20];
  125. int i,n=0, fp = 0;
  126. digestmap_t *dm = digestmap_new();
  127. digestset_t *ds = digestset_new(elts);
  128. for (i = 0; i < elts; ++i) {
  129. crypto_rand(d, 20);
  130. smartlist_add(sl, tor_memdup(d, 20));
  131. }
  132. for (i = 0; i < elts; ++i) {
  133. crypto_rand(d, 20);
  134. smartlist_add(sl2, tor_memdup(d, 20));
  135. }
  136. printf("nbits=%d\n", ds->mask+1);
  137. reset_perftime();
  138. start = perftime();
  139. for (i = 0; i < iters; ++i) {
  140. SMARTLIST_FOREACH(sl, const char *, cp, digestmap_set(dm, cp, (void*)1));
  141. }
  142. pt2 = perftime();
  143. printf("digestmap_set: %.2f ns per element\n",
  144. NANOCOUNT(start, pt2, iters*elts));
  145. for (i = 0; i < iters; ++i) {
  146. SMARTLIST_FOREACH(sl, const char *, cp, digestmap_get(dm, cp));
  147. SMARTLIST_FOREACH(sl2, const char *, cp, digestmap_get(dm, cp));
  148. }
  149. pt3 = perftime();
  150. printf("digestmap_get: %.2f ns per element\n",
  151. NANOCOUNT(pt2, pt3, iters*elts*2));
  152. for (i = 0; i < iters; ++i) {
  153. SMARTLIST_FOREACH(sl, const char *, cp, digestset_add(ds, cp));
  154. }
  155. pt4 = perftime();
  156. printf("digestset_add: %.2f ns per element\n",
  157. NANOCOUNT(pt3, pt4, iters*elts));
  158. for (i = 0; i < iters; ++i) {
  159. SMARTLIST_FOREACH(sl, const char *, cp, n += digestset_isin(ds, cp));
  160. SMARTLIST_FOREACH(sl2, const char *, cp, n += digestset_isin(ds, cp));
  161. }
  162. end = perftime();
  163. printf("digestset_isin: %.2f ns per element.\n",
  164. NANOCOUNT(pt4, end, iters*elts*2));
  165. /* We need to use this, or else the whole loop gets optimized out. */
  166. printf("Hits == %d\n", n);
  167. for (i = 0; i < fpostests; ++i) {
  168. crypto_rand(d, 20);
  169. if (digestset_isin(ds, d)) ++fp;
  170. }
  171. printf("False positive rate on digestset: %.2f%%\n",
  172. (fp/(double)fpostests)*100);
  173. digestmap_free(dm, NULL);
  174. digestset_free(ds);
  175. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  176. SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp));
  177. smartlist_free(sl);
  178. smartlist_free(sl2);
  179. }
  180. static void
  181. bench_cell_ops(void)
  182. {
  183. const int iters = 1<<16;
  184. int i;
  185. /* benchmarks for cell ops at relay. */
  186. or_circuit_t *or_circ = tor_malloc_zero(sizeof(or_circuit_t));
  187. cell_t *cell = tor_malloc(sizeof(cell_t));
  188. int outbound;
  189. uint64_t start, end;
  190. crypto_rand((char*)cell->payload, sizeof(cell->payload));
  191. /* Mock-up or_circuit_t */
  192. or_circ->_base.magic = OR_CIRCUIT_MAGIC;
  193. or_circ->_base.purpose = CIRCUIT_PURPOSE_OR;
  194. /* Initialize crypto */
  195. or_circ->p_crypto = crypto_cipher_new();
  196. crypto_cipher_generate_key(or_circ->p_crypto);
  197. crypto_cipher_encrypt_init_cipher(or_circ->p_crypto);
  198. or_circ->n_crypto = crypto_cipher_new();
  199. crypto_cipher_generate_key(or_circ->n_crypto);
  200. crypto_cipher_encrypt_init_cipher(or_circ->n_crypto);
  201. or_circ->p_digest = crypto_digest_new();
  202. or_circ->n_digest = crypto_digest_new();
  203. reset_perftime();
  204. for (outbound = 0; outbound <= 1; ++outbound) {
  205. cell_direction_t d = outbound ? CELL_DIRECTION_OUT : CELL_DIRECTION_IN;
  206. start = perftime();
  207. for (i = 0; i < iters; ++i) {
  208. char recognized = 0;
  209. crypt_path_t *layer_hint = NULL;
  210. relay_crypt(TO_CIRCUIT(or_circ), cell, d, &layer_hint, &recognized);
  211. }
  212. end = perftime();
  213. printf("%sbound cells: %.2f ns per cell. (%.2f ns per byte of payload)\n",
  214. outbound?"Out":" In",
  215. NANOCOUNT(start,end,iters),
  216. NANOCOUNT(start,end,iters*CELL_PAYLOAD_SIZE));
  217. }
  218. crypto_digest_free(or_circ->p_digest);
  219. crypto_digest_free(or_circ->n_digest);
  220. crypto_cipher_free(or_circ->p_crypto);
  221. crypto_cipher_free(or_circ->n_crypto);
  222. tor_free(or_circ);
  223. tor_free(cell);
  224. }
  225. typedef void (*bench_fn)(void);
  226. typedef struct benchmark_t {
  227. const char *name;
  228. bench_fn fn;
  229. int enabled;
  230. } benchmark_t;
  231. #define ENT(s) { #s , bench_##s, 0 }
  232. static struct benchmark_t benchmarks[] = {
  233. ENT(dmap),
  234. ENT(aes),
  235. ENT(cell_aes),
  236. ENT(cell_ops),
  237. {NULL,NULL,0}
  238. };
  239. static benchmark_t *
  240. find_benchmark(const char *name)
  241. {
  242. benchmark_t *b;
  243. for (b = benchmarks; b->name; ++b) {
  244. if (!strcmp(name, b->name)) {
  245. return b;
  246. }
  247. }
  248. return NULL;
  249. }
  250. /** Main entry point for benchmark code: parse the command line, and run
  251. * some benchmarks. */
  252. int
  253. main(int argc, const char **argv)
  254. {
  255. int i;
  256. int list=0, n_enabled=0;
  257. benchmark_t *b;
  258. tor_threads_init();
  259. for (i = 1; i < argc; ++i) {
  260. if (!strcmp(argv[i], "--list")) {
  261. list = 1;
  262. } else {
  263. benchmark_t *b = find_benchmark(argv[i]);
  264. ++n_enabled;
  265. if (b) {
  266. b->enabled = 1;
  267. } else {
  268. printf("No such benchmark as %s\n", argv[i]);
  269. }
  270. }
  271. }
  272. reset_perftime();
  273. crypto_seed_rng(1);
  274. for (b = benchmarks; b->name; ++b) {
  275. if (b->enabled || n_enabled == 0) {
  276. printf("===== %s =====\n", b->name);
  277. if (!list)
  278. b->fn();
  279. }
  280. }
  281. return 0;
  282. }