-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.js
executable file
·161 lines (145 loc) · 4.55 KB
/
tests.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/local/bin/node
var response = [];
response.push("HTTP/1.1 200 OK");
response.push("date: Mon, 08 Nov 2010 16:53:22 GMT");
response.push("server: Server");
response.push("set-cookie: skin=noskin; path=/; domain=.amazon.co.uk; expires=Mon, 08-Nov-2010 16:53:22 GMT");
response.push("x-amz-id-1: AAAAA");
response.push("x-amz-id-2: BBBBB=");
response.push("vary: Accept-Encoding,User-Agent");
response.push("connection: Keep-Alive");
response.push("Content-Length: 10");
response.push("content-type: text/html; charset=ISO-8859-1");
response.push("set-cookie: session-id-time=2082758401l; path=/; domain=.amazon.co.uk; expires=Tue Jan 01 00:00:01 2036 GMT");
response.push("set-cookie: session-id=277-1111279-4168403; path=/; domain=.amazon.co.uk; expires=Tue Jan 01 00:00:01 2036 GMT");
var display = false;
var chunksize = parseInt(process.ARGV[2]);
var messages = parseInt(process.ARGV[3]);
var version = "0.0.0";
if(process.ARGV.length > 4) {
display = (process.ARGV[4] === "true");
}
if(process.ARGV.length > 5) {
version = process.ARGV[5];
}
function testcpp(buff, type) {
var rr = "";
var HTTPParser = require("./lib/parser").HTTPParser;
var parsed = 0;
var i = chunksize;
while(i--) {
rr += buff.join("\r\n") + "\r\n\r\n0123456789";
}
var payload = new Buffer(rr);
var tlen = payload.length;
var parser = new HTTPParser(type);
parser.onMessageComplete = function (info, headers) {
parsed++;
if(display) console.log(JSON.stringify({
info: info,
headers: headers
}));
if(!info.shouldKeepAlive) parser.reinitialize(type);
};
parser.bind();
var then = new Date().getTime();
process.stderr.write("start: " + then + "\n");
while(parsed < messages) {
for(var i=0; i<tlen; i+=chunksize) {
parser.execute(payload, i, chunksize);
}
}
var now = new Date().getTime();
process.stderr.write("end: " + now + "\n");
var elapsed = (now-then)/1000;
process.stdout.write(version + "\tc++\t" + chunksize + "\t" + parsed + "\t" + elapsed.toFixed(2) + "\t" + (parsed/elapsed).toFixed(2) + "\n");
payload = null;
parser = null;
}
function testnode(buff, type) {
var rr = "";
var HTTPParser = process.binding("http_parser").HTTPParser;
var parsed = 0;
var i = chunksize;
while(i--) {
rr += buff.join("\r\n") + "\r\n\r\n0123456789";
}
var payload = new Buffer(rr);
var tlen = payload.length;
var parser = new HTTPParser(type);
parser.onMessageBegin = function () {
parser.incoming = {"headers": {}};
parser.field = null;
parser.value = null;
};
parser.onHeaderField = function (b, start, len) {
var slice = b.toString('ascii', start, start+len).toLowerCase();
if (parser.value != undefined) {
var dest = parser.incoming.headers;
if (parser.field in dest) {
dest[parser.field].push(parser.value);
} else {
dest[parser.field] = [parser.value];
}
parser.field = null;
parser.value = null;
}
if (parser.field) {
parser.field += slice;
} else {
parser.field = slice;
}
};
parser.onHeaderValue = function (b, start, len) {
var slice = b.toString('ascii', start, start+len);
if (parser.value) {
parser.value += slice;
} else {
parser.value = slice;
}
};
parser.onHeadersComplete = function (info) {
if (parser.field && (parser.value != undefined)) {
var dest = parser.incoming.headers;
if (parser.field in dest) {
dest[parser.field].push(parser.value);
} else {
dest[parser.field] = [parser.value];
}
parser.field = null;
parser.value = null;
}
parser.incoming.info = info;
return false;
};
parser.onMessageComplete = function () {
if (parser.field && (parser.value != undefined)) {
var dest = parser.incoming.headers;
if (parser.field in dest) {
dest[parser.field].push(parser.value);
} else {
dest[parser.field] = [parser.value];
}
}
parsed++;
if(display) console.log(JSON.stringify(parser.incoming));
// TODO - remove the need to do this and reinitialize to same type automatically in the parser
if(!parser.incoming.info.shouldKeepAlive) parser.reinitialize(type);
parser.incoming = null;
};
var then = new Date().getTime();
process.stderr.write("start: " + then + "\n");
while(parsed < messages) {
for(var i=0; i<tlen; i+=chunksize) {
parser.execute(payload, i, chunksize);
}
}
var now = new Date().getTime();
process.stderr.write("end: " + now + "\n");
var elapsed = (now-then)/1000;
process.stdout.write(version + "\tnode\t" + chunksize + "\t" + parsed + "\t" + elapsed.toFixed(2) + "\t" + (parsed/elapsed).toFixed(2) + "\n");
payload = null;
parser = null;
}
testnode(response, "response");
testcpp(response, "response");