syncClient.c 6.5 KB
Newer Older
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * 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/>.
 */

#include "os.h"
#include "tglobal.h"
#include "tulog.h"
#include "trpc.h"
#include "taoserror.h"

typedef struct {
  int       index;
  SRpcEpSet epSet;
  int       num;
  int       numOfReqs;
  int       msgSize;
S
TD-1617  
Shengliang Guan 已提交
28 29
  tsem_t    rspSem;
  tsem_t *  pOverSem;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
30
  pthread_t thread;
S
TD-1617  
Shengliang Guan 已提交
31
  void *    pRpc;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
32 33 34 35
} SInfo;

void processResponse(SRpcMsg *pMsg, SRpcEpSet *pEpSet) {
  SInfo *pInfo = (SInfo *)pMsg->ahandle;
S
TD-1617  
Shengliang Guan 已提交
36 37
  uDebug("thread:%d, response is received, type:%d contLen:%d code:0x%x", pInfo->index, pMsg->msgType, pMsg->contLen,
         pMsg->code);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
38 39 40 41

  if (pEpSet) pInfo->epSet = *pEpSet;
  rpcFreeCont(pMsg->pCont);

S
TD-1617  
Shengliang Guan 已提交
42
  tsem_post(&pInfo->rspSem);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
43 44 45 46 47
}

int tcount = 0;

void *sendRequest(void *param) {
S
TD-1617  
Shengliang Guan 已提交
48 49 50
  SInfo * pInfo = (SInfo *)param;
  SRpcMsg rpcMsg = {0};

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
51 52
  uDebug("thread:%d, start to send request", pInfo->index);

S
TD-1617  
Shengliang Guan 已提交
53
  while (pInfo->numOfReqs == 0 || pInfo->num < pInfo->numOfReqs) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
54 55 56 57 58 59 60
    pInfo->num++;
    rpcMsg.pCont = rpcMallocCont(pInfo->msgSize);
    rpcMsg.contLen = pInfo->msgSize;
    rpcMsg.ahandle = pInfo;
    rpcMsg.msgType = 1;
    uDebug("thread:%d, send request, contLen:%d num:%d", pInfo->index, pInfo->msgSize, pInfo->num);
    rpcSendRequest(pInfo->pRpc, &pInfo->epSet, &rpcMsg);
S
TD-1617  
Shengliang Guan 已提交
61
    if (pInfo->num % 20000 == 0) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
62
      uInfo("thread:%d, %d requests have been sent", pInfo->index, pInfo->num);
S
TD-1617  
Shengliang Guan 已提交
63
    }
S
TD-1057  
Shengliang Guan 已提交
64
    tsem_wait(&pInfo->rspSem);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
65 66 67 68 69 70 71 72 73 74 75 76
  }

  uDebug("thread:%d, it is over", pInfo->index);
  tcount++;

  return NULL;
}

int main(int argc, char *argv[]) {
  SRpcInit  rpcInit;
  SRpcEpSet epSet;
  char      secret[TSDB_KEY_LEN] = "mypassword";
S
TD-1617  
Shengliang Guan 已提交
77 78 79 80 81 82
  int       msgSize = 128;
  int       numOfReqs = 0;
  int       appThreads = 1;
  char      serverIp[40] = "127.0.0.1";
  struct    timeval systemTime;
  int64_t   startTime, endTime;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  pthread_attr_t thattr;

  // server info
  epSet.numOfEps = 1;
  epSet.inUse = 0;
  epSet.port[0] = 7000;
  epSet.port[1] = 7000;
  strcpy(epSet.fqdn[0], serverIp);
  strcpy(epSet.fqdn[1], "192.168.0.1");

  // client info
  memset(&rpcInit, 0, sizeof(rpcInit));
  rpcInit.localPort    = 0;
  rpcInit.label        = "APP";
  rpcInit.numOfThreads = 1;
  rpcInit.cfp          = processResponse;
  rpcInit.sessions     = 100;
  rpcInit.idleTime     = tsShellActivityTimer*1000;
  rpcInit.user         = "michael";
  rpcInit.secret       = secret;
  rpcInit.ckey         = "key";
  rpcInit.spi          = 1;
  rpcInit.connType     = TAOS_CONN_CLIENT;

S
TD-1617  
Shengliang Guan 已提交
107 108
  for (int i = 1; i < argc; ++i) {
    if (strcmp(argv[i], "-p") == 0 && i < argc - 1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
109
      epSet.port[0] = atoi(argv[++i]);
S
TD-1617  
Shengliang Guan 已提交
110 111 112
    } else if (strcmp(argv[i], "-i") == 0 && i < argc - 1) {
      tstrncpy(epSet.fqdn[0], argv[++i], TSDB_FQDN_LEN);
    } else if (strcmp(argv[i], "-t") == 0 && i < argc - 1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
113
      rpcInit.numOfThreads = atoi(argv[++i]);
S
TD-1617  
Shengliang Guan 已提交
114
    } else if (strcmp(argv[i], "-m") == 0 && i < argc - 1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
115
      msgSize = atoi(argv[++i]);
S
TD-1617  
Shengliang Guan 已提交
116 117 118 119 120 121 122 123 124
    } else if (strcmp(argv[i], "-s") == 0 && i < argc - 1) {
      rpcInit.sessions = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-n") == 0 && i < argc - 1) {
      numOfReqs = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-a") == 0 && i < argc - 1) {
      appThreads = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-o") == 0 && i < argc - 1) {
      tsCompressMsgSize = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-u") == 0 && i < argc - 1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
125
      rpcInit.user = argv[++i];
S
TD-1617  
Shengliang Guan 已提交
126
    } else if (strcmp(argv[i], "-k") == 0 && i < argc - 1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
127
      rpcInit.secret = argv[++i];
S
TD-1617  
Shengliang Guan 已提交
128
    } else if (strcmp(argv[i], "-spi") == 0 && i < argc - 1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
129
      rpcInit.spi = atoi(argv[++i]);
S
TD-1617  
Shengliang Guan 已提交
130
    } else if (strcmp(argv[i], "-d") == 0 && i < argc - 1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
      rpcDebugFlag = atoi(argv[++i]);
    } else {
      printf("\nusage: %s [options] \n", argv[0]);
      printf("  [-i ip]: first server IP address, default is:%s\n", serverIp);
      printf("  [-p port]: server port number, default is:%d\n", epSet.port[0]);
      printf("  [-t threads]: number of rpc threads, default is:%d\n", rpcInit.numOfThreads);
      printf("  [-s sessions]: number of rpc sessions, default is:%d\n", rpcInit.sessions);
      printf("  [-m msgSize]: message body size, default is:%d\n", msgSize);
      printf("  [-a threads]: number of app threads, default is:%d\n", appThreads);
      printf("  [-n requests]: number of requests per thread, default is:%d\n", numOfReqs);
      printf("  [-o compSize]: compression message size, default is:%d\n", tsCompressMsgSize);
      printf("  [-u user]: user name for the connection, default is:%s\n", rpcInit.user);
      printf("  [-k secret]: password for the connection, default is:%s\n", rpcInit.secret);
      printf("  [-spi SPI]: security parameter index, default is:%d\n", rpcInit.spi);
      printf("  [-d debugFlag]: debug flag, default:%d\n", rpcDebugFlag);
      printf("  [-h help]: print out this help\n\n");
      exit(0);
    }
  }

  taosInitLog("client.log", 100000, 10);

  void *pRpc = rpcOpen(&rpcInit);
  if (pRpc == NULL) {
    uError("failed to initialize RPC");
    return -1;
  }

  uInfo("client is initialized");

  gettimeofday(&systemTime, NULL);
S
TD-1617  
Shengliang Guan 已提交
162 163 164
  startTime = systemTime.tv_sec * 1000000 + systemTime.tv_usec;

  SInfo *pInfo = (SInfo *)calloc(1, sizeof(SInfo) * appThreads);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
165 166 167 168

  pthread_attr_init(&thattr);
  pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE);

S
TD-1617  
Shengliang Guan 已提交
169
  for (int i = 0; i < appThreads; ++i) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
170 171 172 173
    pInfo->index = i;
    pInfo->epSet = epSet;
    pInfo->numOfReqs = numOfReqs;
    pInfo->msgSize = msgSize;
S
TD-1057  
Shengliang Guan 已提交
174
    tsem_init(&pInfo->rspSem, 0, 0);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
175 176 177 178 179 180 181
    pInfo->pRpc = pRpc;
    pthread_create(&pInfo->thread, &thattr, sendRequest, pInfo);
    pInfo++;
  }

  do {
    usleep(1);
S
TD-1617  
Shengliang Guan 已提交
182
  } while (tcount < appThreads);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
183 184

  gettimeofday(&systemTime, NULL);
S
TD-1617  
Shengliang Guan 已提交
185 186
  endTime = systemTime.tv_sec * 1000000 + systemTime.tv_usec;
  float usedTime = (endTime - startTime) / 1000.0;  // mseconds
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
187

S
TD-1617  
Shengliang Guan 已提交
188 189
  uInfo("it takes %.3f mseconds to send %d requests to server", usedTime, numOfReqs * appThreads);
  uInfo("Performance: %.3f requests per second, msgSize:%d bytes", 1000.0 * numOfReqs * appThreads / usedTime, msgSize);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
190 191 192 193 194

  taosCloseLog();

  return 0;
}