shellNettest.c 4.3 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
#define _GNU_SOURCE
#include "shellInt.h"

19
static void shellWorkAsClient() {
20 21 22 23
  SShellArgs *pArgs = &shell.args;
  SRpcInit    rpcInit = {0};
  SEpSet      epSet = {.inUse = 0, .numOfEps = 1};
  SRpcMsg     rpcRsp = {0};
dengyihao's avatar
dengyihao 已提交
24
  void       *clientRpc = NULL;
25
  char        pass[TSDB_PASSWORD_LEN + 1] = {0};
26

27 28 29 30 31 32 33
  taosEncryptPass_c((uint8_t *)("_pwd"), strlen("_pwd"), pass);
  rpcInit.label = "CHK";
  rpcInit.numOfThreads = 1;
  rpcInit.sessions = 16;
  rpcInit.connType = TAOS_CONN_CLIENT;
  rpcInit.idleTime = tsShellActivityTimer * 1000;
  rpcInit.user = "_dnd";
dengyihao's avatar
dengyihao 已提交
34
  rpcInit.timeToGetConn = tsTimeToGetAvailableConn;
H
Hui Li 已提交
35

36 37
  clientRpc = rpcOpen(&rpcInit);
  if (clientRpc == NULL) {
wafwerar's avatar
wafwerar 已提交
38
    printf("failed to init net test client since %s\r\n", terrstr());
39
    goto _OVER;
H
Hui Li 已提交
40 41
  }

42 43 44 45 46 47 48 49 50 51 52
  if (pArgs->host == NULL) {
    pArgs->host = tsFirst;
  }
  char fqdn[TSDB_FQDN_LEN] = {0};
  tstrncpy(fqdn, pArgs->host, TSDB_FQDN_LEN);
  strtok(fqdn, ":");

  if (pArgs->port == 0) {
    pArgs->port = tsServerPort;
  }

wafwerar's avatar
wafwerar 已提交
53
  printf("network test client is initialized, the server is %s:%u\r\n", fqdn, pArgs->port);
54

55
  tstrncpy(epSet.eps[0].fqdn, fqdn, TSDB_FQDN_LEN);
56
  epSet.eps[0].port = (uint16_t)pArgs->port;
H
Hui Li 已提交
57

58 59
  int32_t  totalSucc = 0;
  uint64_t startTime = taosGetTimestampUs();
H
Hui Li 已提交
60

61
  for (int32_t i = 0; i < pArgs->pktNum; ++i) {
S
Shengliang Guan 已提交
62
    SRpcMsg rpcMsg = {.info.ahandle = (void *)0x9525, .msgType = TDMT_DND_NET_TEST};
63 64
    rpcMsg.pCont = rpcMallocCont(pArgs->pktLen);
    rpcMsg.contLen = pArgs->pktLen;
65

wafwerar's avatar
wafwerar 已提交
66
    printf("request is sent, size:%d\r\n", rpcMsg.contLen);
67
    rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
68
    if (rpcRsp.code == 0 && rpcRsp.contLen == rpcMsg.contLen) {
wafwerar's avatar
wafwerar 已提交
69
      printf("response is received, size:%d\r\n", rpcMsg.contLen);
70 71
      if (rpcRsp.code == 0) totalSucc++;
    } else {
wafwerar's avatar
wafwerar 已提交
72
      printf("response not received since %s\r\n", tstrerror(rpcRsp.code));
73
    }
H
Hui Li 已提交
74

75 76
    rpcFreeCont(rpcRsp.pCont);
    rpcRsp.pCont = NULL;
H
Hui Li 已提交
77 78
  }

79 80
  uint64_t endTime = taosGetTimestampUs();
  uint64_t elT = endTime - startTime;
81

wafwerar's avatar
wafwerar 已提交
82
  printf("\r\ntotal succ:%5d/%d\tcost:%8.2lf ms\tspeed:%8.2lf MB/s\r\n", totalSucc, pArgs->pktNum, elT / 1000.0,
83
         pArgs->pktLen / (elT / 1000000.0) / 1024.0 / 1024.0 * totalSucc);
H
Hui Li 已提交
84

85 86 87
_OVER:
  if (clientRpc != NULL) {
    rpcClose(clientRpc);
88
  }
89 90
  if (rpcRsp.pCont != NULL) {
    rpcFreeCont(rpcRsp.pCont);
S
Shengliang Guan 已提交
91
  }
H
Hui Li 已提交
92 93
}

94
static void shellProcessMsg(void *p, SRpcMsg *pRpc, SEpSet *pEpSet) {
wafwerar's avatar
wafwerar 已提交
95
  printf("request is received, size:%d\r\n", pRpc->contLen);
96
  fflush(stdout);
S
Shengliang Guan 已提交
97
  SRpcMsg rsp = {.info = pRpc->info, .code = 0};
98 99 100 101 102 103
  rsp.pCont = rpcMallocCont(pRpc->contLen);
  if (rsp.pCont == NULL) {
    rsp.code = TSDB_CODE_OUT_OF_MEMORY;
  } else {
    rsp.contLen = pRpc->contLen;
  }
104
  rpcSendResponse(&rsp);
H
Hui Li 已提交
105 106
}

107
void shellNettestHandler(int32_t signum, void *sigInfo, void *context) { shellExit(); }
108

109
static void shellWorkAsServer() {
110 111
  SShellArgs *pArgs = &shell.args;

112 113 114 115
  if (pArgs->port == 0) {
    pArgs->port = tsServerPort;
  }

116
  SRpcInit rpcInit = {0};
dengyihao's avatar
dengyihao 已提交
117
  memcpy(rpcInit.localFqdn, tsLocalFqdn, strlen(tsLocalFqdn));
118
  rpcInit.localPort = pArgs->port;
119
  rpcInit.label = "CHK";
S
Shengliang Guan 已提交
120
  rpcInit.numOfThreads = 2;
121 122 123 124
  rpcInit.cfp = (RpcCfp)shellProcessMsg;
  rpcInit.sessions = 10;
  rpcInit.connType = TAOS_CONN_SERVER;
  rpcInit.idleTime = tsShellActivityTimer * 1000;
H
Hui Li 已提交
125

126 127
  void *serverRpc = rpcOpen(&rpcInit);
  if (serverRpc == NULL) {
wafwerar's avatar
wafwerar 已提交
128
    printf("failed to init net test server since %s\r\n", terrstr());
129
  } else {
wafwerar's avatar
wafwerar 已提交
130
    printf("network test server is initialized, port:%u\r\n", pArgs->port);
131 132
    taosSetSignal(SIGTERM, shellNettestHandler);
    while (1) taosMsleep(10);
133 134 135
  }
}

136 137 138
void shellTestNetWork() {
  if (strcmp(shell.args.netrole, "client") == 0) {
    shellWorkAsClient();
139
  }
140

141 142
  if (strcmp(shell.args.netrole, "server") == 0) {
    shellWorkAsServer();
H
Hui Li 已提交
143 144
  }
}