wangzhengquan
2020-12-01 95c305005862acb9b611ec2c65e5843c0e19176c
update
5个文件已修改
286 ■■■■■ 已修改文件
src/socket/net_conn_pool.h 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/socket/net_mod_server_socket.c 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/socket/net_mod_socket.c 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test_net_socket/net_mod_socket.sh 19 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test_net_socket/test_net_mod_socket.c 260 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/socket/net_conn_pool.h
@@ -1,6 +1,11 @@
/**
 * 配合thread local使用的connection pool
 */
#ifndef __NET_CONN_POOL_H__
#define __NET_CONN_POOL_H__ 
#include "usg_common.h"
#include <poll.h>
src/socket/net_mod_server_socket.c
@@ -138,6 +138,7 @@
  }
  request_head = NetModSocket::decode_request_head(request_head_bs);
printf("server received request from host = %s:%d, key = %d, timeout=%d,\n", 
  request_head.host, request_head.port , request_head.key, request_head.timeout);
src/socket/net_mod_socket.c
@@ -303,6 +303,7 @@
        continue;
      }
      request_head.mod = BUS;
      memcpy(request_head.host, node->host, sizeof(request_head.host));
      request_head.key = node->key;
      request_head.content_length = content_size;
      request_head.topic_length = strlen(topic) + 1;
test_net_socket/net_mod_socket.sh
@@ -23,9 +23,15 @@
     
}
function mclient() {
    ./test_net_mod_socket --fun="start_net_mclient" \
     --sendlist="localhost:5000:11"
function msend() {
    ./test_net_mod_socket --fun="test_net_sendandrecv_threads" \
     --sendlist="localhost:5000:11, localhost:5000:11"
}
function mpub() {
    ./test_net_mod_socket --fun="test_net_pub_threads" \
     --publist="localhost:5000:8, localhost:5000:8"
     
}
@@ -48,8 +54,11 @@
  "client")
     client
  ;;
  "mclient")
    mclient
  "msend")
    msend
  ;;
  "mpub")
    mpub
  ;;
  "close")
     close
test_net_socket/test_net_mod_socket.c
@@ -5,7 +5,7 @@
#include <getopt.h>
typedef struct Targ {
    char *sendlist;
    char *nodelist;
    int id;
}Targ;
@@ -140,90 +140,7 @@
  
}
#define  SCALE  100000
void *runclient(void *arg) {
  Targ *targ = (Targ *)arg;
  char sendbuf[512];
  int i,j, n, recv_arr_size;
  net_mod_recv_msg_t *recv_arr;
  net_node_t *node_arr;
  int node_arr_size = parse_node_list(targ->sendlist, &node_arr);
  void * client = net_mod_socket_open();
    char filename[512];
    sprintf(filename, "test%d.tmp", targ->id);
    FILE *fp = NULL;
    fp = fopen(filename, "w+");
    // fp = stdout;
    int recvsize;
    void *recvbuf;
  for (i = 0; i < SCALE; i++) {
    sprintf(sendbuf, "thread(%d) %d", targ->id, i);
    fprintf(fp, "requst:%s\n", sendbuf);
    n = net_mod_socket_sendandrecv(client, node_arr, node_arr_size, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size);
    //printf("send %d nodes\n", n);
    for(j=0; j < recv_arr_size; j++) {
        fprintf(fp, "reply: host:%s, port: %d, key:%d, content: %s\n",
            recv_arr[j].host,
            recv_arr[j].port,
            recv_arr[j].key,
            recv_arr[j].content
        );
    }
        // 使用完后,不要忘记释放掉
        net_mod_socket_free_recv_msg_arr(recv_arr, recv_arr_size);
  }
  fclose(fp);
  net_mod_socket_close(client);
  return (void *)i;
}
void start_net_mclient(char *sendlist) {
  int status, i = 0, processors = 4;
  void *res[processors];
  // Targ *targs = (Targ *)calloc(processors, sizeof(Targ));
  Targ targs[processors];
  pthread_t tids[processors];
  char sendbuf[512];
  struct timeval start, end;
  long total = 0;
printf("开始测试...\n");
  gettimeofday(&start, NULL);
  for (i = 0; i < processors; i++) {
    targs[i].sendlist = sendlist;
    targs[i].id = i;
    pthread_create(&tids[i], NULL, runclient, (void *)&targs[i]);
  }
  for (i = 0; i < processors; i++) {
    if (pthread_join(tids[i], &res[i]) != 0) {
      perror("multyThreadClient pthread_join");
    } else {
        total += (long)res[i];
      //fprintf(stderr, "client(%d) 写入 %ld 条数据\n", i, (long)res[i]);
    }
  }
  gettimeofday(&end, NULL);
  double difftime = end.tv_sec * 1000000 + end.tv_usec - (start.tv_sec * 1000000 + start.tv_usec);
  long diffsec = (long) (difftime/1000000);
  long diffusec = difftime - diffsec*1000000;
  fprintf(stderr,"发送数目: %ld, 用时: (%ld sec %ld usec), 平均: %f\n", total, diffsec, diffusec, difftime/total );
  // fflush(stdout);
}
void start_bus_server(int key) {
  printf("Start bus server\n");
@@ -251,6 +168,164 @@
    net_mod_socket_sendto(socket, sendbuf, strlen(sendbuf) + 1, remote_port);
    free(recvbuf);
  }
}
#define  SCALE  100000
void *_run_sendandrecv_(void *arg) {
  Targ *targ = (Targ *)arg;
  char sendbuf[512];
  int i,j, n, recv_arr_size;
  net_mod_recv_msg_t *recv_arr;
  int total = 0;
  net_node_t *node_arr;
  int node_arr_size = parse_node_list(targ->nodelist, &node_arr);
  void * client = net_mod_socket_open();
    char filename[512];
    sprintf(filename, "test%d.tmp", targ->id);
    FILE *fp = NULL;
    fp = fopen(filename, "w+");
    // fp = stdout;
    int recvsize;
    void *recvbuf;
  for (i = 0; i < SCALE; i++) {
    sprintf(sendbuf, "thread(%d) %d", targ->id, i);
    fprintf(fp, "requst:%s\n", sendbuf);
    n = net_mod_socket_sendandrecv(client, node_arr, node_arr_size, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size);
    //printf("send %d nodes\n", n);
    for(j=0; j < recv_arr_size; j++) {
        fprintf(fp, "reply: host:%s, port: %d, key:%d, content: %s\n",
            recv_arr[j].host,
            recv_arr[j].port,
            recv_arr[j].key,
            recv_arr[j].content
        );
    }
        // 使用完后,不要忘记释放掉
        net_mod_socket_free_recv_msg_arr(recv_arr, recv_arr_size);
    total += n;
  }
  fclose(fp);
  net_mod_socket_close(client);
  return (void *)total;
}
void test_net_sendandrecv_threads(char *nodelist) {
  int status, i = 0, processors = 4;
  void *res[processors];
  // Targ *targs = (Targ *)calloc(processors, sizeof(Targ));
  Targ targs[processors];
  pthread_t tids[processors];
  char sendbuf[512];
  struct timeval start, end;
  long total = 0;
  printf("开始测试...\n");
  gettimeofday(&start, NULL);
  for (i = 0; i < processors; i++) {
    targs[i].nodelist = nodelist;
    targs[i].id = i;
    pthread_create(&tids[i], NULL, _run_sendandrecv_, (void *)&targs[i]);
  }
  for (i = 0; i < processors; i++) {
    if (pthread_join(tids[i], &res[i]) != 0) {
      perror("multyThreadClient pthread_join");
    } else {
        total += (long)res[i];
      //fprintf(stderr, "client(%d) 写入 %ld 条数据\n", i, (long)res[i]);
    }
  }
  gettimeofday(&end, NULL);
  double difftime = end.tv_sec * 1000000 + end.tv_usec - (start.tv_sec * 1000000 + start.tv_usec);
  long diffsec = (long) (difftime/1000000);
  long diffusec = difftime - diffsec*1000000;
  fprintf(stderr,"发送数目: %ld, 用时: (%ld sec %ld usec), 平均: %f\n", total, diffsec, diffusec, difftime/total );
  // fflush(stdout);
}
void *_run_pub_(void *arg) {
  Targ *targ = (Targ *)arg;
  char sendbuf[512];
  int i,j, n;
  int total = 0;
  net_node_t *node_arr;
  int node_arr_size = parse_node_list(targ->nodelist, &node_arr);
  char *topic = "news";
  void * client = net_mod_socket_open();
  // char filename[512];
  // sprintf(filename, "test%d.tmp", targ->id);
  // FILE *fp = NULL;
  // fp = fopen(filename, "w+");
  // fp = stdout;
  for (i = 0; i < SCALE; i++) {
    sprintf(sendbuf, "thread(%d) %d", targ->id, i);
    n = net_mod_socket_pub(client, node_arr, node_arr_size, topic, strlen(topic)+1, sendbuf, strlen(sendbuf)+1);
    // n = net_mod_socket_pub(client, node_arr, node_arr_size, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size);
    printf( "pub:%s to  %d nodes\n", sendbuf, n);
    total += n;
  }
  // fclose(fp);
  net_mod_socket_close(client);
  return (void *)total;
}
void test_net_pub_threads(char *nodelist) {
  int status, i = 0, processors = 4;
  void *res[processors];
  // Targ *targs = (Targ *)calloc(processors, sizeof(Targ));
  Targ targs[processors];
  pthread_t tids[processors];
  char sendbuf[512];
  struct timeval start, end;
  long total = 0;
printf("开始测试...\n");
  gettimeofday(&start, NULL);
  for (i = 0; i < processors; i++) {
    targs[i].nodelist = nodelist;
    targs[i].id = i;
    pthread_create(&tids[i], NULL, _run_pub_, (void *)&targs[i]);
  }
  for (i = 0; i < processors; i++) {
    if (pthread_join(tids[i], &res[i]) != 0) {
      perror("multyThreadClient pthread_join");
    } else {
      total += (long)res[i];
      //fprintf(stderr, "client(%d) 写入 %ld 条数据\n", i, (long)res[i]);
    }
  }
  gettimeofday(&end, NULL);
  double difftime = end.tv_sec * 1000000 + end.tv_usec - (start.tv_sec * 1000000 + start.tv_usec);
  long diffsec = (long) (difftime/1000000);
  long diffusec = difftime - diffsec*1000000;
  fprintf(stderr,"发送数目: %ld, 用时: (%ld sec %ld usec), 平均: %f\n", total, diffsec, diffusec, difftime/total );
  // fflush(stdout);
}
@@ -299,15 +374,25 @@
    }
    start_net_client(opt.sendlist, opt.publist);
  }
  else if (strcmp("start_net_mclient", opt.fun) == 0) {
  else if (strcmp("test_net_sendandrecv_threads", opt.fun) == 0) {
    if(opt.sendlist == 0) {
      fprintf(stderr, "Missing sendlist .\n");
      usage(argv[0]);
      exit(1);
    }
     
    start_net_mclient(opt.sendlist);
    test_net_sendandrecv_threads(opt.sendlist);
  }
  else if (strcmp("test_net_pub_threads", opt.fun) == 0) {
    if(opt.publist == 0) {
      fprintf(stderr, "Missing publist .\n");
      usage(argv[0]);
      exit(1);
    }
    test_net_pub_threads(opt.publist);
  }
  else {
    usage(argv[0]);
    exit(1);
@@ -327,6 +412,8 @@
  fpe("-f, --funciton                       Function name\n");
  fpe("-p, --port                           TCP/IP Port\n");
  fpe("-k, --key                            SHM Key\n");
  fpe("--sendlist                           format:--sendlist=\"192.168.5.10:5000:11, 192.168.5.22:5000:11, 192.168.20.104:5000:11\"\n");
  fpe("--publist                            format: --publist=\"192.168.5.10:5000:8, 192.168.5.22:5000:8, 192.168.20.104:5000:8\"\n");
  fpe("\n");
}
@@ -345,7 +432,6 @@
    usage(argv[0]);
    exit(0);
  }
  
  argument_t mopt = {};