|
1 | 1 | var http = require('http'); |
2 | 2 | var Agent = require('../'); |
| 3 | +// var Agent = require('http').Agent; |
3 | 4 |
|
4 | 5 | var keepaliveAgent = new Agent({ |
5 | 6 | keepAlive: true |
6 | 7 | }); |
7 | 8 | // https://www.google.com/search?q=nodejs&sugexp=chrome,mod=12&sourceid=chrome&ie=UTF-8 |
| 9 | + |
8 | 10 | var options = { |
9 | | - host: 'gitcafe.com', |
| 11 | + host: 'www.taobao.com', |
10 | 12 | path: '/', |
11 | 13 | method: 'GET', |
| 14 | + port: 80, |
12 | 15 | agent: keepaliveAgent |
13 | 16 | }; |
14 | 17 |
|
15 | | -var start = Date.now(); |
16 | | -var req = http.get(options, function (res) { |
17 | | - console.log('STATUS1: %d, %d ms', res.statusCode, Date.now() - start); |
18 | | - console.log('HEADERS1: %j', res.headers); |
19 | | - res.setEncoding('utf8'); |
20 | | - res.on('data', function (chunk) { |
21 | | - console.log('BODY1: %d', chunk.length); |
22 | | - }); |
23 | | - res.on('end', function () { |
24 | | - process.nextTick(function () { |
25 | | - start = Date.now(); |
26 | | - http.get(options, function (res) { |
27 | | - console.log('STATUS2: %d, %d ms', res.statusCode, Date.now() - start); |
28 | | - console.log('HEADERS2: %j', res.headers); |
29 | | - res.setEncoding('utf8'); |
30 | | - res.on('data', function (chunk) { |
31 | | - console.log('BODY2: %d', chunk.length); |
32 | | - }); |
33 | | - }); |
| 18 | +function get() { |
| 19 | + var start = Date.now(); |
| 20 | + var req = http.get(options, function (res) { |
| 21 | + console.log('STATUS1: %d, %d ms', res.statusCode, Date.now() - start); |
| 22 | + console.log('HEADERS1: %j', res.headers); |
| 23 | + res.on('data', function (chunk) { |
| 24 | + console.log('BODY1: %d', chunk.length); |
| 25 | + }); |
| 26 | + res.on('end', function () { |
| 27 | + console.log('get end'); |
34 | 28 | }); |
35 | 29 | }); |
36 | | -}); |
37 | | -req.on('error', function (e) { |
38 | | - console.log('problem with request: ' + e.message); |
39 | | -}); |
| 30 | + req.on('error', function (e) { |
| 31 | + console.log('problem with request: ' + e.message); |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +get(); |
40 | 36 |
|
41 | 37 | setTimeout(function () { |
42 | 38 | console.log('keep alive sockets:', keepaliveAgent); |
43 | 39 | process.exit(); |
44 | | -}, 3000); |
| 40 | +}, 300000); |
| 41 | + |
| 42 | +var count = 0; |
| 43 | +setInterval(function() { |
| 44 | + var name = keepaliveAgent.getName(options); |
| 45 | + var sockets = keepaliveAgent.sockets[name] || []; |
| 46 | + var freeSockets = keepaliveAgent.freeSockets[name] || []; |
| 47 | + console.log('%ss, %s, sockets: %d, destroyed: %s, free sockets: %d, destroyed: %s', ++count, |
| 48 | + name, sockets.length, sockets[0] && sockets[0].destroyed, |
| 49 | + freeSockets.length, freeSockets[0] && freeSockets[0].destroyed); |
| 50 | +}, 1000); |
| 51 | + |
| 52 | +setInterval(get, 120000); |
0 commit comments