UNPKG

2.01 kBJavaScriptView Raw
1var Client = require('..').client;
2
3// remote service interface path info list
4var records = [{
5 namespace: 'user',
6 serverType: 'test',
7 path: __dirname + '/remote/test'
8}];
9
10var context = {
11 serverId: 'test-server-1'
12};
13
14// server info list
15var servers = [{
16 id: 'test-server-1',
17 serverType: 'test',
18 host: '127.0.0.1',
19 port: 3333
20}];
21
22// route parameter passed to route function
23var routeParam = null;
24
25// route context passed to route function
26var routeContext = servers;
27
28// route function to caculate the remote server id
29var routeFunc = function(routeParam, msg, routeContext, cb) {
30 cb(null, routeContext[0].id);
31};
32
33var client = Client.create({
34 routeContext: routeContext,
35 router: routeFunc,
36 context: context
37});
38
39var start = null;
40client.start(function(err) {
41 console.log('rpc client start ok.');
42
43 client.addProxies(records);
44 client.addServers(servers);
45
46 start = Date.now();
47 run();
48});
49
50var num_requests = 100000;
51var times = 0;
52var mock_data_1 = 'hello';
53var mock_data_2 = 'hello';
54
55var num_repeat = 200; // 100 200 300 400 800
56
57for (var i = 0; i < num_repeat; i++) {
58 mock_data_2 += mock_data_1;
59}
60
61var mock_data_3 = {
62 a: 'run',
63 b: mock_data_2 + Date.now() + '_',
64 time: Date.now()
65}
66
67var payload = mock_data_3;
68
69// console.log(new Buffer(payload).length / 1024 + 'k');
70console.log(new Buffer(JSON.stringify(payload)).length / 1024 + 'k');
71
72function run() {
73 if (times > num_requests) {
74 return;
75 }
76
77 if (times == num_requests) {
78 var now = Date.now();
79 var cost = now - start;
80 console.log('run %d num requests cost: %d ops/sec', num_requests, cost, (num_requests / (cost / 1000)).toFixed(2));
81 times = 0;
82 start = now;
83 // return;
84 return run();
85 }
86
87 times++;
88 rpcRequest(payload, function() {
89 run();
90 });
91}
92
93function rpcRequest(param, cb) {
94 client.proxies.user.test.service.echo(routeParam, param, 123, function(err, resp) {
95 if (err) {
96 console.error(err.stack);
97 }
98 // console.log(resp);
99 cb();
100 });
101}
\No newline at end of file