bench.c 7.5 KB

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