test fiber in reactor way

This commit is contained in:
zhengshuxin 2020-02-09 21:50:37 +08:00
parent f4c063ebc3
commit cf6aab9e9b
3 changed files with 62 additions and 11 deletions

View File

@ -52,11 +52,13 @@ static void echo_client(SOCKET fd)
if (!__read_data) {
__total_count++;
if (i % 10000 == 0)
if (i % 10000 == 0) {
printf("fiber-%d: total %lld, curr %d\r\n",
acl_fiber_self(), __total_count, i);
if (__total_count % 10000 == 0)
}
if (__total_count % 10000 == 0) {
acl_fiber_yield();
}
continue;
}
@ -93,8 +95,9 @@ static void fiber_connect(ACL_FIBER *fiber acl_unused, void *ctx acl_unused)
sa.sin_port = htons(__server_port);
sa.sin_addr.s_addr = inet_addr(__server_ip);
if (__fiber_delay > 0)
if (__fiber_delay > 0) {
acl_fiber_delay(__fiber_delay);
}
#if defined(_WIN32) || defined(_WIN64)
if (acl_fiber_connect(fd, (const struct sockaddr *) &sa, len) < 0) {
@ -134,7 +137,7 @@ static void fiber_connect(ACL_FIBER *fiber acl_unused, void *ctx acl_unused)
__total_count, spent,
(__total_count * 1000) / (spent > 0 ? spent : 1));
acl_fiber_schedule_stop();
//acl_fiber_schedule_stop();
}
}
@ -142,8 +145,9 @@ static void fiber_main(ACL_FIBER *fiber acl_unused, void *ctx acl_unused)
{
int i;
for (i = 0; i < __max_fibers; i++)
for (i = 0; i < __max_fibers; i++) {
acl_fiber_create(fiber_connect, NULL, __stack_size);
}
}
static void usage(const char *procname)

View File

@ -110,8 +110,10 @@ FIBER_LIB = $(FIBER_PATH)/lib
EXTLIBS =
CFLAGS += -I.. -I$(PRO_INC) -I$(ACL_INC) -I$(FIBER_INC)
LDFLAGS = -L$(PRO_LIB) -l_protocol -L$(FIBER_LIB) -l_fiber \
-L$(ACL_LIB) -l_acl $(EXTLIBS) $(SYSLIB)
LDFLAGS = -L$(PRO_LIB) -l_protocol \
-L$(ACL_LIB) -l_acl \
-L$(FIBER_LIB) -l_fiber \
$(EXTLIBS) $(SYSLIB)
###########################################################

View File

@ -64,6 +64,10 @@ static void client_callback(int type acl_unused, ACL_EVENT *event,
acl_fiber_create(echo_client, conn, 160000);
}
//#define LISTEN_REACTOR
#ifdef LISTEN_REACTOR
static void listen_callback(int type acl_unused, ACL_EVENT *event,
ACL_VSTREAM *sstream, void *ctx acl_unused)
{
@ -72,17 +76,48 @@ static void listen_callback(int type acl_unused, ACL_EVENT *event,
if (conn == NULL) {
printf("accept error %s\r\n", acl_last_serror());
acl_fiber_schedule_stop();
return;
}
printf(">>>accept one, fd: %d\r\n", ACL_VSTREAM_SOCK(conn));
acl_event_enable_read(event, conn, 120, client_callback, NULL);
}
static void fiber_event(ACL_FIBER *fiber acl_unused, void *ctx)
#else
static ACL_EVENT *__event;
static void fiber_listen(ACL_FIBER *fiber acl_unused, void *ctx)
{
ACL_VSTREAM *sstream = (ACL_VSTREAM *) ctx;
while (1) {
char ip[64];
ACL_VSTREAM *conn = acl_vstream_accept(sstream, ip, sizeof(ip));
if (conn == NULL) {
printf("accept error %s\r\n", acl_last_serror());
break;
}
printf("%s: accept one, fd: %d\r\n",
__FUNCTION__, ACL_VSTREAM_SOCK(conn));
acl_event_enable_read(__event, conn, 120, client_callback, NULL);
}
acl_fiber_schedule_stop();
}
#endif
static void fiber_event(ACL_FIBER *fiber acl_unused, void *ctx)
{
#ifdef LISTEN_REACTOR
ACL_VSTREAM *sstream = (ACL_VSTREAM *) ctx;
#else
(void) ctx;
#endif
ACL_EVENT *event;
if (__use_kernel) {
@ -91,14 +126,22 @@ static void fiber_event(ACL_FIBER *fiber acl_unused, void *ctx)
event = acl_event_new(ACL_EVENT_POLL, 0, 0, 200);
}
#ifndef LISTEN_REACTOR
__event = event;
#endif
#ifdef LISTEN_REACTOR
printf(">>>enable listen fd: %d\r\n", ACL_VSTREAM_SOCK(sstream));
acl_event_enable_listen(event, sstream, 0, listen_callback, NULL);
#endif
while (!__stop) {
acl_event_loop(event);
}
#ifdef LISTEN_REACTOR
acl_vstream_close(sstream);
#endif
acl_event_free(event);
acl_fiber_schedule_stop();
@ -138,7 +181,7 @@ int main(int argc, char *argv[])
}
}
sstream = acl_vstream_listen(addr, 1024);
sstream = acl_vstream_listen(addr, 10240);
if (sstream == NULL) {
printf("acl_vstream_listen error %s\r\n", acl_last_serror());
return 1;
@ -146,7 +189,9 @@ int main(int argc, char *argv[])
printf("listen %s ok\r\n", addr);
printf("%s: call fiber_creater\r\n", __FUNCTION__);
#ifndef LISTEN_REACTOR
acl_fiber_create(fiber_listen, sstream, STACK_SIZE);
#endif
acl_fiber_create(fiber_event, sstream, STACK_SIZE);
printf("call fiber_schedule\r\n");