[tor-commits] [tor/master] All-in-one benchmark test for cell crypto

nickm at torproject.org nickm at torproject.org
Fri Nov 18 23:42:24 UTC 2011


commit ce51887291decd1654fed6e745fb8f625bf52292
Author: Nick Mathewson <nickm at torproject.org>
Date:   Fri Nov 11 13:06:17 2011 -0500

    All-in-one benchmark test for cell crypto
---
 src/or/relay.c   |    6 +---
 src/or/relay.h   |    5 ++++
 src/test/bench.c |   59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 66 insertions(+), 4 deletions(-)

diff --git a/src/or/relay.c b/src/or/relay.c
index 51a29a2..ac3114b 100644
--- a/src/or/relay.c
+++ b/src/or/relay.c
@@ -11,6 +11,7 @@
  **/
 
 #include <math.h>
+#define RELAY_PRIVATE
 #include "or.h"
 #include "buffers.h"
 #include "circuitbuild.h"
@@ -33,9 +34,6 @@
 #include "routerlist.h"
 #include "routerparse.h"
 
-static int relay_crypt(circuit_t *circ, cell_t *cell,
-                       cell_direction_t cell_direction,
-                       crypt_path_t **layer_hint, char *recognized);
 static edge_connection_t *relay_lookup_conn(circuit_t *circ, cell_t *cell,
                                             cell_direction_t cell_direction,
                                             crypt_path_t *layer_hint);
@@ -297,7 +295,7 @@ circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
  * Return -1 to indicate that we should mark the circuit for close,
  * else return 0.
  */
-static int
+int
 relay_crypt(circuit_t *circ, cell_t *cell, cell_direction_t cell_direction,
             crypt_path_t **layer_hint, char *recognized)
 {
diff --git a/src/or/relay.h b/src/or/relay.h
index 7fce8ed..1cd4008 100644
--- a/src/or/relay.h
+++ b/src/or/relay.h
@@ -66,5 +66,10 @@ void circuit_clear_cell_queue(circuit_t *circ, or_connection_t *orconn);
 
 void tor_gettimeofday_cache_clear(void);
 
+#ifdef RELAY_PRIVATE
+int relay_crypt(circuit_t *circ, cell_t *cell, cell_direction_t cell_direction,
+                crypt_path_t **layer_hint, char *recognized);
+#endif
+
 #endif
 
diff --git a/src/test/bench.c b/src/test/bench.c
index 634bf93..ff2794e 100644
--- a/src/test/bench.c
+++ b/src/test/bench.c
@@ -14,7 +14,10 @@ const char tor_git_revision[] = "";
 
 #include "orconfig.h"
 
+#define RELAY_PRIVATE
+
 #include "or.h"
+#include "relay.h"
 
 #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_PROCESS_CPUTIME_ID)
 static uint64_t nanostart;
@@ -199,6 +202,59 @@ bench_dmap(void)
   smartlist_free(sl2);
 }
 
+static void
+bench_cell_ops(void)
+{
+  const int iters = 1<<16;
+  int i;
+
+  /* benchmarks for cell ops at relay. */
+  or_circuit_t *or_circ = tor_malloc_zero(sizeof(or_circuit_t));
+  cell_t *cell = tor_malloc(sizeof(cell_t));
+  int outbound;
+  uint64_t start, end;
+
+  crypto_rand((char*)cell->payload, sizeof(cell->payload));
+
+  /* Mock-up or_circuit_t */
+  or_circ->_base.magic = OR_CIRCUIT_MAGIC;
+  or_circ->_base.purpose = CIRCUIT_PURPOSE_OR;
+
+  /* Initialize crypto */
+  or_circ->p_crypto = crypto_new_cipher_env();
+  crypto_cipher_generate_key(or_circ->p_crypto);
+  crypto_cipher_encrypt_init_cipher(or_circ->p_crypto);
+  or_circ->n_crypto = crypto_new_cipher_env();
+  crypto_cipher_generate_key(or_circ->n_crypto);
+  crypto_cipher_encrypt_init_cipher(or_circ->n_crypto);
+  or_circ->p_digest = crypto_new_digest_env();
+  or_circ->n_digest = crypto_new_digest_env();
+
+  reset_perftime();
+
+  for (outbound = 0; outbound <= 1; ++outbound) {
+    cell_direction_t d = outbound ? CELL_DIRECTION_OUT : CELL_DIRECTION_IN;
+    start = perftime();
+    for (i = 0; i < iters; ++i) {
+      char recognized = 0;
+      crypt_path_t *layer_hint = NULL;
+      relay_crypt(TO_CIRCUIT(or_circ), cell, d, &layer_hint, &recognized);
+    }
+    end = perftime();
+    printf("%sbound cells: %.2f ns per cell. (%.2f ns per byte of payload)\n",
+           outbound?"Out":" In",
+           NANOCOUNT(start,end,iters),
+           NANOCOUNT(start,end,iters*CELL_PAYLOAD_SIZE));
+  }
+
+  crypto_free_digest_env(or_circ->p_digest);
+  crypto_free_digest_env(or_circ->n_digest);
+  crypto_free_cipher_env(or_circ->p_crypto);
+  crypto_free_cipher_env(or_circ->n_crypto);
+  tor_free(or_circ);
+  tor_free(cell);
+}
+
 typedef void (*bench_fn)(void);
 
 typedef struct benchmark_t {
@@ -213,6 +269,7 @@ static struct benchmark_t benchmarks[] = {
   ENT(dmap),
   ENT(aes),
   ENT(cell_aes),
+  ENT(cell_ops),
   {NULL,NULL,0}
 };
 
@@ -255,6 +312,8 @@ main(int argc, const char **argv)
 
   reset_perftime();
 
+  crypto_seed_rng(1);
+
   for (b = benchmarks; b->name; ++b) {
     if (b->enabled || n_enabled == 0) {
       printf("===== %s =====\n", b->name);





More information about the tor-commits mailing list