-
Notifications
You must be signed in to change notification settings - Fork 1
/
request-speed2.js
103 lines (75 loc) · 2.18 KB
/
request-speed2.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var headers, options, request, rqstURI, startTime, t,
// functions
logConsole, processDom, processHTML,
// modules
eyes, http;
logConsole = function (text) {
var t = Date.now() - startTime;
console.log('[' + t + '] ' + text);
};
startTime = Date.now();
logConsole("starting... loading modules...");
http = require('http');
eyes = require('eyes');
logConsole("modules loaded");
rqstURI = '/docs/v0.4.3/api/all.html';
options = {
host: 'nodejs.org',
method: 'GET',
port: 80,
path: rqstURI,
'Accept': 'text/html;q=0.9,text/plain;q=0.8;',
'Cache-Control': 'max-age=0',
'User-Agent': 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-us) IDX Engine using NodeJS/0.5.pre (like Safari/533.20.27)'
};
logConsole("building request...");
request = http.request(options, function(response) {
var chunks, data, size, status;
chunks = 0;
data = '';
size = 0;
status = response.statusCode;
logConsole('STATUS: ' + status);
response.setEncoding('utf8');
response.on('error', function (e) {
var errno = e.errno;
if (errno === process.ECONNREFUSED) {
sys.log('ECONNREFUSED: connection refused to ' + request.host + ':' + request.port);
} else if (errno === process.EAGAIN) {
sys.log('EAGAIN: No DNS for ' + request.host);
} else {
console.log('!error encountered: ' + e);
}
});
response.on('data', function (chunk) {
var a;
chunks += 1;
size += chunk.length;
// reduce whitespace
data += chunk.split(/\s+/g).join(' ');
});
response.on('end', function(){
if (status === 200) {
if (chunks) {
logConsole('Data Complete in ' + chunks + ' chunks comprising ' + size + ' byte');
} else {
logConsole('Aborting... no chunks received');
}
} else {
logConsole('Aborting... Headers:');
eyes.inspect(response.headers);
}
});
});
request.on('error', function (e) {
var errno = e.errno;
if (errno === process.ECONNREFUSED) {
sys.log('ECONNREFUSED: connection refused to ' + request.host + ':' + request.port);
} else if (errno === process.EAGAIN) {
sys.log('EAGAIN: No DNS for ' + request.host);
} else {
console.log('!error encountered: ' + e);
}
});
logConsole("issuing request...");
request.end();