From 93ad89e9d219d6cea764652a05c236210c7de3fa Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 25 Sep 2013 11:36:02 -0400 Subject: [PATCH] Rename bench_workqueue -> test_workqueue and make it a unit test. --- .gitignore | 2 + src/test/include.am | 19 ++-- .../{bench_workqueue.c => test_workqueue.c} | 90 +++++++++++++++---- 3 files changed, 85 insertions(+), 26 deletions(-) rename src/test/{bench_workqueue.c => test_workqueue.c} (71%) diff --git a/.gitignore b/.gitignore index 9ddd0c5385..e63576cfd4 100644 --- a/.gitignore +++ b/.gitignore @@ -163,10 +163,12 @@ cscope.* /src/test/test-bt-cl /src/test/test-child /src/test/test-ntor-cl +/src/test/test_workqueue /src/test/test.exe /src/test/test-bt-cl.exe /src/test/test-child.exe /src/test/test-ntor-cl.exe +/src/test/test_workqueue.exe # /src/tools/ /src/tools/tor-checkkey diff --git a/src/test/include.am b/src/test/include.am index 6ad1b552b7..2badc47a47 100644 --- a/src/test/include.am +++ b/src/test/include.am @@ -1,8 +1,8 @@ TESTS += src/test/test -noinst_PROGRAMS+= src/test/bench src/test/bench_workqueue +noinst_PROGRAMS+= src/test/bench if UNITTESTS_ENABLED -noinst_PROGRAMS+= src/test/test src/test/test-child +noinst_PROGRAMS+= src/test/test src/test/test-child src/test/test_workqueue endif src_test_AM_CPPFLAGS = -DSHARE_DATADIR="\"$(datadir)\"" \ @@ -62,8 +62,10 @@ src_test_test_CPPFLAGS= $(src_test_AM_CPPFLAGS) src_test_bench_SOURCES = \ src/test/bench.c -src_test_bench_workqueue_SOURCES = \ - src/test/bench_workqueue.c +src_test_test_workqueue_SOURCES = \ + src/test/test_workqueue.c +src_test_test_workqueue_CPPFLAGS= $(src_test_AM_CPPFLAGS) +src_test_test_workqueue_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS) src_test_test_LDFLAGS = @TOR_LDFLAGS_zlib@ @TOR_LDFLAGS_openssl@ \ @TOR_LDFLAGS_libevent@ @@ -83,11 +85,12 @@ src_test_bench_LDADD = src/or/libtor.a src/common/libor.a \ @TOR_OPENSSL_LIBS@ @TOR_LIB_WS32@ @TOR_LIB_GDI@ @CURVE25519_LIBS@ \ @TOR_SYSTEMD_LIBS@ -src_test_bench_workqueue_LDFLAGS = @TOR_LDFLAGS_zlib@ @TOR_LDFLAGS_openssl@ \ +src_test_test_workqueue_LDFLAGS = @TOR_LDFLAGS_zlib@ @TOR_LDFLAGS_openssl@ \ @TOR_LDFLAGS_libevent@ -src_test_bench_workqueue_LDADD = src/or/libtor.a src/common/libor.a \ - src/common/libor-crypto.a $(LIBDONNA) \ - src/common/libor-event.a \ +src_test_test_workqueue_LDADD = src/or/libtor-testing.a \ + src/common/libor-testing.a \ + src/common/libor-crypto-testing.a $(LIBDONNA) \ + src/common/libor-event-testing.a \ @TOR_ZLIB_LIBS@ @TOR_LIB_MATH@ @TOR_LIBEVENT_LIBS@ \ @TOR_OPENSSL_LIBS@ @TOR_LIB_WS32@ @TOR_LIB_GDI@ @CURVE25519_LIBS@ diff --git a/src/test/bench_workqueue.c b/src/test/test_workqueue.c similarity index 71% rename from src/test/bench_workqueue.c rename to src/test/test_workqueue.c index f190c613e5..4077fb27a8 100644 --- a/src/test/bench_workqueue.c +++ b/src/test/test_workqueue.c @@ -18,16 +18,19 @@ #include #endif +static int opt_verbose = 0; +static int opt_n_threads = 8; +static int opt_n_items = 10000; +static int opt_n_inflight = 1000; +static int opt_n_lowwater = 250; +static int opt_ratio_rsa = 5; + #ifdef TRACK_RESPONSES tor_mutex_t bitmap_mutex; int handled_len; bitarray_t *handled; #endif -#define N_ITEMS 10000 -#define N_INFLIGHT 1000 -#define RELAUNCH_AT 250 - typedef struct state_s { int magic; int n_handled; @@ -174,7 +177,9 @@ handle_reply(void *arg) static int add_work(threadpool_t *tp) { - int add_rsa = tor_weak_random_range(&weak_rng, 5) == 0; + int add_rsa = + opt_ratio_rsa == 0 || + tor_weak_random_range(&weak_rng, opt_ratio_rsa) == 0; if (add_rsa) { rsa_work_t *w = tor_malloc_zero(sizeof(*w)); w->serial = n_sent++; @@ -206,10 +211,11 @@ replysock_readable_cb(tor_socket_t sock, short what, void *arg) if (old_r == n_received) return; - printf("%d / %d\n", n_received, n_sent); + if (opt_verbose) + printf("%d / %d\n", n_received, n_sent); #ifdef TRACK_RESPONSES tor_mutex_acquire(&bitmap_mutex); - for (i = 0; i < N_ITEMS; ++i) { + for (i = 0; i < opt_n_items; ++i) { if (bitarray_is_set(received, i)) putc('o', stdout); else if (bitarray_is_set(handled, i)) @@ -221,8 +227,8 @@ replysock_readable_cb(tor_socket_t sock, short what, void *arg) tor_mutex_release(&bitmap_mutex); #endif - if (n_sent - n_received < RELAUNCH_AT) { - while (n_sent < n_received + N_INFLIGHT && n_sent < N_ITEMS) { + if (n_sent - n_received < opt_n_lowwater) { + while (n_sent < n_received + opt_n_inflight && n_sent < opt_n_items) { if (! add_work(tp)) { puts("Couldn't add work."); tor_event_base_loopexit(tor_libevent_get_base(), NULL); @@ -230,11 +236,23 @@ replysock_readable_cb(tor_socket_t sock, short what, void *arg) } } - if (n_received == n_sent && n_sent >= N_ITEMS) { + if (n_received == n_sent && n_sent >= opt_n_items) { tor_event_base_loopexit(tor_libevent_get_base(), NULL); } } +static void +help(void) +{ + puts( + "Options:\n" + " -N Run this many items of work\n" + " -T Use this many threads\n" + " -I Have no more than this many requests queued at once\n" + " -L Add items whenever fewer than this many are pending.\n" + " -R Make one out of this many items be a slow (RSA) one"); +} + int main(int argc, char **argv) { @@ -244,8 +262,33 @@ main(int argc, char **argv) tor_libevent_cfg evcfg; struct event *ev; - (void)argc; - (void)argv; + for (i = 1; i < argc; ++i) { + if (!strcmp(argv[i], "-v")) { + opt_verbose = 1; + } else if (!strcmp(argv[i], "-T") && i+1