Skip to content

Commit

Permalink
bench: add 'million async handles' benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Dec 24, 2012
1 parent 1c722d6 commit 5af43ba
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/benchmark-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ BENCHMARK_DECLARE (async_pummel_4)
BENCHMARK_DECLARE (async_pummel_8)
BENCHMARK_DECLARE (spawn)
BENCHMARK_DECLARE (thread_create)
BENCHMARK_DECLARE (million_async)
BENCHMARK_DECLARE (million_timers)
HELPER_DECLARE (tcp4_blackhole_server)
HELPER_DECLARE (tcp_pump_server)
Expand Down Expand Up @@ -157,5 +158,6 @@ TASK_LIST_START

BENCHMARK_ENTRY (spawn)
BENCHMARK_ENTRY (thread_create)
BENCHMARK_ENTRY (million_async)
BENCHMARK_ENTRY (million_timers)
TASK_LIST_END
112 changes: 112 additions & 0 deletions test/benchmark-million-async.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

#include "task.h"
#include "uv.h"

struct async_container {
unsigned async_events;
unsigned handles_seen;
uv_async_t async_handles[1024 * 1024];
};

static volatile int done;
static uv_thread_t thread_id;
static struct async_container* container;


static unsigned fastrand(void) {
static unsigned g = 0;
g = g * 214013 + 2531011;
return g;
}


static void thread_cb(void* arg) {
unsigned i;

while (done == 0) {
i = fastrand() % ARRAY_SIZE(container->async_handles);
uv_async_send(container->async_handles + i);
}
}


static void async_cb(uv_async_t* handle, int status) {
container->async_events++;
handle->data = handle;
}


static void timer_cb(uv_timer_t* handle, int status) {
unsigned i;

done = 1;
ASSERT(0 == uv_thread_join(&thread_id));

for (i = 0; i < ARRAY_SIZE(container->async_handles); i++) {
uv_async_t* handle = container->async_handles + i;

if (handle->data != NULL)
container->handles_seen++;

uv_close((uv_handle_t*) handle, NULL);
}

uv_close((uv_handle_t*) handle, NULL);
}


BENCHMARK_IMPL(million_async) {
uv_timer_t timer_handle;
uv_async_t* handle;
uv_loop_t* loop;
int timeout;
unsigned i;

loop = uv_default_loop();
timeout = 5000;

container = malloc(sizeof(*container));
ASSERT(container != NULL);
container->async_events = 0;
container->handles_seen = 0;

for (i = 0; i < ARRAY_SIZE(container->async_handles); i++) {
handle = container->async_handles + i;
ASSERT(0 == uv_async_init(loop, handle, async_cb));
handle->data = NULL;
}

ASSERT(0 == uv_timer_init(loop, &timer_handle));
ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, timeout, 0));
ASSERT(0 == uv_thread_create(&thread_id, thread_cb, NULL));
ASSERT(0 == uv_run(loop));
printf("%s async events in %.1f seconds (%s/s, %s unique handles seen)\n",
fmt(container->async_events),
timeout / 1000.,
fmt(container->async_events / (timeout / 1000.)),
fmt(container->handles_seen));
free(container);

MAKE_VALGRIND_HAPPY();
return 0;
}
1 change: 1 addition & 0 deletions uv.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@
'test/benchmark-getaddrinfo.c',
'test/benchmark-list.h',
'test/benchmark-loop-count.c',
'test/benchmark-million-async.c',
'test/benchmark-million-timers.c',
'test/benchmark-multi-accept.c',
'test/benchmark-ping-pongs.c',
Expand Down

0 comments on commit 5af43ba

Please sign in to comment.