|
|
@@ -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);
|
|
|
});
|
|
|
|