Blame test/benchmark-getaddrinfo.c

Packit b5b901
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
Packit b5b901
 *
Packit b5b901
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit b5b901
 * of this software and associated documentation files (the "Software"), to
Packit b5b901
 * deal in the Software without restriction, including without limitation the
Packit b5b901
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit b5b901
 * sell copies of the Software, and to permit persons to whom the Software is
Packit b5b901
 * furnished to do so, subject to the following conditions:
Packit b5b901
 *
Packit b5b901
 * The above copyright notice and this permission notice shall be included in
Packit b5b901
 * all copies or substantial portions of the Software.
Packit b5b901
 *
Packit b5b901
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit b5b901
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit b5b901
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit b5b901
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit b5b901
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit b5b901
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit b5b901
 * IN THE SOFTWARE.
Packit b5b901
 */
Packit b5b901
Packit b5b901
#include "uv.h"
Packit b5b901
#include "task.h"
Packit b5b901
#include <stdlib.h>
Packit b5b901
Packit b5b901
#define CONCURRENT_CALLS 10
Packit b5b901
#define TOTAL_CALLS 10000
Packit b5b901
Packit b5b901
static const char* name = "localhost";
Packit b5b901
Packit b5b901
static uv_loop_t* loop;
Packit b5b901
Packit b5b901
static uv_getaddrinfo_t handles[CONCURRENT_CALLS];
Packit b5b901
Packit b5b901
static int calls_initiated = 0;
Packit b5b901
static int calls_completed = 0;
Packit b5b901
static int64_t start_time;
Packit b5b901
static int64_t end_time;
Packit b5b901
Packit b5b901
Packit b5b901
static void getaddrinfo_initiate(uv_getaddrinfo_t* handle);
Packit b5b901
Packit b5b901
Packit b5b901
static void getaddrinfo_cb(uv_getaddrinfo_t* handle, int status,
Packit b5b901
    struct addrinfo* res) {
Packit b5b901
  ASSERT(status == 0);
Packit b5b901
  calls_completed++;
Packit b5b901
  if (calls_initiated < TOTAL_CALLS) {
Packit b5b901
    getaddrinfo_initiate(handle);
Packit b5b901
  }
Packit b5b901
Packit b5b901
  uv_freeaddrinfo(res);
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static void getaddrinfo_initiate(uv_getaddrinfo_t* handle) {
Packit b5b901
  int r;
Packit b5b901
Packit b5b901
  calls_initiated++;
Packit b5b901
Packit b5b901
  r = uv_getaddrinfo(loop, handle, &getaddrinfo_cb, name, NULL, NULL);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
BENCHMARK_IMPL(getaddrinfo) {
Packit b5b901
  int i;
Packit b5b901
Packit b5b901
  loop = uv_default_loop();
Packit b5b901
Packit b5b901
  uv_update_time(loop);
Packit b5b901
  start_time = uv_now(loop);
Packit b5b901
Packit b5b901
  for (i = 0; i < CONCURRENT_CALLS; i++) {
Packit b5b901
    getaddrinfo_initiate(&handles[i]);
Packit b5b901
  }
Packit b5b901
Packit b5b901
  uv_run(loop, UV_RUN_DEFAULT);
Packit b5b901
Packit b5b901
  uv_update_time(loop);
Packit b5b901
  end_time = uv_now(loop);
Packit b5b901
Packit b5b901
  ASSERT(calls_initiated == TOTAL_CALLS);
Packit b5b901
  ASSERT(calls_completed == TOTAL_CALLS);
Packit b5b901
Packit b5b901
  fprintf(stderr, "getaddrinfo: %.0f req/s\n",
Packit b5b901
          (double) calls_completed / (double) (end_time - start_time) * 1000.0);
Packit b5b901
  fflush(stderr);
Packit b5b901
Packit b5b901
  MAKE_VALGRIND_HAPPY();
Packit b5b901
  return 0;
Packit b5b901
}