shellNettest.c 4.2 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";
H
Hui Li 已提交
34

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

41 42 43 44 45 46 47 48 49 50 51
  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 已提交
52
  printf("network test client is initialized, the server is %s:%u\r\n", fqdn, pArgs->port);
53

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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