xref: /libuv/test/benchmark-getaddrinfo.c (revision 011a1ac1)
1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21 
22 #include "uv.h"
23 #include "task.h"
24 #include <stdlib.h>
25 
26 #define CONCURRENT_CALLS 10
27 #define TOTAL_CALLS 10000
28 
29 static const char* name = "localhost";
30 
31 static uv_loop_t* loop;
32 
33 static uv_getaddrinfo_t handles[CONCURRENT_CALLS];
34 
35 static int calls_initiated = 0;
36 static int calls_completed = 0;
37 static int64_t start_time;
38 static int64_t end_time;
39 
40 
41 static void getaddrinfo_initiate(uv_getaddrinfo_t* handle);
42 
43 
getaddrinfo_cb(uv_getaddrinfo_t * handle,int status,struct addrinfo * res)44 static void getaddrinfo_cb(uv_getaddrinfo_t* handle, int status,
45     struct addrinfo* res) {
46   ASSERT_OK(status);
47   calls_completed++;
48   if (calls_initiated < TOTAL_CALLS) {
49     getaddrinfo_initiate(handle);
50   }
51 
52   uv_freeaddrinfo(res);
53 }
54 
55 
getaddrinfo_initiate(uv_getaddrinfo_t * handle)56 static void getaddrinfo_initiate(uv_getaddrinfo_t* handle) {
57   int r;
58 
59   calls_initiated++;
60 
61   r = uv_getaddrinfo(loop, handle, &getaddrinfo_cb, name, NULL, NULL);
62   ASSERT_OK(r);
63 }
64 
65 
BENCHMARK_IMPL(getaddrinfo)66 BENCHMARK_IMPL(getaddrinfo) {
67   int i;
68 
69   loop = uv_default_loop();
70 
71   uv_update_time(loop);
72   start_time = uv_now(loop);
73 
74   for (i = 0; i < CONCURRENT_CALLS; i++) {
75     getaddrinfo_initiate(&handles[i]);
76   }
77 
78   uv_run(loop, UV_RUN_DEFAULT);
79 
80   uv_update_time(loop);
81   end_time = uv_now(loop);
82 
83   ASSERT_EQ(calls_initiated, TOTAL_CALLS);
84   ASSERT_EQ(calls_completed, TOTAL_CALLS);
85 
86   fprintf(stderr, "getaddrinfo: %.0f req/s\n",
87           (double) calls_completed / (double) (end_time - start_time) * 1000.0);
88   fflush(stderr);
89 
90   MAKE_VALGRIND_HAPPY(loop);
91   return 0;
92 }
93