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 34 35 36
  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";
  rpcInit.ckey = "_key";
  rpcInit.spi = 1;
  rpcInit.secret = pass;
H
Hui Li 已提交
37

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

44 45 46 47 48 49 50 51 52 53 54
  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;
  }

55
  printf("network test client is initialized, the server is %s:%u\n", fqdn, pArgs->port);
56

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

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

63
  for (int32_t i = 0; i < pArgs->pktNum; ++i) {
64
    SRpcMsg rpcMsg = {.ahandle = (void *)0x9525, .msgType = TDMT_DND_NET_TEST};
65 66
    rpcMsg.pCont = rpcMallocCont(pArgs->pktLen);
    rpcMsg.contLen = pArgs->pktLen;
67

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

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

81 82
  uint64_t endTime = taosGetTimestampUs();
  uint64_t elT = endTime - startTime;
83

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

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

96
static void shellProcessMsg(void *p, SRpcMsg *pRpc, SEpSet *pEpSet) {
97 98
  printf("request is received, size:%d\n", pRpc->contLen);
  fflush(stdout);
99
  SRpcMsg rsp = {.handle = pRpc->handle, .refId = pRpc->refId, .ahandle = pRpc->ahandle, .code = 0};
100 101 102 103 104 105
  rsp.pCont = rpcMallocCont(pRpc->contLen);
  if (rsp.pCont == NULL) {
    rsp.code = TSDB_CODE_OUT_OF_MEMORY;
  } else {
    rsp.contLen = pRpc->contLen;
  }
106
  rpcSendResponse(&rsp);
H
Hui Li 已提交
107 108
}

109
void shellNettestHandler(int32_t signum, void *sigInfo, void *context) { shellExit(); }
110

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

114 115 116 117
  if (pArgs->port == 0) {
    pArgs->port = tsServerPort;
  }

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

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

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

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