-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.js
More file actions
64 lines (56 loc) · 1.4 KB
/
test_client.js
File metadata and controls
64 lines (56 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var http = require('http');
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
function processHttpPost(host,port,path,data,callback){
var putOptions = {
host: host,
port: port,
path: path,
method: 'POST',
headers: {
'Content-Type' : 'application/json',
'Content-Length': data.length
}
};
var req = http.request(putOptions,function(response) {
var message = '';
response.on('data',function(chunk){
message += chunk;
});
response.on('end',function(){
return callback(message);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
console.log(data);
req.write(data);
req.end();
}
if (cluster.isMaster) {
var worker = null;
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
worker = cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
//重启worker进程
worker = cluster.fork();
});
} else {
var cnt = 0;
setInterval(function(){
++cnt;
var test_data = {
"index": "es_test",
"cnt": cnt,
"msg": "this is a test!!!"
}
processHttpPost('localhost',8082,'/',JSON.stringify(test_data),function(msg){
console.log(cnt);
});
}, 1*1000);
}