tnettest.c 15.7 KB
Newer Older
H
Hui Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

16
#define _DEFAULT_SOURCE
17
#define ALLOW_FORBID_FUNC
H
Hui Li 已提交
18 19
#include "os.h"
#include "taosdef.h"
H
Hongze Cheng 已提交
20
#include "tmsg.h"
H
Hui Li 已提交
21
#include "taoserror.h"
S
log  
Shengliang Guan 已提交
22
#include "tlog.h"
H
Hui Li 已提交
23
#include "tglobal.h"
H
Hui Li 已提交
24 25
#include "trpc.h"
#include "rpcHead.h"
S
TD-2861  
Shengliang Guan 已提交
26 27
#include "tchecksum.h"
#include "syncMsg.h"
H
Hui Li 已提交
28

29 30
#include "osSocket.h"

31
#define MAX_PKG_LEN (64 * 1000)
32
#define MAX_SPEED_PKG_LEN (1024 * 1024 * 1024)
33 34
#define MIN_SPEED_PKG_LEN 1024
#define MAX_SPEED_PKG_NUM 10000
35
#define MIN_SPEED_PKG_NUM 1
36 37
#define BUFFER_SIZE (MAX_PKG_LEN + 1024)

38
extern int tsRpcMaxUdpSize;
H
Hui Li 已提交
39 40

typedef struct {
41
  char *   hostFqdn;
H
Hui Li 已提交
42
  uint32_t hostIp;
43 44 45 46 47 48 49
  int32_t  port;
  int32_t  pktLen;
} STestInfo;

static void *taosNetBindUdpPort(void *sarg) {
  STestInfo *pinfo = (STestInfo *)sarg;
  int32_t    port = pinfo->port;
50
  SOCKET     serverSocket;
51 52 53
  char       buffer[BUFFER_SIZE];
  int32_t    iDataNum;
  socklen_t  sin_size;
S
Shengliang Guan 已提交
54
  int32_t bufSize = 1024000;
H
Hui Li 已提交
55 56 57

  struct sockaddr_in server_addr;
  struct sockaddr_in clientAddr;
58

59 60 61
  setThreadName("netBindUdpPort");

  if ((serverSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
S
Shengliang Guan 已提交
62
    uError("failed to create UDP socket since %s", strerror(errno));
H
Hui Li 已提交
63 64 65 66 67 68 69 70 71
    return NULL;
  }

  bzero(&server_addr, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(port);
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);

  if (bind(serverSocket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
S
Shengliang Guan 已提交
72
    uError("failed to bind UDP port:%d since %s", port, strerror(errno));
H
Hui Li 已提交
73 74 75
    return NULL;
  }

wafwerar's avatar
wafwerar 已提交
76
  TdSocketPtr pSocket = (TdSocketPtr)taosMemoryMalloc(sizeof(TdSocket));
77 78 79 80 81 82 83 84
  if (pSocket == NULL) {
    taosCloseSocketNoCheck1(serverSocket);
    return NULL;
  }
  pSocket->fd = serverSocket;
  pSocket->refId = 0;

  if (taosSetSockOpt(pSocket, SOL_SOCKET, SO_SNDBUF, (void *)&bufSize, sizeof(bufSize)) != 0) {
S
Shengliang Guan 已提交
85
    uError("failed to set the send buffer size for UDP socket\n");
86
    taosCloseSocket(&pSocket);
S
Shengliang Guan 已提交
87 88 89
    return NULL;
  }

90
  if (taosSetSockOpt(pSocket, SOL_SOCKET, SO_RCVBUF, (void *)&bufSize, sizeof(bufSize)) != 0) {
S
Shengliang Guan 已提交
91
    uError("failed to set the receive buffer size for UDP socket\n");
92
    taosCloseSocket(&pSocket);
S
Shengliang Guan 已提交
93 94 95 96
    return NULL;
  }

  uInfo("UDP server at port:%d is listening", port);
H
Hui Li 已提交
97 98 99 100 101 102 103

  while (1) {
    memset(buffer, 0, BUFFER_SIZE);
    sin_size = sizeof(*(struct sockaddr *)&server_addr);
    iDataNum = recvfrom(serverSocket, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&clientAddr, &sin_size);

    if (iDataNum < 0) {
104
      uDebug("failed to perform recvfrom func at %d since %s", port, strerror(errno));
H
Hui Li 已提交
105 106 107
      continue;
    }

S
Shengliang Guan 已提交
108 109
    uInfo("UDP: recv:%d bytes from %s at %d", iDataNum, taosInetNtoa(clientAddr.sin_addr), port);

110
    if (iDataNum > 0) {
111
      iDataNum = taosSendto(pSocket, buffer, iDataNum, 0, (struct sockaddr *)&clientAddr, (int32_t)sin_size);
H
Hui Li 已提交
112
    }
S
Shengliang Guan 已提交
113 114

    uInfo("UDP: send:%d bytes to %s at %d", iDataNum, taosInetNtoa(clientAddr.sin_addr), port);
H
Hui Li 已提交
115 116
  }

117
  taosCloseSocket(&pSocket);
H
Hui Li 已提交
118 119 120
  return NULL;
}

121
static void *taosNetBindTcpPort(void *sarg) {
H
Hui Li 已提交
122 123
  struct sockaddr_in server_addr;
  struct sockaddr_in clientAddr;
124

125
  STestInfo *pinfo = sarg;
126
  int32_t    port = pinfo->port;
127
  SOCKET     serverSocket;
128
  int32_t    addr_len = sizeof(clientAddr);
129
  SOCKET     client;
130
  char       buffer[BUFFER_SIZE];
H
Hui Li 已提交
131

132 133
  setThreadName("netBindTcpPort");

134
  if ((serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
S
Shengliang Guan 已提交
135
    uError("failed to create TCP socket since %s", strerror(errno));
H
Hui Li 已提交
136 137 138 139 140 141 142 143
    return NULL;
  }

  bzero(&server_addr, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(port);
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);

S
Shengliang Guan 已提交
144
  int32_t reuse = 1;
wafwerar's avatar
wafwerar 已提交
145
  TdSocketPtr pSocket = (TdSocketPtr)taosMemoryMalloc(sizeof(TdSocket));
146 147 148 149 150 151 152 153
  if (pSocket == NULL) {
    taosCloseSocketNoCheck1(serverSocket);
    return NULL;
  }
  pSocket->fd = serverSocket;
  pSocket->refId = 0;

  if (taosSetSockOpt(pSocket, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(reuse)) < 0) {
S
Shengliang Guan 已提交
154
    uError("setsockopt SO_REUSEADDR failed: %d (%s)", errno, strerror(errno));
155
    taosCloseSocket(&pSocket);
S
Shengliang Guan 已提交
156 157 158
    return NULL;
  }

H
Hui Li 已提交
159
  if (bind(serverSocket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
S
Shengliang Guan 已提交
160
    uError("failed to bind TCP port:%d since %s", port, strerror(errno));
161
    taosCloseSocket(&pSocket);
H
Hui Li 已提交
162 163 164
    return NULL;
  }

165
  if (taosKeepTcpAlive(pSocket) < 0) {
S
Shengliang Guan 已提交
166
    uError("failed to set tcp server keep-alive option since %s", strerror(errno));
167
    taosCloseSocket(&pSocket);
H
Hui Li 已提交
168 169 170
    return NULL;
  }

S
Shengliang Guan 已提交
171 172
  if (listen(serverSocket, 10) < 0) {
    uError("failed to listen TCP port:%d since %s", port, strerror(errno));
173
    taosCloseSocket(&pSocket);
S
Shengliang Guan 已提交
174 175 176 177
    return NULL;
  }

  uInfo("TCP server at port:%d is listening", port);
178

H
Hui Li 已提交
179
  while (1) {
180
    client = accept(serverSocket, (struct sockaddr *)&clientAddr, (socklen_t *)&addr_len);
H
Hui Li 已提交
181
    if (client < 0) {
S
Shengliang Guan 已提交
182
      uDebug("TCP: failed to accept at port:%d since %s", port, strerror(errno));
H
Hui Li 已提交
183 184 185
      continue;
    }

186
    int32_t ret = taosReadMsg(pSocket, buffer, pinfo->pktLen);
S
Shengliang Guan 已提交
187
    if (ret < 0 || ret != pinfo->pktLen) {
S
Shengliang Guan 已提交
188
      uError("TCP: failed to read %d bytes at port:%d since %s", pinfo->pktLen, port, strerror(errno));
189
      taosCloseSocket(&pSocket);
S
Shengliang Guan 已提交
190
      return NULL;
H
Hui Li 已提交
191
    }
192

S
Shengliang Guan 已提交
193 194
    uInfo("TCP: read:%d bytes from %s at %d", pinfo->pktLen, taosInetNtoa(clientAddr.sin_addr), port);

195
    ret = taosWriteMsg(pSocket, buffer, pinfo->pktLen);
S
Shengliang Guan 已提交
196
    if (ret < 0) {
S
Shengliang Guan 已提交
197
      uError("TCP: failed to write %d bytes at %d since %s", pinfo->pktLen, port, strerror(errno));
198
      taosCloseSocket(&pSocket);
S
Shengliang Guan 已提交
199
      return NULL;
H
Hui Li 已提交
200
    }
S
Shengliang Guan 已提交
201 202

    uInfo("TCP: write:%d bytes to %s at %d", pinfo->pktLen, taosInetNtoa(clientAddr.sin_addr), port);
H
Hui Li 已提交
203
  }
S
Shengliang Guan 已提交
204

205
  taosCloseSocket(&pSocket);
H
Hui Li 已提交
206 207 208
  return NULL;
}

209
static int32_t taosNetCheckTcpPort(STestInfo *info) {
210
  SOCKET  clientSocket;
S
Shengliang Guan 已提交
211
  char    buffer[BUFFER_SIZE] = {0};
212

213
  if ((clientSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
S
Shengliang Guan 已提交
214
    uError("failed to create TCP client socket since %s", strerror(errno));
H
Hui Li 已提交
215 216 217
    return -1;
  }

S
Shengliang Guan 已提交
218
  int32_t reuse = 1;
wafwerar's avatar
wafwerar 已提交
219
  TdSocketPtr pSocket = (TdSocketPtr)taosMemoryMalloc(sizeof(TdSocket));
220 221 222 223 224 225 226 227
  if (pSocket == NULL) {
    taosCloseSocketNoCheck1(clientSocket);
    return -1;
  }
  pSocket->fd = clientSocket;
  pSocket->refId = 0;

  if (taosSetSockOpt(pSocket, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(reuse)) < 0) {
S
Shengliang Guan 已提交
228
    uError("setsockopt SO_REUSEADDR failed: %d (%s)", errno, strerror(errno));
229
    taosCloseSocket(&pSocket);
S
Shengliang Guan 已提交
230
    return -1;
H
Hui Li 已提交
231 232
  }

S
Shengliang Guan 已提交
233 234
  struct sockaddr_in serverAddr;
  memset((char *)&serverAddr, 0, sizeof(serverAddr));
H
Hui Li 已提交
235
  serverAddr.sin_family = AF_INET;
S
Shengliang Guan 已提交
236
  serverAddr.sin_port = (uint16_t)htons((uint16_t)info->port);
H
Hui Li 已提交
237 238 239
  serverAddr.sin_addr.s_addr = info->hostIp;

  if (connect(clientSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) {
S
Shengliang Guan 已提交
240
    uError("TCP: failed to connect port %s:%d since %s", taosIpStr(info->hostIp), info->port, strerror(errno));
241
    taosCloseSocket(&pSocket);
H
Hui Li 已提交
242 243
    return -1;
  }
244

245
  taosKeepTcpAlive(pSocket);
H
Hui Li 已提交
246

S
Shengliang Guan 已提交
247 248
  sprintf(buffer, "client send TCP pkg to %s:%d, content: 1122334455", taosIpStr(info->hostIp), info->port);
  sprintf(buffer + info->pktLen - 16, "1122334455667788");
H
Hui Li 已提交
249

250
  int32_t ret = taosWriteMsg(pSocket, buffer, info->pktLen);
S
Shengliang Guan 已提交
251
  if (ret < 0) {
S
Shengliang Guan 已提交
252
    uError("TCP: failed to write msg to %s:%d since %s", taosIpStr(info->hostIp), info->port, strerror(errno));
253
    taosCloseSocket(&pSocket);
S
Shengliang Guan 已提交
254
    return -1;
H
Hui Li 已提交
255 256
  }

257
  ret = taosReadMsg(pSocket, buffer, info->pktLen);
S
Shengliang Guan 已提交
258 259
  if (ret < 0) {
    uError("TCP: failed to read msg from %s:%d since %s", taosIpStr(info->hostIp), info->port, strerror(errno));
260
    taosCloseSocket(&pSocket);
H
Hui Li 已提交
261 262 263
    return -1;
  }

264
  taosCloseSocket(&pSocket);
H
Hui Li 已提交
265 266 267
  return 0;
}

268
static int32_t taosNetCheckUdpPort(STestInfo *info) {
269
  SOCKET  clientSocket;
S
Shengliang Guan 已提交
270
  char    buffer[BUFFER_SIZE] = {0};
271
  int32_t iDataNum = 0;
S
Shengliang Guan 已提交
272
  int32_t bufSize = 1024000;
273

H
Hui Li 已提交
274
  struct sockaddr_in serverAddr;
275

276
  if ((clientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
277
    uError("failed to create udp client socket since %s", strerror(errno));
H
Hui Li 已提交
278 279 280
    return -1;
  }

wafwerar's avatar
wafwerar 已提交
281
  TdSocketPtr pSocket = (TdSocketPtr)taosMemoryMalloc(sizeof(TdSocket));
282 283 284 285 286 287 288 289
  if (pSocket == NULL) {
    taosCloseSocketNoCheck1(clientSocket);
    return -1;
  }
  pSocket->fd = clientSocket;
  pSocket->refId = 0;

  if (taosSetSockOpt(pSocket, SOL_SOCKET, SO_SNDBUF, (void *)&bufSize, sizeof(bufSize)) != 0) {
S
Shengliang Guan 已提交
290
    uError("failed to set the send buffer size for UDP socket\n");
291
    taosCloseSocket(&pSocket);
S
Shengliang Guan 已提交
292 293 294
    return -1;
  }

295
  if (taosSetSockOpt(pSocket, SOL_SOCKET, SO_RCVBUF, (void *)&bufSize, sizeof(bufSize)) != 0) {
S
Shengliang Guan 已提交
296
    uError("failed to set the receive buffer size for UDP socket\n");
297
    taosCloseSocket(&pSocket);
S
Shengliang Guan 已提交
298 299 300
    return -1;
  }

H
Hui Li 已提交
301 302 303
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(info->port);
  serverAddr.sin_addr.s_addr = info->hostIp;
304

H
Hui Li 已提交
305 306
  struct in_addr ipStr;
  memcpy(&ipStr, &info->hostIp, 4);
S
Shengliang Guan 已提交
307 308
  sprintf(buffer, "client send udp pkg to %s:%d, content: 1122334455", taosInetNtoa(ipStr), info->port);
  sprintf(buffer + info->pktLen - 16, "1122334455667788");
H
Hui Li 已提交
309 310 311

  socklen_t sin_size = sizeof(*(struct sockaddr *)&serverAddr);

312
  iDataNum = taosSendto(pSocket, buffer, info->pktLen, 0, (struct sockaddr *)&serverAddr, (int32_t)sin_size);
S
Shengliang Guan 已提交
313 314
  if (iDataNum < 0 || iDataNum != info->pktLen) {
    uError("UDP: failed to perform sendto func since %s", strerror(errno));
315
    taosCloseSocket(&pSocket);
H
Hui Li 已提交
316 317 318
    return -1;
  }

S
Shengliang Guan 已提交
319 320 321
  memset(buffer, 0, BUFFER_SIZE);
  sin_size = sizeof(*(struct sockaddr *)&serverAddr);
  iDataNum = recvfrom(clientSocket, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&serverAddr, &sin_size);
H
Hui Li 已提交
322

S
Shengliang Guan 已提交
323 324
  if (iDataNum < 0 || iDataNum != info->pktLen) {
    uError("UDP: received ack:%d bytes(expect:%d) from port:%d since %s", iDataNum, info->pktLen, info->port, strerror(errno));
325
    taosCloseSocket(&pSocket);
H
Hui Li 已提交
326 327
    return -1;
  }
328

329
  taosCloseSocket(&pSocket);
H
Hui Li 已提交
330 331 332
  return 0;
}

333 334 335
static void taosNetCheckPort(uint32_t hostIp, int32_t startPort, int32_t endPort, int32_t pktLen) {
  int32_t   ret;
  STestInfo info;
H
Hui Li 已提交
336

337 338 339
  memset(&info, 0, sizeof(STestInfo));
  info.hostIp = hostIp;
  info.pktLen = pktLen;
H
Hui Li 已提交
340

341
  for (int32_t port = startPort; port <= endPort; port++) {
H
Hui Li 已提交
342
    info.port = port;
343
    ret = taosNetCheckTcpPort(&info);
H
Hui Li 已提交
344
    if (ret != 0) {
345
      printf("failed to test TCP port:%d\n", port);
H
Hui Li 已提交
346
    } else {
347
      printf("successed to test TCP port:%d\n", port);
H
Hui Li 已提交
348
    }
349 350

    ret = taosNetCheckUdpPort(&info);
H
Hui Li 已提交
351
    if (ret != 0) {
352
      printf("failed to test UDP port:%d\n", port);
H
Hui Li 已提交
353
    } else {
354
      printf("successed to test UDP port:%d\n", port);
H
Hui Li 已提交
355 356 357 358
    }
  }
}

359
static void taosNetTestClient(char *host, int32_t startPort, int32_t pkgLen) {
360
  uInfo("work as client, host:%s Port:%d pkgLen:%d\n", host, startPort, pkgLen);
361

362
  uint32_t serverIp = taosGetIpv4FromFqdn(host);
H
Hui Li 已提交
363
  if (serverIp == 0xFFFFFFFF) {
364
    uError("failed to resolve fqdn:%s", host);
H
Hui Li 已提交
365 366
    exit(-1);
  }
H
Hui Li 已提交
367

368
  uInfo("server ip:%s is resolved from host:%s", taosIpStr(serverIp), host);
369
  taosNetCheckPort(serverIp, startPort, startPort, pkgLen);
H
Hui Li 已提交
370
}
H
Hui Li 已提交
371

372
static void taosNetTestServer(char *host, int32_t startPort, int32_t pkgLen) {
373
  uInfo("work as server, host:%s Port:%d pkgLen:%d\n", host, startPort, pkgLen);
H
Hui Li 已提交
374

375
  int32_t port = startPort;
376
  int32_t num = 1;
377
  if (num < 0) num = 1;
H
Hui Li 已提交
378

wafwerar's avatar
wafwerar 已提交
379 380 381
  TdThread *pids = taosMemoryMalloc(2 * num * sizeof(TdThread));
  STestInfo *tinfos = taosMemoryMalloc(num * sizeof(STestInfo));
  STestInfo *uinfos = taosMemoryMalloc(num * sizeof(STestInfo));
H
Hui Li 已提交
382

383 384 385 386
  for (int32_t i = 0; i < num; i++) {
    STestInfo *tcpInfo = tinfos + i;
    tcpInfo->port = port + i;
    tcpInfo->pktLen = pkgLen;
H
Hui Li 已提交
387

wafwerar's avatar
wafwerar 已提交
388
    if (taosThreadCreate(pids + i, NULL, taosNetBindTcpPort, tcpInfo) != 0) {
S
Shengliang Guan 已提交
389
      uInfo("failed to create TCP test thread, %s:%d", tcpInfo->hostFqdn, tcpInfo->port);
H
Hui Li 已提交
390 391 392
      exit(-1);
    }

393
    STestInfo *udpInfo = uinfos + i;
S
Shengliang Guan 已提交
394 395
    udpInfo->port = port + i;
    tcpInfo->pktLen = pkgLen;
wafwerar's avatar
wafwerar 已提交
396
    if (taosThreadCreate(pids + num + i, NULL, taosNetBindUdpPort, udpInfo) != 0) {
S
Shengliang Guan 已提交
397
      uInfo("failed to create UDP test thread, %s:%d", tcpInfo->hostFqdn, tcpInfo->port);
H
Hui Li 已提交
398 399 400
      exit(-1);
    }
  }
401 402

  for (int32_t i = 0; i < num; i++) {
wafwerar's avatar
wafwerar 已提交
403 404
    taosThreadJoin(pids[i], NULL);
    taosThreadJoin(pids[(num + i)], NULL);
H
Hui Li 已提交
405 406 407
  }
}

408 409
static void taosNetCheckSpeed(char *host, int32_t port, int32_t pkgLen,
                              int32_t pkgNum, char *pkgType) {
S
Shengliang Guan 已提交
410 411
#if 0
                              
412 413 414
  // record config
  int32_t compressTmp = tsCompressMsgSize;
  int32_t maxUdpSize  = tsRpcMaxUdpSize;
415
  int32_t forceTcp  = tsRpcForceTcp;
416

417
  if (0 == strcmp("tcp", pkgType)){
418
    tsRpcForceTcp = 1;
419 420
    tsRpcMaxUdpSize = 0;            // force tcp
  } else {
421
    tsRpcForceTcp = 0;
422 423
    tsRpcMaxUdpSize = INT_MAX;
  }
424 425
  tsCompressMsgSize = -1;

426
  SEpSet epSet;
427 428 429 430 431 432 433 434 435 436
  SRpcMsg   reqMsg;
  SRpcMsg   rspMsg;
  void *    pRpcConn;
  char secretEncrypt[32] = {0};
  char    spi = 0;
  pRpcConn = taosNetInitRpc(secretEncrypt, spi);
  if (NULL == pRpcConn) {
    uError("failed to init client rpc");
    return;
  }
437

438
  printf("check net spend, host:%s port:%d pkgLen:%d pkgNum:%d pkgType:%s\n\n", host, port, pkgLen, pkgNum, pkgType);
439
  int32_t totalSucc = 0;
440
  uint64_t startT = taosGetTimestampUs();
441
  for (int32_t i = 1; i <= pkgNum; i++) {
442
    uint64_t startTime = taosGetTimestampUs();
443

444 445 446
    memset(&epSet, 0, sizeof(SEpSet));
    strcpy(epSet.eps[0].fqdn, host);
    epSet.eps[0].port = port;
447 448
    epSet.numOfEps = 1;

H
Hongze Cheng 已提交
449
    reqMsg.msgType = TDMT_DND_NETWORK_TEST;
450 451 452 453 454 455 456
    reqMsg.pCont = rpcMallocCont(pkgLen);
    reqMsg.contLen = pkgLen;
    reqMsg.code = 0;
    reqMsg.handle = NULL;   // rpc handle returned to app
    reqMsg.ahandle = NULL;  // app handle set by client
    strcpy(reqMsg.pCont, "nettest speed");

457 458 459
    rpcSendRecv(pRpcConn, &epSet, &reqMsg, &rspMsg);

    int code = 0;
H
Hongze Cheng 已提交
460
    if ((rspMsg.code != 0) || (rspMsg.msgType != TDMT_DND_NETWORK_TEST + 1)) {
461 462 463 464 465 466
      uError("ret code 0x%x %s", rspMsg.code, tstrerror(rspMsg.code));
      code = -1;
    }else{
      totalSucc ++;
    }

467 468
    rpcFreeCont(rspMsg.pCont);

469 470
    uint64_t endTime = taosGetTimestampUs();
    uint64_t el = endTime - startTime;
471
    printf("progress:%5d/%d\tstatus:%d\tcost:%8.2lf ms\tspeed:%8.2lf MB/s\n", i, pkgNum, code, el/1000.0, pkgLen/(el/1000000.0)/1024.0/1024.0);
472
  }
473
  int64_t endT = taosGetTimestampUs();
474
  uint64_t elT = endT - startT;
475
  printf("\ntotal succ:%5d/%d\tcost:%8.2lf ms\tspeed:%8.2lf MB/s\n", totalSucc, pkgNum, elT/1000.0, pkgLen/(elT/1000000.0)/1024.0/1024.0*totalSucc);
476

477 478 479 480 481
  rpcClose(pRpcConn);

  // return config
  tsCompressMsgSize = compressTmp;
  tsRpcMaxUdpSize = maxUdpSize;
482
  tsRpcForceTcp = forceTcp;
483
  return;
S
Shengliang Guan 已提交
484
#endif
485 486
}

S
Shengliang Guan 已提交
487
void taosNetTest(char *role, char *host, int32_t port, int32_t pkgLen, int32_t pkgNum, char *pkgType) {
488
  tsLogEmbedded = 1;
489 490
  if (host == NULL) host = tsLocalFqdn;
  if (port == 0) port = tsServerPort;
S
Shengliang Guan 已提交
491
  if (0 == strcmp("speed", role)) {
492 493
    if (pkgLen <= MIN_SPEED_PKG_LEN) pkgLen = MIN_SPEED_PKG_LEN;
    if (pkgLen > MAX_SPEED_PKG_LEN) pkgLen = MAX_SPEED_PKG_LEN;
494 495
    if (pkgNum <= MIN_SPEED_PKG_NUM) pkgNum = MIN_SPEED_PKG_NUM;
    if (pkgNum > MAX_SPEED_PKG_NUM) pkgNum = MAX_SPEED_PKG_NUM;
S
Shengliang Guan 已提交
496
  } else {
497 498 499
    if (pkgLen <= 10) pkgLen = 1000;
    if (pkgLen > MAX_PKG_LEN) pkgLen = MAX_PKG_LEN;
  }
500 501 502 503 504

  if (0 == strcmp("client", role)) {
    taosNetTestClient(host, port, pkgLen);
  } else if (0 == strcmp("server", role)) {
    taosNetTestServer(host, port, pkgLen);
505
  } else if (0 == strcmp("speed", role)) {
506
    tsLogEmbedded = 0;
507
    char type[10] = {0};
508
    taosNetCheckSpeed(host, port, pkgLen, pkgNum, strtolower(type, pkgType));
H
Hui Li 已提交
509
  } else {
S
Shengliang Guan 已提交
510
    TASSERT(1);
H
Hui Li 已提交
511 512
  }

513
  tsLogEmbedded = 0;
H
Hui Li 已提交
514
}