]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Add benchmark to test onionskin performance.
authorNick Mathewson <nickm@torproject.org>
Wed, 28 Nov 2012 20:39:00 +0000 (15:39 -0500)
committerNick Mathewson <nickm@torproject.org>
Thu, 6 Dec 2012 06:54:09 +0000 (01:54 -0500)
src/test/bench.c

index 2e65d0b2d431c0fa25fc07e30a6140fa087035c2..cf8ba4a48e4d5c80f3206865f9516e684d5db0d1 100644 (file)
@@ -15,9 +15,12 @@ const char tor_git_revision[] = "";
 #include "orconfig.h"
 
 #define RELAY_PRIVATE
+#define CONFIG_PRIVATE
 
 #include "or.h"
+#include "onion.h"
 #include "relay.h"
+#include "config.h"
 
 #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_PROCESS_CPUTIME_ID)
 static uint64_t nanostart;
@@ -96,6 +99,66 @@ bench_aes(void)
   crypto_cipher_free(c);
 }
 
+static void
+bench_onion_TAP(void)
+{
+  const int iters = 1<<9;
+  int i;
+  crypto_pk_t *key, *key2;
+  uint64_t start, end;
+  char os[ONIONSKIN_CHALLENGE_LEN];
+  char or[ONIONSKIN_REPLY_LEN];
+  crypto_dh_t *dh_out;
+
+  key = crypto_pk_new();
+  key2 = crypto_pk_new();
+  crypto_pk_generate_key_with_bits(key, 1024);
+  crypto_pk_generate_key_with_bits(key2, 1024);
+
+  reset_perftime();
+  start = perftime();
+  for (i = 0; i < iters; ++i) {
+    onion_skin_create(key, &dh_out, os);
+    crypto_dh_free(dh_out);
+  }
+  end = perftime();
+  printf("Client-side, part 1: %f msec.\n", NANOCOUNT(start, end, iters)/1e6);
+
+  onion_skin_create(key, &dh_out, os);
+  start = perftime();
+  for (i = 0; i < iters; ++i) {
+    char key_out[CPATH_KEY_MATERIAL_LEN];
+    onion_skin_server_handshake(os, key, NULL, or, key_out, sizeof(key_out));
+  }
+  end = perftime();
+  printf("Server-side, key guessed right: %f msec\n",
+         NANOCOUNT(start, end, iters)/1e6);
+
+  start = perftime();
+  for (i = 0; i < iters; ++i) {
+    char key_out[CPATH_KEY_MATERIAL_LEN];
+    onion_skin_server_handshake(os, key2, key, or, key_out, sizeof(key_out));
+  }
+  end = perftime();
+  printf("Server-side, key guessed wrong: %f msec.\n",
+         NANOCOUNT(start, end, iters)/1e6);
+
+  start = perftime();
+  for (i = 0; i < iters; ++i) {
+    crypto_dh_t *dh;
+    char key_out[CPATH_KEY_MATERIAL_LEN];
+    int s;
+    dh = crypto_dh_dup(dh_out);
+    s = onion_skin_client_handshake(dh, or, key_out, sizeof(key_out));
+    tor_assert(s == 0);
+  }
+  end = perftime();
+  printf("Client-side, part 2: %f msec.\n",
+         NANOCOUNT(start, end, iters)/1e6);
+
+  crypto_pk_free(key);
+}
+
 static void
 bench_cell_aes(void)
 {
@@ -261,6 +324,7 @@ typedef struct benchmark_t {
 static struct benchmark_t benchmarks[] = {
   ENT(dmap),
   ENT(aes),
+  ENT(onion_TAP),
   ENT(cell_aes),
   ENT(cell_ops),
   {NULL,NULL,0}
@@ -286,6 +350,8 @@ main(int argc, const char **argv)
   int i;
   int list=0, n_enabled=0;
   benchmark_t *b;
+  char *errmsg;
+  or_options_t *options;
 
   tor_threads_init();
 
@@ -306,6 +372,16 @@ main(int argc, const char **argv)
   reset_perftime();
 
   crypto_seed_rng(1);
+  options = options_new();
+  init_logging();
+  options->command = CMD_RUN_UNITTESTS;
+  options->DataDirectory = tor_strdup("");
+  options_init(options);
+  if (set_options(options, &errmsg) < 0) {
+    printf("Failed to set initial options: %s\n", errmsg);
+    tor_free(errmsg);
+    return 1;
+  }
 
   for (b = benchmarks; b->name; ++b) {
     if (b->enabled || n_enabled == 0) {