test fiber demo

This commit is contained in:
郑树新 2021-10-13 20:40:22 +08:00
parent e2deb72f12
commit 249d34c53c
2 changed files with 29 additions and 8 deletions

View File

@ -41,9 +41,9 @@ static size_t do_echo(acl::socket_stream& conn, int count, bool readable) {
return i;
}
static size_t connect_server(const char* addr, int count, bool readable) {
static size_t start(const char* addr, int count, bool readable, int timeout) {
acl::socket_stream conn;
if (!conn.open(addr, 10, 10)) {
if (!conn.open(addr, timeout, timeout)) {
printf("connect %s error %s\r\n", addr, acl::last_serror());
return 0;
}
@ -61,18 +61,19 @@ static void usage(const char* procname) {
" -c fiber_count[default: 1]\r\n"
" -n count[default: 100]\r\n"
" -r [if call readable? default: false]\r\n"
" -T timeout[default: 10 seconds]\r\n"
, procname);
}
int main(int argc, char *argv[]) {
int ch, nfiber = 1, count = 100;
int ch, nfiber = 1, count = 100, timeout = 10;
bool readable = false;
acl::string addr = "127.0.0.1:9000", event_type("kernel");
acl::acl_cpp_init();
acl::log::stdout_open(true);
while ((ch = getopt(argc, argv, "he:s:c:n:r")) > 0) {
while ((ch = getopt(argc, argv, "he:s:c:n:rT:")) > 0) {
switch (ch) {
case 'h':
usage(argv[0]);
@ -92,6 +93,9 @@ int main(int argc, char *argv[]) {
case 'r':
readable = true;
break;
case 'T':
timeout = atoi(optarg);
break;
default:
break;
}
@ -114,7 +118,7 @@ int main(int argc, char *argv[]) {
for (int i = 0; i < nfiber; i++) {
go[&] {
total += connect_server(addr, count, readable);
total += start(addr, count, readable, timeout);
};
}

View File

@ -55,18 +55,23 @@ static void server_listen(acl::server_socket& ss, bool readable) {
//////////////////////////////////////////////////////////////////////////////
static void usage(const char* procname) {
printf("usage: %s -h [help] -s server_addr -r [if call readable]\r\n", procname);
printf("usage: %s -h [help]\r\n"
" -e event_type[kernel|select|poll]\r\n"
" -s server_addr\r\n"
" -r [if call readable]\r\n"
, procname);
}
int main(int argc, char *argv[]) {
int ch;
bool readable = false;
acl::string addr = "0.0.0.0:9000";
acl::string event_type("kernel");
acl::acl_cpp_init();
acl::log::stdout_open(true);
while ((ch = getopt(argc, argv, "hs:r")) > 0) {
while ((ch = getopt(argc, argv, "hs:e:r")) > 0) {
switch (ch) {
case 'h':
usage(argv[0]);
@ -74,6 +79,9 @@ int main(int argc, char *argv[]) {
case 's':
addr = optarg;
break;
case 'e':
event_type = optarg;
break;
case 'r':
readable = true;
break;
@ -92,7 +100,16 @@ int main(int argc, char *argv[]) {
server_listen(ss, readable);
};
acl::fiber::schedule();
acl::fiber_event_t type;
if (event_type == "select") {
type = acl::FIBER_EVENT_T_SELECT;
} else if (event_type == "poll") {
type = acl::FIBER_EVENT_T_POLL;
} else {
type = acl::FIBER_EVENT_T_KERNEL;
}
acl::fiber::schedule_with(type);
return 0;
}