1百w条 发送接收 119秒
0mq 单线程 性能测试 实例
client
1 #include <assert.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <zmq.h>
5 #include <time.h>
6
7 int main()
8 {
9
10 printf("client1!");
11 clock_t start = clock();
12 int rc;
13 void *ctx, *s;
14
15 ctx = zmq_init(1);
16 assert(ctx);
17
18 s = zmq_socket(ctx, ZMQ_REQ);
19 assert(s);
20
21 rc = zmq_connect(s, "tcp://127.0.0.1:5555");
22 assert(rc == 0);
23
24
25 for (int i =0; i < 1000000; ++i)
26 {
27 int rc;
28
29 zmq_msg_t query, resultset;
30
31 // send
32 const char *query_string = "hello ZeroMQ.";
33 rc = zmq_msg_init_size(&query, strlen(query_string)+1);
34 assert(rc == 0);
35 memcpy(zmq_msg_data(&query), query_string, strlen(query_string)+1);
36 rc = zmq_send(s, &query, 0);
37 assert(rc == 0);
38 zmq_msg_close(&query);
39
40
41 // recv
42 rc = zmq_msg_init(&resultset);
43 assert(rc == 0);
44
45 rc = zmq_recv(s, &resultset, 0);
46 assert(rc == 0);
47
48 //printf("ack: %s\n", (const char *)zmq_msg_data(&resultset));
49 zmq_msg_close(&resultset);
50
51 }
52
53 clock_t end = clock();
54
55 double d = (double)(end - start) / CLOCKS_PER_SEC;
56
57 printf("start:%ld end:%ld total:%lf sec\d",start,end, d);
58
59 zmq_close(s);
60 zmq_term(ctx);
61 system("pause");
62 return 0;
63 }
srv
1 //
2 // Hello World server in C++
3 // Binds REP socket to tcp://*:5555
4 // Expects "Hello" from client, replies with "World"
5 //
6
7
8 #include <zmq.hpp>
9 #include <string>
10 #include <iostream>
11 #include <windows.h>
12 #include <Winbase.h>
13 #pragma comment(lib, "Kernel32.lib")
14
15 int main () {
16 // Prepare our context and socket
17 zmq::context_t context (1);
18 zmq::socket_t socket (context, ZMQ_REP);
19 socket.bind ("tcp://*:5555");
20
21 while (true) {
22 zmq::message_t request;
23
24 // Wait for next request from client
25 socket.recv (&request);
26 //std::cout << "Received Hello" << std::endl;
27
28 // Do some 'work'
29 //Sleep(1);
30
31 // Send reply back to client
32 zmq::message_t reply (5);
33 memcpy ((void *) reply.data (), "World", 5);
34 socket.send (reply);
35 }
36 return 0;
37 }
posted on 2012-04-01 16:26
爬 阅读(4155)
评论(1) 编辑 收藏 引用 所属分类:
测试训练