PageRenderTime 77ms CodeModel.GetById 37ms app.highlight 10ms RepoModel.GetById 14ms app.codeStats 4ms

/test/benchmark-getaddrinfo.c

http://github.com/joyent/libuv
C | 95 lines | 48 code | 27 blank | 20 comment | 6 complexity | 1ab198069ef1dbeb8734961c43dbefaf MD5 | raw file
 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
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h> /* strlen */
28
29
30#define CONCURRENT_CALLS 10
31#define TOTAL_CALLS 10000
32
33const char* name = "localhost";
34
35static uv_loop_t* loop;
36
37static uv_getaddrinfo_t handles[CONCURRENT_CALLS];
38
39static int calls_initiated = 0;
40static int calls_completed = 0;
41static int64_t start_time;
42static int64_t end_time;
43
44
45static void getaddrinfo_initiate(uv_getaddrinfo_t* handle);
46
47
48static void getaddrinfo_cb(uv_getaddrinfo_t* handle, int status,
49    struct addrinfo* res) {
50  ASSERT(status == 0);
51  calls_completed++;
52  if (calls_initiated < TOTAL_CALLS) {
53    getaddrinfo_initiate(handle);
54  }
55
56  uv_freeaddrinfo(res);
57}
58
59
60static void getaddrinfo_initiate(uv_getaddrinfo_t* handle) {
61  int r;
62
63  calls_initiated++;
64
65  r = uv_getaddrinfo(loop, handle, &getaddrinfo_cb, name, NULL, NULL);
66  ASSERT(r == 0);
67}
68
69
70BENCHMARK_IMPL(getaddrinfo) {
71  int i;
72
73  loop = uv_default_loop();
74
75  uv_update_time(loop);
76  start_time = uv_now(loop);
77
78  for (i = 0; i < CONCURRENT_CALLS; i++) {
79    getaddrinfo_initiate(&handles[i]);
80  }
81
82  uv_run(loop);
83
84  uv_update_time(loop);
85  end_time = uv_now(loop);
86
87  ASSERT(calls_initiated == TOTAL_CALLS);
88  ASSERT(calls_completed == TOTAL_CALLS);
89
90  LOGF("getaddrinfo: %.0f req/s\n",
91       (double) calls_completed / (double) (end_time - start_time) * 1000.0);
92
93  MAKE_VALGRIND_HAPPY();
94  return 0;
95}