summaryrefslogtreecommitdiff
path: root/deps/uv/test/benchmark-ares.c
blob: 27084fae209704f1872c9bc6e2425df09458488a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* 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 "uv.h"
#include "task.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h> /* strlen */

static uv_loop_t* loop;

static ares_channel channel;
static struct ares_options options;
static int optmask;

static int ares_callbacks;
static int ares_errors;
static int argument;

#define NUM_CALLS_TO_START    1000

static int64_t start_time;
static int64_t end_time;


/* callback method. */
static void aresbynamecallback(void *arg,
                               int status,
                               int timeouts,
                               struct hostent *hostent) {
  ares_callbacks++;
  if (status != 0) {
    ares_errors++;
  }
}


static void prep_tcploopback()
{
  /* for test, use echo server - TCP port TEST_PORT on loopback */
  struct sockaddr_in test_server = uv_ip4_addr("127.0.0.1", 0);
  int rc = 0;
  optmask = 0;

  optmask = ARES_OPT_SERVERS | ARES_OPT_TCP_PORT | ARES_OPT_FLAGS;
  options.servers = &test_server.sin_addr;
  options.nservers = 1;
  options.tcp_port = htons(TEST_PORT_2);
  options.flags = ARES_FLAG_USEVC;

  rc = uv_ares_init_options(loop, &channel, &options, optmask);

  ASSERT(rc == ARES_SUCCESS);
}


BENCHMARK_IMPL(gethostbyname) {

  int rc = 0;
  int ares_start;;

  rc = ares_library_init(ARES_LIB_INIT_ALL);
  if (rc != 0) {
    printf("ares library init fails %d\n", rc);
    return 1;
  }

  loop = uv_default_loop();

  ares_callbacks = 0;
  ares_errors = 0;

  start_time = uv_hrtime();

  prep_tcploopback();

  for (ares_start = 0; ares_start < NUM_CALLS_TO_START; ares_start++) {
    ares_gethostbyname(channel,
                       "echos.srv",
                       AF_INET,
                       &aresbynamecallback,
                       &argument);
  }

  uv_run(loop);

  uv_ares_destroy(loop, channel);

  end_time = uv_hrtime();

  if (ares_errors > 0) {
    printf("There were %d failures\n", ares_errors);
  }
  LOGF("ares_gethostbyname: %.0f req/s\n",
       1e9 * ares_callbacks / (double)(end_time - start_time));

  return 0;
}