From 328f10ca7556f6523ca4ff2b095b1c4586147fb2 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期三, 02 十二月 2020 13:37:14 +0800
Subject: [PATCH] update
---
test_net_socket/test_net_mod_socket.c | 184 +++++++++++++++++++++++++++++++++++----------
1 files changed, 141 insertions(+), 43 deletions(-)
diff --git a/test_net_socket/test_net_mod_socket.c b/test_net_socket/test_net_mod_socket.c
index ba494b5..ab16c93 100644
--- a/test_net_socket/test_net_mod_socket.c
+++ b/test_net_socket/test_net_mod_socket.c
@@ -1,11 +1,13 @@
#include "net_mod_server_socket_wrapper.h"
#include "net_mod_socket_wrapper.h"
-#include "shm_mm.h"
+#include "shm_mm_wraper.h"
#include "usg_common.h"
#include <getopt.h>
+#define SCALE 100000
+
typedef struct Targ {
- char *sendlist;
+ char *nodelist;
int id;
}Targ;
@@ -24,6 +26,10 @@
void usage(char *name);
int parse_node_list(char *str, net_node_t *node_arr_addr[]) ;
void print_node_list(net_node_t *node_arr, int len);
+
+
+
+void * client;
void start_net_proxy(int port) {
printf("Start net proxy\n");
@@ -47,7 +53,7 @@
}
void start_net_client(char *sendlist, char*publist ){
- void * client = net_mod_socket_open();
+ client = net_mod_socket_open();
char content[MAXLINE];
char action[512];
char topic[512];
@@ -87,8 +93,8 @@
if (fgets(content, MAXLINE, stdin) != NULL) {
// 鏀跺埌娑堟伅鐨勮妭鐐瑰嵆浣挎病鏈夊搴旂殑淇℃伅锛� 涔熻鍥炲涓�涓〃绀烘棤鐨勬秷鎭�,鍚﹀垯浼氫竴鐩寸瓑寰�
- n = net_mod_socket_sendandrecv_timeout(client, node_arr, node_arr_size, content,
- strlen(content), &recv_arr, &recv_arr_size, 5000);
+ n = net_mod_socket_sendandrecv(client, node_arr, node_arr_size, content,
+ strlen(content), &recv_arr, &recv_arr_size);
printf("send %d nodes\n", n);
for(i=0; i<recv_arr_size; i++) {
printf("host:%s, port: %d, key:%d, content: %s\n",
@@ -140,24 +146,53 @@
}
+
+
+void start_bus_server(int key) {
+ printf("Start bus server\n");
+ void * server_socket = net_mod_socket_open();
+
+ net_mod_socket_bind(server_socket, key);
+
+ net_mod_socket_start_bus(server_socket);
+}
+
-#define SCALE 100000
+void start_reply(int key) {
+ printf("start reply\n");
+ void *socket = net_mod_socket_open();
+ net_mod_socket_bind(socket, key);
+ int size;
+ void *recvbuf;
+ char sendbuf[512];
+ int rv;
+ int remote_port;
+ while ( (rv = net_mod_socket_recvfrom(socket, &recvbuf, &size, &remote_port) ) == 0) {
+ // printf( "server: RECEIVED REQUEST FROM PORT %d NAME %s\n", remote_port, recvbuf);
+ sprintf(sendbuf, "RECEIVED PORT %d NAME %s", remote_port, recvbuf);
+ net_mod_socket_sendto(socket, sendbuf, strlen(sendbuf) + 1, remote_port);
+ free(recvbuf);
+ }
+}
-void *runclient(void *arg) {
+
+
+
+
+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->sendlist, &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;
@@ -181,13 +216,14 @@
}
// 浣跨敤瀹屽悗锛屼笉瑕佸繕璁伴噴鏀炬帀
net_mod_socket_free_recv_msg_arr(recv_arr, recv_arr_size);
+ total += n;
}
fclose(fp);
- net_mod_socket_close(client);
- return (void *)i;
+
+ return (void *)total;
}
-void start_net_mclient(char *sendlist) {
+void test_net_sendandrecv_threads(char *nodelist) {
int status, i = 0, processors = 4;
void *res[processors];
@@ -198,12 +234,13 @@
struct timeval start, end;
long total = 0;
-printf("寮�濮嬫祴璇�...\n");
+ client = net_mod_socket_open();
+ printf("寮�濮嬫祴璇�...\n");
gettimeofday(&start, NULL);
for (i = 0; i < processors; i++) {
- targs[i].sendlist = sendlist;
+ targs[i].nodelist = nodelist;
targs[i].id = i;
- pthread_create(&tids[i], NULL, runclient, (void *)&targs[i]);
+ pthread_create(&tids[i], NULL, _run_sendandrecv_, (void *)&targs[i]);
}
for (i = 0; i < processors; i++) {
@@ -222,41 +259,88 @@
long diffusec = difftime - diffsec*1000000;
fprintf(stderr,"鍙戦�佹暟鐩�: %ld, 鐢ㄦ椂: (%ld sec %ld usec), 骞冲潎: %f\n", total, diffsec, diffusec, difftime/total );
// fflush(stdout);
+ net_mod_socket_close(client);
}
-void start_bus_server(int key) {
- printf("Start bus server\n");
- void * server_socket = net_mod_socket_open();
-
- net_mod_socket_bind(server_socket, key);
-
- net_mod_socket_start_bus(server_socket);
-}
+
+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";
+
+ // char filename[512];
+ // sprintf(filename, "test%d.tmp", targ->id);
+ // FILE *fp = NULL;
+ // fp = fopen(filename, "w+");
+ // fp = stdout;
-void start_reply(int key) {
- printf("start reply\n");
- void *socket = net_mod_socket_open();
- net_mod_socket_bind(socket, key);
- int size;
- void *recvbuf;
- char sendbuf[512];
- int rv;
- int remote_port;
- while ( (rv = net_mod_socket_recvfrom(socket, &recvbuf, &size, &remote_port) ) == 0) {
- printf( "server: RECEIVED REQUEST FROM PORT %d NAME %s\n", remote_port, recvbuf);
- sprintf(sendbuf, "RECEIVED PORT %d NAME %s", remote_port, recvbuf);
- net_mod_socket_sendto(socket, sendbuf, strlen(sendbuf) + 1, remote_port);
- free(recvbuf);
+
+ 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);
+
+ 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;
+ client = net_mod_socket_open();
+
+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);
+ net_mod_socket_close(client);
}
int main(int argc, char *argv[]) {
- shm_init(512);
+ shm_mm_wrapper_init(512);
argument_t opt = parse_args(argc, argv);
@@ -298,15 +382,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);
@@ -326,6 +420,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");
}
@@ -344,7 +440,6 @@
usage(argv[0]);
exit(0);
}
-
argument_t mopt = {};
@@ -460,6 +555,7 @@
net_node_t *node_arr = (net_node_t *) calloc(entry_arr_len, sizeof(net_node_t));
for(i = 0; i < entry_arr_len; i++) {
property_arr_len = str_split(entry_arr[i], ":", &property_arr);
+ // printf("%s, %s, %s\n", property_arr[0], property_arr[1], property_arr[2]);
node_arr[i] = {trim(property_arr[0], 0), atoi(property_arr[1]), atoi(property_arr[2])};
free(entry_arr[i]);
free(property_arr[1]);
@@ -472,7 +568,9 @@
}
void print_node_list(net_node_t *node_arr, int len) {
+ printf("============node list begin==========\n");
for(int i = 0; i < len; i++) {
printf("%s,%d,%d,\n", node_arr[i].host, node_arr[i].port, node_arr[i].key);
}
+ printf("============node list end==========\n");
}
--
Gitblit v1.8.0