shellNettest.c 15.8 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 17 18 19 20 21
#define _GNU_SOURCE
#include "shellInt.h"

void shellTestNetWork() {}

#if 0
22
#define ALLOW_FORBID_FUNC
H
Hui Li 已提交
23
#include "os.h"
24 25
#include "rpcHead.h"
#include "syncMsg.h"
H
Hui Li 已提交
26 27
#include "taosdef.h"
#include "taoserror.h"
28
#include "tchecksum.h"
H
Hui Li 已提交
29
#include "tglobal.h"
30 31
#include "tlog.h"
#include "tmsg.h"
H
Hui Li 已提交
32
#include "trpc.h"
H
Hui Li 已提交
33

34 35
#include "osSocket.h"

36
#define MAX_PKG_LEN       (64 * 1000)
37
#define MAX_SPEED_PKG_LEN (1024 * 1024 * 1024)
38 39
#define MIN_SPEED_PKG_LEN 1024
#define MAX_SPEED_PKG_NUM 10000
40
#define MIN_SPEED_PKG_NUM 1
41
#define BUFFER_SIZE       (MAX_PKG_LEN + 1024)
42

43
extern int tsRpcMaxUdpSize;
H
Hui Li 已提交
44 45

typedef struct {
46
  char *   hostFqdn;
H
Hui Li 已提交
47
  uint32_t hostIp;
48 49 50 51 52 53 54
  int32_t  port;
  int32_t  pktLen;
} STestInfo;

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

  struct sockaddr_in server_addr;
  struct sockaddr_in clientAddr;
63

64 65 66
  setThreadName("netBindUdpPort");

  if ((serverSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
S
Shengliang Guan 已提交
67
    uError("failed to create UDP socket since %s", strerror(errno));
H
Hui Li 已提交
68 69 70 71 72 73 74 75 76
    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 已提交
77
    uError("failed to bind UDP port:%d since %s", port, strerror(errno));
H
Hui Li 已提交
78 79 80
    return NULL;
  }

wafwerar's avatar
wafwerar 已提交
81
  TdSocketPtr pSocket = (TdSocketPtr)taosMemoryMalloc(sizeof(TdSocket));
82 83 84 85 86 87 88 89
  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 已提交
90
    uError("failed to set the send buffer size for UDP socket\n");
91
    taosCloseSocket(&pSocket);
S
Shengliang Guan 已提交
92 93 94
    return NULL;
  }

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

  uInfo("UDP server at port:%d is listening", port);
H
Hui Li 已提交
102 103 104 105 106 107 108

  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) {
109
      uDebug("failed to perform recvfrom func at %d since %s", port, strerror(errno));
H
Hui Li 已提交
110 111 112
      continue;
    }

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

115
    if (iDataNum > 0) {
116
      iDataNum = taosSendto(pSocket, buffer, iDataNum, 0, (struct sockaddr *)&clientAddr, (int32_t)sin_size);
H
Hui Li 已提交
117
    }
S
Shengliang Guan 已提交
118 119

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

122
  taosCloseSocket(&pSocket);
H
Hui Li 已提交
123 124 125
  return NULL;
}

126
static void *taosNetBindTcpPort(void *sarg) {
H
Hui Li 已提交
127 128
  struct sockaddr_in server_addr;
  struct sockaddr_in clientAddr;
129

130
  STestInfo *pinfo = sarg;
131
  int32_t    port = pinfo->port;
132
  SOCKET     serverSocket;
133
  int32_t    addr_len = sizeof(clientAddr);
134
  SOCKET     client;
135
  char       buffer[BUFFER_SIZE];
H
Hui Li 已提交
136

137 138
  setThreadName("netBindTcpPort");

139
  if ((serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
S
Shengliang Guan 已提交
140
    uError("failed to create TCP socket since %s", strerror(errno));
H
Hui Li 已提交
141 142 143 144 145 146 147 148
    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 已提交
149
  int32_t reuse = 1;
wafwerar's avatar
wafwerar 已提交
150
  TdSocketPtr pSocket = (TdSocketPtr)taosMemoryMalloc(sizeof(TdSocket));
151 152 153 154 155 156 157 158
  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 已提交
159
    uError("setsockopt SO_REUSEADDR failed: %d (%s)", errno, strerror(errno));
160
    taosCloseSocket(&pSocket);
S
Shengliang Guan 已提交
161 162 163
    return NULL;
  }

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

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

S
Shengliang Guan 已提交
176 177
  if (listen(serverSocket, 10) < 0) {
    uError("failed to listen TCP port:%d since %s", port, strerror(errno));
178
    taosCloseSocket(&pSocket);
S
Shengliang Guan 已提交
179 180 181 182
    return NULL;
  }

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

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

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

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

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

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

210
  taosCloseSocket(&pSocket);
H
Hui Li 已提交
211 212 213
  return NULL;
}

214
static int32_t taosNetCheckTcpPort(STestInfo *info) {
215
  SOCKET  clientSocket;
S
Shengliang Guan 已提交
216
  char    buffer[BUFFER_SIZE] = {0};
217

218
  if ((clientSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
S
Shengliang Guan 已提交
219
    uError("failed to create TCP client socket since %s", strerror(errno));
H
Hui Li 已提交
220 221 222
    return -1;
  }

S
Shengliang Guan 已提交
223
  int32_t reuse = 1;
wafwerar's avatar
wafwerar 已提交
224
  TdSocketPtr pSocket = (TdSocketPtr)taosMemoryMalloc(sizeof(TdSocket));
225 226 227 228 229 230 231 232
  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 已提交
233
    uError("setsockopt SO_REUSEADDR failed: %d (%s)", errno, strerror(errno));
234
    taosCloseSocket(&pSocket);
S
Shengliang Guan 已提交
235
    return -1;
H
Hui Li 已提交
236 237
  }

S
Shengliang Guan 已提交
238 239
  struct sockaddr_in serverAddr;
  memset((char *)&serverAddr, 0, sizeof(serverAddr));
H
Hui Li 已提交
240
  serverAddr.sin_family = AF_INET;
S
Shengliang Guan 已提交
241
  serverAddr.sin_port = (uint16_t)htons((uint16_t)info->port);
H
Hui Li 已提交
242 243 244
  serverAddr.sin_addr.s_addr = info->hostIp;

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

250
  taosKeepTcpAlive(pSocket);
H
Hui Li 已提交
251

S
Shengliang Guan 已提交
252 253
  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 已提交
254

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

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

269
  taosCloseSocket(&pSocket);
H
Hui Li 已提交
270 271 272
  return 0;
}

273
static int32_t taosNetCheckUdpPort(STestInfo *info) {
274
  SOCKET  clientSocket;
S
Shengliang Guan 已提交
275
  char    buffer[BUFFER_SIZE] = {0};
276
  int32_t iDataNum = 0;
S
Shengliang Guan 已提交
277
  int32_t bufSize = 1024000;
278

H
Hui Li 已提交
279
  struct sockaddr_in serverAddr;
280

281
  if ((clientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
282
    uError("failed to create udp client socket since %s", strerror(errno));
H
Hui Li 已提交
283 284 285
    return -1;
  }

wafwerar's avatar
wafwerar 已提交
286
  TdSocketPtr pSocket = (TdSocketPtr)taosMemoryMalloc(sizeof(TdSocket));
287 288 289 290 291 292 293 294
  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 已提交
295
    uError("failed to set the send buffer size for UDP socket\n");
296
    taosCloseSocket(&pSocket);
S
Shengliang Guan 已提交
297 298 299
    return -1;
  }

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

H
Hui Li 已提交
306 307 308
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(info->port);
  serverAddr.sin_addr.s_addr = info->hostIp;
309

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

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

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

S
Shengliang Guan 已提交
324 325 326
  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 已提交
327

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

333
  taosCloseSocket(&pSocket);
H
Hui Li 已提交
334 335 336
  return 0;
}

337 338 339
static void taosNetCheckPort(uint32_t hostIp, int32_t startPort, int32_t endPort, int32_t pktLen) {
  int32_t   ret;
  STestInfo info;
H
Hui Li 已提交
340

341 342 343
  memset(&info, 0, sizeof(STestInfo));
  info.hostIp = hostIp;
  info.pktLen = pktLen;
H
Hui Li 已提交
344

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

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

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

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

372
  uInfo("server ip:%s is resolved from host:%s", taosIpStr(serverIp), host);
373
  taosNetCheckPort(serverIp, startPort, startPort, pkgLen);
H
Hui Li 已提交
374
}
H
Hui Li 已提交
375

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

379
  int32_t port = startPort;
380
  int32_t num = 1;
381
  if (num < 0) num = 1;
H
Hui Li 已提交
382

wafwerar's avatar
wafwerar 已提交
383 384 385
  TdThread *pids = taosMemoryMalloc(2 * num * sizeof(TdThread));
  STestInfo *tinfos = taosMemoryMalloc(num * sizeof(STestInfo));
  STestInfo *uinfos = taosMemoryMalloc(num * sizeof(STestInfo));
H
Hui Li 已提交
386

387 388 389 390
  for (int32_t i = 0; i < num; i++) {
    STestInfo *tcpInfo = tinfos + i;
    tcpInfo->port = port + i;
    tcpInfo->pktLen = pkgLen;
H
Hui Li 已提交
391

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

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

  for (int32_t i = 0; i < num; i++) {
wafwerar's avatar
wafwerar 已提交
407 408
    taosThreadJoin(pids[i], NULL);
    taosThreadJoin(pids[(num + i)], NULL);
H
Hui Li 已提交
409 410 411
  }
}

412 413
static void taosNetCheckSpeed(char *host, int32_t port, int32_t pkgLen,
                              int32_t pkgNum, char *pkgType) {
S
Shengliang Guan 已提交
414
#if 0
415

416 417 418
  // record config
  int32_t compressTmp = tsCompressMsgSize;
  int32_t maxUdpSize  = tsRpcMaxUdpSize;
419
  int32_t forceTcp  = tsRpcForceTcp;
420

421
  if (0 == strcmp("tcp", pkgType)){
422
    tsRpcForceTcp = 1;
423 424
    tsRpcMaxUdpSize = 0;            // force tcp
  } else {
425
    tsRpcForceTcp = 0;
426 427
    tsRpcMaxUdpSize = INT_MAX;
  }
428 429
  tsCompressMsgSize = -1;

430
  SEpSet epSet;
431 432 433 434 435 436 437 438 439 440
  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;
  }
441

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

448 449 450
    memset(&epSet, 0, sizeof(SEpSet));
    strcpy(epSet.eps[0].fqdn, host);
    epSet.eps[0].port = port;
451 452
    epSet.numOfEps = 1;

H
Hongze Cheng 已提交
453
    reqMsg.msgType = TDMT_DND_NETWORK_TEST;
454 455 456 457 458 459 460
    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");

461 462 463
    rpcSendRecv(pRpcConn, &epSet, &reqMsg, &rspMsg);

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

471 472
    rpcFreeCont(rspMsg.pCont);

473 474
    uint64_t endTime = taosGetTimestampUs();
    uint64_t el = endTime - startTime;
475 476
    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);
477
  }
478
  int64_t endT = taosGetTimestampUs();
479
  uint64_t elT = endT - startT;
480 481
  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);
482

483 484 485 486 487
  rpcClose(pRpcConn);

  // return config
  tsCompressMsgSize = compressTmp;
  tsRpcMaxUdpSize = maxUdpSize;
488
  tsRpcForceTcp = forceTcp;
489
  return;
S
Shengliang Guan 已提交
490
#endif
491 492
}

S
Shengliang Guan 已提交
493
void taosNetTest(char *role, char *host, int32_t port, int32_t pkgLen, int32_t pkgNum, char *pkgType) {
494
  tsLogEmbedded = 1;
495 496
  if (host == NULL) host = tsLocalFqdn;
  if (port == 0) port = tsServerPort;
S
Shengliang Guan 已提交
497
  if (0 == strcmp("speed", role)) {
498 499
    if (pkgLen <= MIN_SPEED_PKG_LEN) pkgLen = MIN_SPEED_PKG_LEN;
    if (pkgLen > MAX_SPEED_PKG_LEN) pkgLen = MAX_SPEED_PKG_LEN;
500 501
    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 已提交
502
  } else {
503 504 505
    if (pkgLen <= 10) pkgLen = 1000;
    if (pkgLen > MAX_PKG_LEN) pkgLen = MAX_PKG_LEN;
  }
506 507 508 509 510

  if (0 == strcmp("client", role)) {
    taosNetTestClient(host, port, pkgLen);
  } else if (0 == strcmp("server", role)) {
    taosNetTestServer(host, port, pkgLen);
511
  } else if (0 == strcmp("speed", role)) {
512
    tsLogEmbedded = 0;
513
    char type[10] = {0};
514
    taosNetCheckSpeed(host, port, pkgLen, pkgNum, strtolower(type, pkgType));
H
Hui Li 已提交
515
  } else {
S
Shengliang Guan 已提交
516
    TASSERT(1);
H
Hui Li 已提交
517 518
  }

519
  tsLogEmbedded = 0;
H
Hui Li 已提交
520
}
521 522

#endif