client_performance_test.cpp 3.2 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
羽飞's avatar
羽飞 已提交
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 28 29 30 31 32
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Longda on 2021
//

#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#include "common/defs.h"
#include "common/metrics/metrics.h"
#include "common/metrics/metrics_registry.h"
#include "common/metrics/console_reporter.h"

#define MAX_MEM_BUFFER_SIZE 8192
33
#define PORT_DEFAULT 6789
羽飞's avatar
羽飞 已提交
34 35 36

using namespace common;
char *server_host = (char *)LOCAL_HOST;
37
int server_port = PORT_DEFAULT;
羽飞's avatar
羽飞 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

void *test_server(void *param)
{
  Meter *tps_meter = (Meter *)param;

  std::cout << "Begin to connect server. " << std::endl;
  int sockfd, sendbytes;
  // char send[MAXLINE];

  char send_buf[MAX_MEM_BUFFER_SIZE] = {0};
  char recv_buf[MAX_MEM_BUFFER_SIZE] = {0};
  snprintf(send_buf, sizeof(send_buf), "%s", "select count(*) from test");
  // char buf[MAXDATASIZE];
  struct hostent *host;
  struct sockaddr_in serv_addr;

  if ((host = gethostbyname(server_host)) == NULL) {
    perror("gethostbyname");
    exit(1);
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket error \n");
    exit(1);
  }

  serv_addr.sin_family = AF_INET;
64
  serv_addr.sin_port = htons((uint16_t)server_port);
羽飞's avatar
羽飞 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
  bzero(&(serv_addr.sin_zero), 8);

  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1) {
    perror("Failed to connect \n");
    exit(1);
  }

  while (true) {
    if ((sendbytes = send(sockfd, send_buf, strlen(send_buf) + 1, 0)) == -1) {
      perror("send error \n");
      exit(1);
    }

    memset(recv_buf, 0, sizeof(recv_buf));

    int len = recv(sockfd, recv_buf, sizeof(recv_buf), 0);
    if (len < 0) {
      printf("connection exception\n");
      break;
    }
    if (len == 0) {
      printf("Connection has been closed\n");
      break;
    }

    tps_meter->inc();
  }
  close(sockfd);
  return NULL;
}

int main(int argc, char *argv[])
{
99 100 101 102 103 104 105 106 107 108 109
  int opt;
  extern char *optarg;
  while ((opt = getopt(argc, argv, "h:p:")) > 0) {
    switch (opt) {
      case 'p':
        server_port = atoi(optarg);
        break;
      case 'h':
        server_host = optarg;
        break;
    }
羽飞's avatar
羽飞 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
  }

  MetricsRegistry &metric_registry = get_metrics_registry();
  ConsoleReporter *console_reporter = get_console_reporter();
  metric_registry.add_reporter(console_reporter);

  Meter *tps_meter = new Meter();

  metric_registry.register_metric("client.sendtps", tps_meter);

  for (int i = 0; i < 8; i++) {
    pthread_t pid;
    pthread_create(&pid, NULL, test_server, tps_meter);
  }

  while (1) {
    sleep(60);
    metric_registry.snapshot();
    metric_registry.report();
  }
  return 0;
}