benchmark-async.c
Go to the documentation of this 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 "task.h"
23 #include "uv.h"
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 
28 #define NUM_PINGS (1000 * 1000)
29 
30 struct ctx {
33  uv_async_t main_async; /* wake up main thread */
34  uv_async_t worker_async; /* wake up worker */
35  unsigned int nthreads;
36  unsigned int main_sent;
37  unsigned int main_seen;
38  unsigned int worker_sent;
39  unsigned int worker_seen;
40 };
41 
42 
44  struct ctx* ctx = container_of(handle, struct ctx, worker_async);
45 
47  ctx->worker_sent++;
48  ctx->worker_seen++;
49 
50  if (ctx->worker_sent >= NUM_PINGS)
52 }
53 
54 
56  struct ctx* ctx = container_of(handle, struct ctx, main_async);
57 
59  ctx->main_sent++;
60  ctx->main_seen++;
61 
62  if (ctx->main_sent >= NUM_PINGS)
63  uv_close((uv_handle_t*) &ctx->main_async, NULL);
64 }
65 
66 
67 static void worker(void* arg) {
68  struct ctx* ctx = arg;
72 }
73 
74 
75 static int test_async(int nthreads) {
76  struct ctx* threads;
77  struct ctx* ctx;
78  uint64_t time;
79  int i;
80 
81  threads = calloc(nthreads, sizeof(threads[0]));
82  ASSERT(threads != NULL);
83 
84  for (i = 0; i < nthreads; i++) {
85  ctx = threads + i;
87  ASSERT(0 == uv_loop_init(&ctx->loop));
90  &ctx->main_async,
91  main_async_cb));
93  }
94 
95  time = uv_hrtime();
96 
98 
99  for (i = 0; i < nthreads; i++)
101 
102  time = uv_hrtime() - time;
103 
104  for (i = 0; i < nthreads; i++) {
105  ctx = threads + i;
108  ASSERT(ctx->main_sent == (unsigned int) NUM_PINGS);
109  ASSERT(ctx->main_seen == (unsigned int) NUM_PINGS);
110  }
111 
112  printf("async%d: %.2f sec (%s/sec)\n",
113  nthreads,
114  time / 1e9,
115  fmt(NUM_PINGS / (time / 1e9)));
116 
117  free(threads);
118 
120  return 0;
121 }
122 
123 
124 BENCHMARK_IMPL(async1) {
125  return test_async(1);
126 }
127 
128 
129 BENCHMARK_IMPL(async2) {
130  return test_async(2);
131 }
132 
133 
134 BENCHMARK_IMPL(async4) {
135  return test_async(4);
136 }
137 
138 
139 BENCHMARK_IMPL(async8) {
140  return test_async(8);
141 }
task.h
ctx
Definition: benchmark-async.c:30
BENCHMARK_IMPL
BENCHMARK_IMPL(async1)
Definition: benchmark-async.c:124
NUM_PINGS
#define NUM_PINGS
Definition: benchmark-async.c:28
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
ASSERT
#define ASSERT(expr)
Definition: task.h:102
ctx
static struct test_ctx ctx
Definition: test-ipc-send-recv.c:65
uv_thread_join
UV_EXTERN int uv_thread_join(uv_thread_t *tid)
Definition: libuv/src/unix/thread.c:271
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
container_of
#define container_of(ptr, type, member)
Definition: uv-common.h:57
threads
static uv_thread_t * threads
Definition: threadpool.c:38
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
worker
static void worker(void *arg)
Definition: benchmark-async.c:67
uv_loop_close
UV_EXTERN int uv_loop_close(uv_loop_t *loop)
Definition: uv-common.c:761
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
uv_async_s
Definition: uv.h:834
worker_async_cb
static void worker_async_cb(uv_async_t *handle)
Definition: benchmark-async.c:43
ctx::main_sent
unsigned int main_sent
Definition: benchmark-async.c:36
worker
Definition: worker.py:1
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
test_async
static int test_async(int nthreads)
Definition: benchmark-async.c:75
arg
Definition: cmdline.cc:40
nthreads
static unsigned int nthreads
Definition: threadpool.c:37
uv_loop_init
UV_EXTERN int uv_loop_init(uv_loop_t *loop)
Definition: loop.c:30
uv_thread_create
UV_EXTERN int uv_thread_create(uv_thread_t *tid, uv_thread_cb entry, void *arg)
Definition: libuv/src/unix/thread.c:209
main_async_cb
static void main_async_cb(uv_async_t *handle)
Definition: benchmark-async.c:55
ctx::worker_seen
unsigned int worker_seen
Definition: benchmark-async.c:39
uv.h
ctx::loop
uv_loop_t loop
Definition: benchmark-async.c:31
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
testing::internal::fmt
GTEST_API_ const char * fmt
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1808
uv_async_init
UV_EXTERN int uv_async_init(uv_loop_t *, uv_async_t *async, uv_async_cb async_cb)
Definition: unix/async.c:44
ctx::main_async
uv_async_t main_async
Definition: benchmark-async.c:33
uv_hrtime
UV_EXTERN uint64_t uv_hrtime(void)
Definition: unix/core.c:107
ctx::main_seen
unsigned int main_seen
Definition: benchmark-async.c:37
arg
struct arg arg
ctx::worker_async
uv_async_t worker_async
Definition: benchmark-async.c:34
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
uv_thread_t
pthread_t uv_thread_t
Definition: unix.h:134
uv_loop_s
Definition: uv.h:1767
ctx::worker_sent
unsigned int worker_sent
Definition: benchmark-async.c:38
thread
static uv_thread_t thread
Definition: test-async-null-cb.c:29
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
uv_async_send
UV_EXTERN int uv_async_send(uv_async_t *async)
Definition: unix/async.c:62
ctx::nthreads
unsigned int nthreads
Definition: benchmark-async.c:35
ctx::thread
uv_thread_t thread
Definition: benchmark-async.c:32


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:36