|
| 1 | +#include "stdafx.h" |
| 2 | + |
| 3 | +static bool use_sockopt = false; |
| 4 | +static std::atomic<long long> total_count(0); |
| 5 | + |
| 6 | +static void fiber_main(const char *addr, int count, int timeo) { |
| 7 | + acl::socket_stream conn; |
| 8 | + if (!conn.open(addr, timeo, timeo)) { |
| 9 | + printf("Connect %s error %s\r\n", addr, acl::last_serror()); |
| 10 | + return; |
| 11 | + } |
| 12 | + |
| 13 | + conn.set_rw_timeout(timeo, use_sockopt); |
| 14 | + |
| 15 | + printf("Connect %s ok, fd=%d\r\n", addr, conn.sock_handle()); |
| 16 | + |
| 17 | + const char s[] = "hello world!\r\n"; |
| 18 | + char buf[1024]; |
| 19 | + int i; |
| 20 | + |
| 21 | + for (i = 0; i < count; i++) { |
| 22 | + if (conn.write(s, sizeof(s) - 1) == -1) { |
| 23 | + printf("write error %s\r\n", acl::last_serror()); |
| 24 | + break; |
| 25 | + } |
| 26 | + if (conn.read(buf, sizeof(buf) -1, false) == -1) { |
| 27 | + printf("read error %s\r\n", acl::last_serror()); |
| 28 | + break; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + total_count += i; |
| 33 | +} |
| 34 | + |
| 35 | +static void thread_main(const char *addr, int nfibers, int count , int timeo) { |
| 36 | + for (int j = 0; j < nfibers; j++) { |
| 37 | + go[=] { |
| 38 | + fiber_main(addr, count, timeo); |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + acl::fiber::schedule_with(acl::FIBER_EVENT_T_KERNEL); |
| 44 | +} |
| 45 | + |
| 46 | +static void usage(const char *procname) { |
| 47 | + printf("usage: %s -h [help]\r\n" |
| 48 | + " -s server_addr[default: 127.0.0.1:9001]\r\n" |
| 49 | + " -k cpus[default: 1]\r\n" |
| 50 | + " -c fibers count[default: 100]\r\n" |
| 51 | + " -o io timeout[default: -1]\r\n" |
| 52 | + " -O [if use setsockopt to set IO timeout, default: false]\r\n" |
| 53 | + , procname); |
| 54 | +} |
| 55 | + |
| 56 | +int main(int argc, char *argv[]) { |
| 57 | + int ch, cpus = 1, count = 10000, nfibers = 100, timeo = -1; |
| 58 | + std::string addr("127.0.0.1:9001"); |
| 59 | + |
| 60 | + while ((ch = getopt(argc, argv, "hs:k:c:o:O")) > 0) { |
| 61 | + switch (ch) { |
| 62 | + case 'h': |
| 63 | + usage(argv[0]); |
| 64 | + return 0; |
| 65 | + case 's': |
| 66 | + addr = optarg; |
| 67 | + break; |
| 68 | + case 'k': |
| 69 | + cpus = atoi(optarg); |
| 70 | + break; |
| 71 | + case 'c': |
| 72 | + nfibers = atoi(optarg); |
| 73 | + break; |
| 74 | + case 'o': |
| 75 | + timeo = atoi(optarg); |
| 76 | + break; |
| 77 | + case 'O': |
| 78 | + use_sockopt = true; |
| 79 | + break; |
| 80 | + default: |
| 81 | + usage(argv[0]); |
| 82 | + return 1; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + struct timeval begin; |
| 87 | + gettimeofday(&begin, nullptr); |
| 88 | + |
| 89 | + std::vector<std::thread*> threads; |
| 90 | + for (int i = 0; i < cpus; i++) { |
| 91 | + std::thread *thr = new std::thread([&]{ |
| 92 | + thread_main(addr.c_str(), nfibers, count, timeo); |
| 93 | + }); |
| 94 | + threads.push_back(thr); |
| 95 | + } |
| 96 | + |
| 97 | + for (auto thr : threads) { |
| 98 | + thr->join(); |
| 99 | + delete thr; |
| 100 | + } |
| 101 | + |
| 102 | + struct timeval end; |
| 103 | + gettimeofday(&end, nullptr); |
| 104 | + double tc = acl::stamp_sub(end, begin); |
| 105 | + |
| 106 | + double speed = (total_count * 1000) / (tc > 0 ? tc : 0.0001); |
| 107 | + printf("Total count=%lld, tc=%.2f ms, speed=%.2f\r\n", |
| 108 | + total_count.load(), tc, speed); |
| 109 | + |
| 110 | + return 0; |
| 111 | +} |
0 commit comments