UNPKG

4.08 kBPlain TextView Raw
1// import * as bluebird from "bluebird"
2
3import {preload} from './preload';
4preload();
5
6
7// // 使用bluebird输出完整的promise调用链
8// global.Promise = bluebird.Promise;
9// // 开启长堆栈
10// bluebird.config({
11// // Enable warnings
12// warnings: false,
13// // Enable long stack traces
14// longStackTraces: false,
15// // Enable cancellation
16// cancellation: false,
17// // Enable monitoring
18// monitoring: false
19// });
20
21
22import { RpcClient, RpcMsg, RouteContext, RouteServers } from '../index';
23import { configure } from 'pinus-logger';
24configure('./config/log4js.json');
25
26// remote service interface path info list
27let records = [{
28 namespace: 'user',
29 serverType: 'test',
30 path: __dirname + '/remote/test'
31}];
32
33let context = {
34 serverId: 'test-server-1'
35};
36
37// server info list
38let servers =
39 [{
40 id: 'test-server-1',
41 serverType: 'test',
42 host: '127.0.0.1',
43 port: 3333
44 }];
45// route parameter passed to route function
46let routeParam: string = null;
47
48// route context passed to route function
49let routeContext = servers;
50
51
52// route function to caculate the remote server id
53let routeFunc = function (session: { [key: string]: any }, msg: RpcMsg, context: RouteServers, cb: (err: Error, serverId?: string) => void) {
54 cb(null, context[0].id);
55};
56
57let client = new RpcClient({
58 routeContext: servers,
59 router: routeFunc,
60 context: context,
61 pendingSize: 10000000000
62});
63
64let start: number = null;
65client.start(async function (err) {
66 console.log('rpc client start ok.');
67
68 client.addProxies(records);
69 client.addServers(servers);
70
71 start = Date.now();
72 // runSerial();
73 // runParallels();
74 runOnlySends();
75});
76
77let num_requests = 100000;
78let times = 0;
79let mock_data_1 = 'hello';
80let mock_data_2 = 'hello';
81
82let num_repeat = 200; // 100 200 300 400 800
83
84for (let i = 0; i < num_repeat; i++) {
85 mock_data_2 += mock_data_1;
86}
87let mock_data_3 = {
88 a: 'run',
89 b: mock_data_2 + Date.now() + '_',
90 time: Date.now()
91};
92
93let payload = mock_data_3;
94
95// console.log(new Buffer(payload).length / 1024 + 'k');
96console.log(new Buffer(JSON.stringify(payload)).length / 1024 + 'k');
97
98async function runParallels() {
99 let maxParallel = 1;
100 while (true) {
101 if (maxParallel > 10000) {
102 maxParallel = 10000;
103 }
104 let now = Date.now();
105 start = now;
106 await runParallel(maxParallel);
107
108 now = Date.now();
109 let cost = now - start;
110 console.log(`runParallel ${num_requests} num requests(maxParallel:${maxParallel}) cost ${cost}ms , ${(num_requests / (cost / 1000)).toFixed(2)}ops/sec`);
111
112 maxParallel = maxParallel * 2;
113 }
114}
115
116async function runParallel(maxParallel: number) {
117 let all = [];
118 for (let times = 0; times < num_requests; times++) {
119 all.push(rpcRequest(payload));
120 if (all.length === maxParallel) {
121 await Promise.all(all);
122 all.length = 0;
123 }
124 }
125 await Promise.all(all);
126}
127
128async function runSerial() {
129 if (times > num_requests) {
130 return;
131 }
132
133 if (times === num_requests) {
134 let now = Date.now();
135 let cost = now - start;
136 console.log(`runSerial ${num_requests} num requests cost ${cost}ms , ${(num_requests / (cost / 1000)).toFixed(2)}ops/sec`);
137 times = 0;
138 start = now;
139 // return;
140 await runSerial();
141 return;
142 }
143
144 times++;
145 await rpcRequest(payload);
146 runSerial();
147}
148
149async function rpcRequest(param: any) {
150 let result = await client.proxies.user.test.service.echo(routeParam, mock_data_1, 123);
151 // console.log(count++);
152}
153
154
155
156
157
158async function runOnlySends() {
159 let maxParallel = 1;
160 while (true) {
161 if (maxParallel > 10000) {
162 maxParallel = 10000;
163 }
164 let now = Date.now();
165 start = now;
166 runOnlySend(maxParallel);
167
168 now = Date.now();
169 let cost = now - start;
170 console.log(`runOnlySend ${num_requests} num requests(maxParallel:${maxParallel}) cost ${cost}ms , ${(num_requests / (cost / 1000)).toFixed(2)}ops/sec`);
171
172 maxParallel = maxParallel * 2;
173 }
174}
175
176function runOnlySend(maxParallel: number) {
177 for (let times = 0; times < num_requests; times++) {
178 rpcRequest(payload);
179 }
180}