Back to index

tor  0.2.3.18-rc
Classes | Defines | Typedefs | Functions | Variables
bench.c File Reference

Benchmarks for lower level Tor modules. More...

#include "orconfig.h"
#include "or.h"
#include "relay.h"

Go to the source code of this file.

Classes

struct  benchmark_t

Defines

#define RELAY_PRIVATE
#define NANOCOUNT(start, end, iters)   ( ((double)((end)-(start))) / (iters) )
#define ENT(s)   { #s , bench_##s, 0 }

Typedefs

typedef void(* bench_fn )(void)
typedef struct benchmark_t benchmark_t

Functions

static void reset_perftime (void)
static uint64_t perftime (void)
static void bench_aes (void)
 Run AES performance benchmarks.
static void bench_cell_aes (void)
static void bench_dmap (void)
 Run digestmap_t performance benchmarks.
static void bench_cell_ops (void)
static benchmark_tfind_benchmark (const char *name)
int main (int argc, const char **argv)
 Main entry point for benchmark code: parse the command line, and run some benchmarks.

Variables

const char tor_git_revision [] = ""
 String describing which Tor subversion repository version the source was built from.
static struct timeval = { 0, 0 }

Detailed Description

Benchmarks for lower level Tor modules.

Definition in file bench.c.


Class Documentation

struct benchmark_t

Definition at line 253 of file bench.c.

Class Members
int enabled
bench_fn fn
const char * name

Define Documentation

#define ENT (   s)    { #s , bench_##s, 0 }

Definition at line 259 of file bench.c.

#define NANOCOUNT (   start,
  end,
  iters 
)    ( ((double)((end)-(start))) / (iters) )

Definition at line 67 of file bench.c.

#define RELAY_PRIVATE

Definition at line 17 of file bench.c.


Typedef Documentation

typedef void(* bench_fn)(void)

Definition at line 251 of file bench.c.

static struct benchmark_t
Initial value:
 {
  ENT(dmap),
  ENT(aes),
  ENT(cell_aes),
  ENT(cell_ops),
  {NULL,NULL,0}
}

Definition at line 261 of file bench.c.


Function Documentation

static void bench_aes ( void  ) [static]

Run AES performance benchmarks.

Definition at line 72 of file bench.c.

{
  int len, i;
  char *b1, *b2;
  crypto_cipher_t *c;
  uint64_t start, end;
  const int bytes_per_iter = (1<<24);
  reset_perftime();
  c = crypto_cipher_new(NULL);

  for (len = 1; len <= 8192; len *= 2) {
    int iters = bytes_per_iter / len;
    b1 = tor_malloc_zero(len);
    b2 = tor_malloc_zero(len);
    start = perftime();
    for (i = 0; i < iters; ++i) {
      crypto_cipher_encrypt(c, b1, b2, len);
    }
    end = perftime();
    tor_free(b1);
    tor_free(b2);
    printf("%d bytes: %.2f nsec per byte\n", len,
           NANOCOUNT(start, end, iters*len));
  }
  crypto_cipher_free(c);
}

Here is the call graph for this function:

static void bench_cell_aes ( void  ) [static]

Definition at line 100 of file bench.c.

{
  uint64_t start, end;
  const int len = 509;
  const int iters = (1<<16);
  const int max_misalign = 15;
  char *b = tor_malloc(len+max_misalign);
  crypto_cipher_t *c;
  int i, misalign;

  c = crypto_cipher_new(NULL);

  reset_perftime();
  for (misalign = 0; misalign <= max_misalign; ++misalign) {
    start = perftime();
    for (i = 0; i < iters; ++i) {
      crypto_cipher_crypt_inplace(c, b+misalign, len);
    }
    end = perftime();
    printf("%d bytes, misaligned by %d: %.2f nsec per byte\n", len, misalign,
           NANOCOUNT(start, end, iters*len));
  }

  crypto_cipher_free(c);
  tor_free(b);
}

Here is the call graph for this function:

static void bench_cell_ops ( void  ) [static]

Definition at line 203 of file bench.c.

{
  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_cipher_new(NULL);
  or_circ->n_crypto = crypto_cipher_new(NULL);
  or_circ->p_digest = crypto_digest_new();
  or_circ->n_digest = crypto_digest_new();

  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_digest_free(or_circ->p_digest);
  crypto_digest_free(or_circ->n_digest);
  crypto_cipher_free(or_circ->p_crypto);
  crypto_cipher_free(or_circ->n_crypto);
  tor_free(or_circ);
  tor_free(cell);
}

Here is the call graph for this function:

static void bench_dmap ( void  ) [static]

Run digestmap_t performance benchmarks.

Definition at line 129 of file bench.c.

{
  smartlist_t *sl = smartlist_new();
  smartlist_t *sl2 = smartlist_new();
  uint64_t start, end, pt2, pt3, pt4;
  int iters = 8192;
  const int elts = 4000;
  const int fpostests = 100000;
  char d[20];
  int i,n=0, fp = 0;
  digestmap_t *dm = digestmap_new();
  digestset_t *ds = digestset_new(elts);

  for (i = 0; i < elts; ++i) {
    crypto_rand(d, 20);
    smartlist_add(sl, tor_memdup(d, 20));
  }
  for (i = 0; i < elts; ++i) {
    crypto_rand(d, 20);
    smartlist_add(sl2, tor_memdup(d, 20));
  }
  printf("nbits=%d\n", ds->mask+1);

  reset_perftime();

  start = perftime();
  for (i = 0; i < iters; ++i) {
    SMARTLIST_FOREACH(sl, const char *, cp, digestmap_set(dm, cp, (void*)1));
  }
  pt2 = perftime();
  printf("digestmap_set: %.2f ns per element\n",
         NANOCOUNT(start, pt2, iters*elts));

  for (i = 0; i < iters; ++i) {
    SMARTLIST_FOREACH(sl, const char *, cp, digestmap_get(dm, cp));
    SMARTLIST_FOREACH(sl2, const char *, cp, digestmap_get(dm, cp));
  }
  pt3 = perftime();
  printf("digestmap_get: %.2f ns per element\n",
         NANOCOUNT(pt2, pt3, iters*elts*2));

  for (i = 0; i < iters; ++i) {
    SMARTLIST_FOREACH(sl, const char *, cp, digestset_add(ds, cp));
  }
  pt4 = perftime();
  printf("digestset_add: %.2f ns per element\n",
         NANOCOUNT(pt3, pt4, iters*elts));

  for (i = 0; i < iters; ++i) {
    SMARTLIST_FOREACH(sl, const char *, cp, n += digestset_isin(ds, cp));
    SMARTLIST_FOREACH(sl2, const char *, cp, n += digestset_isin(ds, cp));
  }
  end = perftime();
  printf("digestset_isin: %.2f ns per element.\n",
         NANOCOUNT(pt4, end, iters*elts*2));
  /* We need to use this, or else the whole loop gets optimized out. */
  printf("Hits == %d\n", n);

  for (i = 0; i < fpostests; ++i) {
    crypto_rand(d, 20);
    if (digestset_isin(ds, d)) ++fp;
  }
  printf("False positive rate on digestset: %.2f%%\n",
         (fp/(double)fpostests)*100);

  digestmap_free(dm, NULL);
  digestset_free(ds);
  SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp));
  smartlist_free(sl);
  smartlist_free(sl2);
}

Here is the call graph for this function:

static benchmark_t* find_benchmark ( const char *  name) [static]

Definition at line 270 of file bench.c.

{
  benchmark_t *b;
  for (b = benchmarks; b->name; ++b) {
    if (!strcmp(name, b->name)) {
      return b;
    }
  }
  return NULL;
}

Here is the caller graph for this function:

int main ( int  argc,
const char **  argv 
)

Main entry point for benchmark code: parse the command line, and run some benchmarks.

Definition at line 284 of file bench.c.

{
  int i;
  int list=0, n_enabled=0;
  benchmark_t *b;

  tor_threads_init();

  for (i = 1; i < argc; ++i) {
    if (!strcmp(argv[i], "--list")) {
      list = 1;
    } else {
      benchmark_t *b = find_benchmark(argv[i]);
      ++n_enabled;
      if (b) {
        b->enabled = 1;
      } else {
        printf("No such benchmark as %s\n", argv[i]);
      }
    }
  }

  reset_perftime();

  crypto_seed_rng(1);

  for (b = benchmarks; b->name; ++b) {
    if (b->enabled || n_enabled == 0) {
      printf("===== %s =====\n", b->name);
      if (!list)
        b->fn();
    }
  }

  return 0;
}

Here is the call graph for this function:

static uint64_t perftime ( void  ) [static]

Definition at line 58 of file bench.c.

{
  struct timeval now, out;
  tor_gettimeofday(&now);
  timersub(&now, &tv_start, &out);
  return ((uint64_t)out.tv_sec)*1000000000 + out.tv_usec*1000;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void reset_perftime ( void  ) [static]

Definition at line 53 of file bench.c.

{
  tor_gettimeofday(&tv_start);
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

struct timeval = { 0, 0 } [static]

Definition at line 51 of file bench.c.

const char tor_git_revision[] = ""

String describing which Tor subversion repository version the source was built from.

This string is generated by a bit of shell kludging int src/or/Makefile.am, and is usually right.

Definition at line 8 of file bench.c.