shellNettest.c 3.6 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 20 21 22 23 24
static void shellWorkAsClient() {
  SRpcInit rpcInit = {0};
  SEpSet   epSet = {.inUse = 0, .numOfEps = 1};
  SRpcMsg  rpcRsp = {0};
  void    *clientRpc = NULL;
  char     pass[TSDB_PASSWORD_LEN + 1] = {0};
25

26 27 28 29 30 31 32 33 34 35
  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 已提交
36

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

44 45
  tstrncpy(epSet.eps[0].fqdn, shell.args.host, TSDB_FQDN_LEN);
  epSet.eps[0].port = (uint16_t)shell.args.port;
H
Hui Li 已提交
46

47 48
  int32_t  totalSucc = 0;
  uint64_t startTime = taosGetTimestampUs();
H
Hui Li 已提交
49

50 51 52 53
  for (int32_t i = 0; i < shell.args.pktNum; ++i) {
    SRpcMsg rpcMsg = {.ahandle = (void *)0x9525, .msgType = TDMT_DND_NET_TEST};
    rpcMsg.pCont = rpcMallocCont(shell.args.pktLen);
    rpcMsg.contLen = shell.args.pktLen;
54

55 56 57
    printf("net test request is sent, size:%d\n", rpcMsg.contLen);
    rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
    printf("net test response is received, size:%d\n", rpcMsg.contLen);
H
Hui Li 已提交
58

59
    if (rpcRsp.code == 0) totalSucc++;
H
Hui Li 已提交
60

61 62
    rpcFreeCont(rpcRsp.pCont);
    rpcRsp.pCont = NULL;
H
Hui Li 已提交
63 64
  }

65 66
  uint64_t endTime = taosGetTimestampUs();
  uint64_t elT = endTime - startTime;
67

68 69
  printf("\ntotal succ:%5d/%d\tcost:%8.2lf ms\tspeed:%8.2lf MB/s\n", totalSucc, shell.args.pktNum, elT / 1000.0,
         shell.args.pktLen / (elT / 1000000.0) / 1024.0 / 1024.0 * totalSucc);
H
Hui Li 已提交
70

71 72 73
_OVER:
  if (clientRpc != NULL) {
    rpcClose(clientRpc);
74
  }
75 76
  if (rpcRsp.pCont != NULL) {
    rpcFreeCont(rpcRsp.pCont);
S
Shengliang Guan 已提交
77
  }
H
Hui Li 已提交
78 79
}

80 81 82 83 84 85
static void shellProcessMsg(void *p, SRpcMsg *pRpc, SEpSet *pEpSet) {
  printf("net test request is received, size:%d\n", pRpc->contLen);
  SRpcMsg rsp = {.handle = pRpc->handle, .ahandle = pRpc->ahandle, .code = 0};
  rsp.pCont = rpcMallocCont(shell.args.pktLen);
  rsp.contLen = shell.args.pktLen;
  rpcSendResponse(&rsp);
H
Hui Li 已提交
86 87
}

88
void shellNettestHandler(int32_t signum, void *sigInfo, void *context) { shellExit(); }
89

90 91 92 93 94 95 96 97 98
static void shellWorkAsServer() {
  SRpcInit rpcInit = {0};
  rpcInit.localPort = shell.args.port;
  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 已提交
99

100 101 102
  void *serverRpc = rpcOpen(&rpcInit);
  if (serverRpc == NULL) {
    printf("failed to init net test server since %s", terrstr());
103
  }
104

105
  printf("net test server is initialized\n");
106

107 108
  taosSetSignal(SIGTERM, shellNettestHandler);
  while (1) taosMsleep(10);
109 110
}

111 112 113
void shellTestNetWork() {
  if (strcmp(shell.args.netrole, "client") == 0) {
    shellWorkAsClient();
114
  }
115

116 117
  if (strcmp(shell.args.netrole, "server") == 0) {
    shellWorkAsServer();
H
Hui Li 已提交
118 119
  }
}