bench.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. #include "or.h"
  14. #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_PROCESS_CPUTIME_ID)
  15. static uint64_t nanostart;
  16. static inline uint64_t
  17. timespec_to_nsec(const struct timespec *ts)
  18. {
  19. return ((uint64_t)ts->tv_sec)*1000000000 + ts->tv_nsec;
  20. }
  21. static void
  22. reset_perftime(void)
  23. {
  24. struct timespec ts;
  25. int r;
  26. r = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
  27. tor_assert(r == 0);
  28. nanostart = timespec_to_nsec(&ts);
  29. }
  30. static uint64_t
  31. 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. return timespec_to_nsec(&ts) - nanostart;
  38. }
  39. #else
  40. static struct timeval tv_start = { 0, 0 };
  41. static void
  42. reset_perftime(void)
  43. {
  44. tor_gettimeofday(&tv_start);
  45. }
  46. static uint64_t
  47. perftime(void)
  48. {
  49. struct timeval now, out;
  50. tor_gettimeofday(&now);
  51. timersub(&now, &tv_start, &out);
  52. return ((uint64_t)out.tv_sec)*1000000000 + out.tv_usec*1000;
  53. }
  54. #endif
  55. /** Run AES performance benchmarks. */
  56. static void
  57. bench_aes(void)
  58. {
  59. int len, i;
  60. char *b1, *b2;
  61. crypto_cipher_env_t *c;
  62. uint64_t start, end;
  63. const int bytes_per_iter = (1<<24);
  64. reset_perftime();
  65. c = crypto_new_cipher_env();
  66. crypto_cipher_generate_key(c);
  67. crypto_cipher_encrypt_init_cipher(c);
  68. for (len = 1; len <= 8192; len *= 2) {
  69. int iters = bytes_per_iter / len;
  70. b1 = tor_malloc_zero(len);
  71. b2 = tor_malloc_zero(len);
  72. start = perftime();
  73. for (i = 0; i < iters; ++i) {
  74. crypto_cipher_encrypt(c, b1, b2, len);
  75. }
  76. end = perftime();
  77. tor_free(b1);
  78. tor_free(b2);
  79. printf("%d bytes: %.2f nsec per byte\n", len,
  80. ((double)(end-start))/(iters*len));
  81. }
  82. crypto_free_cipher_env(c);
  83. }
  84. static void
  85. bench_cell_aes(void)
  86. {
  87. uint64_t start, end;
  88. const int len = 509;
  89. const int iters = (1<<16);
  90. const int max_misalign = 15;
  91. char *b = tor_malloc(len+max_misalign);
  92. crypto_cipher_env_t *c;
  93. int i, misalign;
  94. c = crypto_new_cipher_env();
  95. crypto_cipher_generate_key(c);
  96. crypto_cipher_encrypt_init_cipher(c);
  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. ((double)(end-start))/(iters*len));
  106. }
  107. crypto_free_cipher_env(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_create();
  115. smartlist_t *sl2 = smartlist_create();
  116. struct timeval start, end, pt2, pt3, pt4;
  117. const int iters = 10000;
  118. const int elts = 4000;
  119. const int fpostests = 1000000;
  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. tor_gettimeofday(&start);
  134. for (i = 0; i < iters; ++i) {
  135. SMARTLIST_FOREACH(sl, const char *, cp, digestmap_set(dm, cp, (void*)1));
  136. }
  137. tor_gettimeofday(&pt2);
  138. for (i = 0; i < iters; ++i) {
  139. SMARTLIST_FOREACH(sl, const char *, cp, digestmap_get(dm, cp));
  140. SMARTLIST_FOREACH(sl2, const char *, cp, digestmap_get(dm, cp));
  141. }
  142. tor_gettimeofday(&pt3);
  143. for (i = 0; i < iters; ++i) {
  144. SMARTLIST_FOREACH(sl, const char *, cp, digestset_add(ds, cp));
  145. }
  146. tor_gettimeofday(&pt4);
  147. for (i = 0; i < iters; ++i) {
  148. SMARTLIST_FOREACH(sl, const char *, cp, n += digestset_isin(ds, cp));
  149. SMARTLIST_FOREACH(sl2, const char *, cp, n += digestset_isin(ds, cp));
  150. }
  151. tor_gettimeofday(&end);
  152. for (i = 0; i < fpostests; ++i) {
  153. crypto_rand(d, 20);
  154. if (digestset_isin(ds, d)) ++fp;
  155. }
  156. printf("%ld\n",(unsigned long)tv_udiff(&start, &pt2));
  157. printf("%ld\n",(unsigned long)tv_udiff(&pt2, &pt3));
  158. printf("%ld\n",(unsigned long)tv_udiff(&pt3, &pt4));
  159. printf("%ld\n",(unsigned long)tv_udiff(&pt4, &end));
  160. printf("-- %d\n", n);
  161. printf("++ %f\n", fp/(double)fpostests);
  162. digestmap_free(dm, NULL);
  163. digestset_free(ds);
  164. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  165. SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp));
  166. smartlist_free(sl);
  167. smartlist_free(sl2);
  168. }
  169. typedef void (*bench_fn)(void);
  170. typedef struct benchmark_t {
  171. const char *name;
  172. bench_fn fn;
  173. int enabled;
  174. } benchmark_t;
  175. #define ENT(s) { #s , bench_##s, 0 }
  176. static struct benchmark_t benchmarks[] = {
  177. ENT(dmap),
  178. ENT(aes),
  179. ENT(cell_aes),
  180. {NULL,NULL,0}
  181. };
  182. static benchmark_t *
  183. find_benchmark(const char *name)
  184. {
  185. benchmark_t *b;
  186. for (b = benchmarks; b->name; ++b) {
  187. if (!strcmp(name, b->name)) {
  188. return b;
  189. }
  190. }
  191. return NULL;
  192. }
  193. /** Main entry point for benchmark code: parse the command line, and run
  194. * some benchmarks. */
  195. int
  196. main(int argc, const char **argv)
  197. {
  198. int i;
  199. int list=0, n_enabled=0;
  200. benchmark_t *b;
  201. tor_threads_init();
  202. for (i = 1; i < argc; ++i) {
  203. if (!strcmp(argv[i], "--list")) {
  204. list = 1;
  205. } else {
  206. benchmark_t *b = find_benchmark(argv[i]);
  207. ++n_enabled;
  208. if (b) {
  209. b->enabled = 1;
  210. } else {
  211. printf("No such benchmark as %s\n", argv[i]);
  212. }
  213. }
  214. }
  215. reset_perftime();
  216. for (b = benchmarks; b->name; ++b) {
  217. if (b->enabled || n_enabled == 0) {
  218. printf("===== %s =====\n", b->name);
  219. if (!list)
  220. b->fn();
  221. }
  222. }
  223. return 0;
  224. }