ithewei 4 年之前
父节点
当前提交
bbe9c5b67b
共有 3 个文件被更改,包括 52 次插入33 次删除
  1. 8 7
      echo-servers/benchmark.sh
  2. 0 1
      echo-servers/build.sh
  3. 44 25
      echo-servers/pingpong_client.cpp

+ 8 - 7
echo-servers/benchmark.sh

@@ -2,16 +2,18 @@
 
 host=127.0.0.1
 port=2000
-client=2
-time=10
+connections=100
+duration=10
+threads=2
 
-while getopts 'h:p:c:t:' opt
+while getopts 'h:p:c:d:t:' opt
 do
     case $opt in
         h) host=$OPTARG;;
         p) port=$OPTARG;;
-        c) client=$OPTARG;;
-        t) time=$OPTARG;;
+        c) connections=$OPTARG;;
+        d) duration=$OPTARG;;
+        t) threads=$OPTARG;;
         *) exit -1;;
     esac
 done
@@ -78,8 +80,7 @@ sleep 1
 
 for ((p=$sport+1; p<=$port; ++p)); do
     echo -e "\n==============$p====================================="
-    # bin/webbench -q -c $client -t $time $host:$p
-    bin/pingpong_client -H $host -p $p
+    bin/pingpong_client -H $host -p $p -c $connections -d $duration -t $threads
     sleep 1
 done
 

+ 0 - 1
echo-servers/build.sh

@@ -32,4 +32,3 @@ fi
 cd ${ROOT_DIR}
 make libhv && sudo make install
 make echo-servers
-make webbench

+ 44 - 25
echo-servers/pingpong_client.cpp

@@ -1,27 +1,30 @@
-#include "hv/hmain.h"
+#include "hv/hmain.h" // import parse_opt
 #include "hv/hloop.h"
 #include "hv/hsocket.h"
 
 #include "hv/EventLoopThreadPool.h"
 using namespace hv;
 
-static const char options[] = "hH:p:t:c:s:b:";
+static const char options[] = "hvH:p:c:d:t:b:";
 
 static const char detail_options[] = R"(
-  -h                Print help
+  -h                Print help infomation
+  -v                Show verbose infomation
   -H <Host>         default 127.0.0.1
   -p <port>
-  -t <threads>      default 4
-  -c <connections>  default 1000
-  -s <seconds>      default 10
-  -b <bytes>        send buffer size, default 1024
+  -c <connections>  Number of connections, default: 1000
+  -d <duration>     Duration of test, default: 10s
+  -t <threads>      Number of threads, default: 4
+  -b <bytes>        Bytes of send buffer, default: 1024
 )";
 
+static int connections = 1000;
+static int duration = 10;
+static int threads = 4;
+
+static bool verbose = false;
 static const char* host = "127.0.0.1";
 static int port = 0;
-static int threads = 4;
-static int connections = 1000;
-static int seconds = 10;
 static int sendbytes = 1024;
 static void* sendbuf = NULL;
 
@@ -39,16 +42,18 @@ static void print_result() {
     printf("total readcount=%llu readbytes=%llu\n",
         (unsigned long long)total_readcount,
         (unsigned long long)total_readbytes);
-    printf("throughput = %llu MB/s\n", (total_readbytes) / ((unsigned long long)seconds * 1024 * 1024));
+    printf("throughput = %llu MB/s\n", (total_readbytes) / ((unsigned long long)duration * 1024 * 1024));
 }
 
 static void on_close(hio_t* io) {
     if (++disconnected_num == connections) {
-        printf("all disconnected\n");
+        if (verbose) {
+            printf("all disconnected\n");
+        }
     }
 }
 
-void on_recv(hio_t* io, void* buf, int readbytes) {
+static void on_recv(hio_t* io, void* buf, int readbytes) {
     ++total_readcount;
     total_readbytes += readbytes;
     hio_write(io, buf, readbytes);
@@ -56,15 +61,15 @@ void on_recv(hio_t* io, void* buf, int readbytes) {
 
 static void on_connect(hio_t* io) {
     if (++connected_num == connections) {
-        printf("all connected\n");
+        if (verbose) {
+            printf("all connected\n");
+        }
     }
 
-    tcp_nodelay(hio_fd(io), 1);
+    hio_write(io, sendbuf, sendbytes);
+
     hio_setcb_read(io, on_recv);
-    hio_setcb_close(io, on_close);
     hio_read_start(io);
-
-    hio_write(io, sendbuf, sendbytes);
 }
 
 int main(int argc, char** argv) {
@@ -77,37 +82,51 @@ int main(int argc, char** argv) {
     }
     const char* strHost = get_arg("H");
     const char* strPort = get_arg("p");
-    const char* strThreads = get_arg("t");
     const char* strConnections = get_arg("c");
-    const char* strSeconds = get_arg("s");
+    const char* strDuration = get_arg("d");
+    const char* strThreads = get_arg("t");
     const char* strBytes = get_arg("b");
 
     if (strHost)        host = strHost;
     if (strPort)        port = atoi(strPort);
-    if (strThreads)     threads = atoi(strThreads);
     if (strConnections) connections = atoi(strConnections);
-    if (strSeconds)     seconds = atoi(strSeconds);
+    if (strDuration)    duration = atoi(strDuration);
+    if (strThreads)     threads = atoi(strThreads);
     if (strBytes)       sendbytes = atoi(strBytes);
 
     if (get_arg("h") || port == 0) {
         print_help();
         exit(0);
     }
+    if (get_arg("v")) {
+        verbose = true;
+    }
     sendbuf = malloc(sendbytes);
 
     printf("[%s:%d] %d threads %d connections run %ds\n",
         host, port,
-        threads, connections, seconds);
+        threads, connections, duration);
 
     EventLoopThreadPool loop_threads(threads);
     loop_threads.start(true);
     for (int i = 0; i < connections; ++i) {
         EventLoopPtr loop = loop_threads.nextLoop();
-        loop->runInLoop(std::bind(hloop_create_tcp_client, loop->loop(), host, port, on_connect));
+        hloop_t* hloop = loop->loop();
+        loop->runInLoop([hloop](){
+            hio_t* io = hio_create_socket(hloop, host, port, HIO_TYPE_TCP, HIO_CLIENT_SIDE);
+            if (io == NULL) {
+                perror("socket");
+                exit(1);
+            }
+            tcp_nodelay(hio_fd(io), 1);
+            hio_setcb_connect(io, on_connect);
+            hio_setcb_close(io, on_close);
+            hio_connect(io);
+        });
     }
 
     // stop after seconds
-    loop_threads.loop()->setTimeout(seconds * 1000, [&loop_threads](TimerID timerID){
+    loop_threads.loop()->setTimeout(duration * 1000, [&loop_threads](TimerID timerID){
         loop_threads.stop(false);
     });