diff options
author | Bert Belder <bertbelder@gmail.com> | 2015-01-13 02:32:09 +0100 |
---|---|---|
committer | Bert Belder <bertbelder@gmail.com> | 2015-01-13 02:35:27 +0100 |
commit | 0bf1d124af481ac9be095fcb846a1aad08595233 (patch) | |
tree | a86f47d152523eda52a2e7ecb9fb564410386190 /test | |
parent | f468e5f1ec34d51f1691ab1f8c9cd813ebee85ef (diff) | |
download | node-new-0bf1d124af481ac9be095fcb846a1aad08595233.tar.gz |
http: optimize on_headers_complete
Use an array instead of an object to pass a parsed header chunk from c++
to javascript. This offers a 5-10% speedup on the http_simple benchmark,
as evidenced by running:
ab -k -t 100 -c 100 http://127.0.0.1:8000/bytes/100
PR: https://github.com/iojs/io.js/pull/292
Reviewed-by: Ben Noordhuis <info@bnoordhuis.nl>
Diffstat (limited to 'test')
-rw-r--r-- | test/parallel/test-http-parser.js | 294 |
1 files changed, 161 insertions, 133 deletions
diff --git a/test/parallel/test-http-parser.js b/test/parallel/test-http-parser.js index e860e91a0b..d8c181bf60 100644 --- a/test/parallel/test-http-parser.js +++ b/test/parallel/test-http-parser.js @@ -76,15 +76,17 @@ function expectBody(expected) { 'GET /hello HTTP/1.1' + CRLF + CRLF); - var parser = newParser(REQUEST); - - parser[kOnHeadersComplete] = mustCall(function(info) { - assert.equal(info.method, methods.indexOf('GET')); - assert.equal(info.url || parser.url, '/hello'); - assert.equal(info.versionMajor, 1); - assert.equal(info.versionMinor, 1); - }); + var onHeadersComplete = function(versionMajor, versionMinor, headers, method, + url, statusCode, statusMessage, upgrade, + shouldKeepAlive) { + assert.equal(versionMajor, 1); + assert.equal(versionMinor, 1); + assert.equal(method, methods.indexOf('GET')); + assert.equal(url || parser.url, '/hello'); + }; + var parser = newParser(REQUEST); + parser[kOnHeadersComplete] = mustCall(onHeadersComplete); parser.execute(request, 0, request.length); // @@ -115,21 +117,24 @@ function expectBody(expected) { CRLF + 'pong'); - var parser = newParser(RESPONSE); - - parser[kOnHeadersComplete] = mustCall(function(info) { - assert.equal(info.method, undefined); - assert.equal(info.versionMajor, 1); - assert.equal(info.versionMinor, 1); - assert.equal(info.statusCode, 200); - assert.equal(info.statusMessage, "OK"); - }); + var onHeadersComplete = function(versionMajor, versionMinor, headers, method, + url, statusCode, statusMessage, upgrade, + shouldKeepAlive) { + assert.equal(method, undefined); + assert.equal(versionMajor, 1); + assert.equal(versionMinor, 1); + assert.equal(statusCode, 200); + assert.equal(statusMessage, 'OK'); + }; - parser[kOnBody] = mustCall(function(buf, start, len) { + var onBody = function(buf, start, len) { var body = '' + buf.slice(start, start + len); assert.equal(body, 'pong'); - }); + }; + var parser = newParser(RESPONSE); + parser[kOnHeadersComplete] = mustCall(onHeadersComplete); + parser[kOnBody] = mustCall(onBody); parser.execute(request, 0, request.length); })(); @@ -142,17 +147,19 @@ function expectBody(expected) { 'HTTP/1.0 200 Connection established' + CRLF + CRLF); - var parser = newParser(RESPONSE); - - parser[kOnHeadersComplete] = mustCall(function(info) { - assert.equal(info.method, undefined); - assert.equal(info.versionMajor, 1); - assert.equal(info.versionMinor, 0); - assert.equal(info.statusCode, 200); - assert.equal(info.statusMessage, "Connection established"); - assert.deepEqual(info.headers || parser.headers, []); - }); + var onHeadersComplete = function(versionMajor, versionMinor, headers, method, + url, statusCode, statusMessage, upgrade, + shouldKeepAlive) { + assert.equal(versionMajor, 1); + assert.equal(versionMinor, 0); + assert.equal(method, undefined); + assert.equal(statusCode, 200); + assert.equal(statusMessage, 'Connection established'); + assert.deepEqual(headers || parser.headers, []); + }; + var parser = newParser(RESPONSE); + parser[kOnHeadersComplete] = mustCall(onHeadersComplete); parser.execute(request, 0, request.length); })(); @@ -174,29 +181,31 @@ function expectBody(expected) { var seen_body = false; - function onHeaders(headers, url) { + var onHeaders = function(headers, url) { assert.ok(seen_body); // trailers should come after the body - assert.deepEqual(headers, - ['Vary', '*', 'Content-Type', 'text/plain']); - } - - var parser = newParser(REQUEST); + assert.deepEqual(headers, ['Vary', '*', 'Content-Type', 'text/plain']); + }; - parser[kOnHeadersComplete] = mustCall(function(info) { - assert.equal(info.method, methods.indexOf('POST')); - assert.equal(info.url || parser.url, '/it'); - assert.equal(info.versionMajor, 1); - assert.equal(info.versionMinor, 1); + var onHeadersComplete = function(versionMajor, versionMinor, headers, method, + url, statusCode, statusMessage, upgrade, + shouldKeepAlive) { + assert.equal(method, methods.indexOf('POST')); + assert.equal(url || parser.url, '/it'); + assert.equal(versionMajor, 1); + assert.equal(versionMinor, 1); // expect to see trailing headers now parser[kOnHeaders] = mustCall(onHeaders); - }); + }; - parser[kOnBody] = mustCall(function(buf, start, len) { + var onBody = function(buf, start, len) { var body = '' + buf.slice(start, start + len); assert.equal(body, 'ping'); seen_body = true; - }); + }; + var parser = newParser(REQUEST); + parser[kOnHeadersComplete] = mustCall(onHeadersComplete); + parser[kOnBody] = mustCall(onBody); parser.execute(request, 0, request.length); })(); @@ -212,18 +221,19 @@ function expectBody(expected) { 'X-Filler2: 42' + CRLF + CRLF); - var parser = newParser(REQUEST); - - parser[kOnHeadersComplete] = mustCall(function(info) { - assert.equal(info.method, methods.indexOf('GET')); - assert.equal(info.versionMajor, 1); - assert.equal(info.versionMinor, 0); - assert.deepEqual(info.headers || parser.headers, - ['X-Filler', '1337', - 'X-Filler', '42', - 'X-Filler2', '42']); - }); + var onHeadersComplete = function(versionMajor, versionMinor, headers, method, + url, statusCode, statusMessage, upgrade, + shouldKeepAlive) { + assert.equal(method, methods.indexOf('GET')); + assert.equal(versionMajor, 1); + assert.equal(versionMinor, 0); + assert.deepEqual( + headers || parser.headers, + ['X-Filler', '1337', 'X-Filler', '42', 'X-Filler2', '42']); + }; + var parser = newParser(REQUEST); + parser[kOnHeadersComplete] = mustCall(onHeadersComplete); parser.execute(request, 0, request.length); })(); @@ -241,23 +251,25 @@ function expectBody(expected) { lots_of_headers + CRLF); - var parser = newParser(REQUEST); - - parser[kOnHeadersComplete] = mustCall(function(info) { - assert.equal(info.method, methods.indexOf('GET')); - assert.equal(info.url || parser.url, '/foo/bar/baz?quux=42#1337'); - assert.equal(info.versionMajor, 1); - assert.equal(info.versionMinor, 0); + var onHeadersComplete = function(versionMajor, versionMinor, headers, method, + url, statusCode, statusMessage, upgrade, + shouldKeepAlive) { + assert.equal(method, methods.indexOf('GET')); + assert.equal(url || parser.url, '/foo/bar/baz?quux=42#1337'); + assert.equal(versionMajor, 1); + assert.equal(versionMinor, 0); - var headers = info.headers || parser.headers; + var headers = headers || parser.headers; assert.equal(headers.length, 2 * 256); // 256 key/value pairs for (var i = 0; i < headers.length; i += 2) { assert.equal(headers[i], 'X-Filler'); assert.equal(headers[i + 1], '42'); } - }); + }; + var parser = newParser(REQUEST); + parser[kOnHeadersComplete] = mustCall(onHeadersComplete); parser.execute(request, 0, request.length); })(); @@ -273,20 +285,23 @@ function expectBody(expected) { CRLF + 'foo=42&bar=1337'); - var parser = newParser(REQUEST); - - parser[kOnHeadersComplete] = mustCall(function(info) { - assert.equal(info.method, methods.indexOf('POST')); - assert.equal(info.url || parser.url, '/it'); - assert.equal(info.versionMajor, 1); - assert.equal(info.versionMinor, 1); - }); + var onHeadersComplete = function(versionMajor, versionMinor, headers, method, + url, statusCode, statusMessage, upgrade, + shouldKeepAlive) { + assert.equal(method, methods.indexOf('POST')); + assert.equal(url || parser.url, '/it'); + assert.equal(versionMajor, 1); + assert.equal(versionMinor, 1); + }; - parser[kOnBody] = mustCall(function(buf, start, len) { + var onBody = function(buf, start, len) { var body = '' + buf.slice(start, start + len); assert.equal(body, 'foo=42&bar=1337'); - }); + }; + var parser = newParser(REQUEST); + parser[kOnHeadersComplete] = mustCall(onHeadersComplete); + parser[kOnBody] = mustCall(onBody); parser.execute(request, 0, request.length); })(); @@ -308,23 +323,25 @@ function expectBody(expected) { '1234567890' + CRLF + '0' + CRLF); - var parser = newParser(REQUEST); - - parser[kOnHeadersComplete] = mustCall(function(info) { - assert.equal(info.method, methods.indexOf('POST')); - assert.equal(info.url || parser.url, '/it'); - assert.equal(info.versionMajor, 1); - assert.equal(info.versionMinor, 1); - }); + var onHeadersComplete = function(versionMajor, versionMinor, headers, method, + url, statusCode, statusMessage, upgrade, + shouldKeepAlive) { + assert.equal(method, methods.indexOf('POST')); + assert.equal(url || parser.url, '/it'); + assert.equal(versionMajor, 1); + assert.equal(versionMinor, 1); + }; var body_part = 0, body_parts = ['123', '123456', '1234567890']; - function onBody(buf, start, len) { + var onBody = function(buf, start, len) { var body = '' + buf.slice(start, start + len); assert.equal(body, body_parts[body_part++]); - } + }; + var parser = newParser(REQUEST); + parser[kOnHeadersComplete] = mustCall(onHeadersComplete); parser[kOnBody] = mustCall(onBody, body_parts.length); parser.execute(request, 0, request.length); })(); @@ -344,25 +361,26 @@ function expectBody(expected) { '6' + CRLF + '123456' + CRLF); - var parser = newParser(REQUEST); - - parser[kOnHeadersComplete] = mustCall(function(info) { - assert.equal(info.method, methods.indexOf('POST')); - assert.equal(info.url || parser.url, '/it'); - assert.equal(info.versionMajor, 1); - assert.equal(info.versionMinor, 1); - }); + var onHeadersComplete = function(versionMajor, versionMinor, headers, method, + url, statusCode, statusMessage, upgrade, + shouldKeepAlive) { + assert.equal(method, methods.indexOf('POST')); + assert.equal(url || parser.url, '/it'); + assert.equal(versionMajor, 1); + assert.equal(versionMinor, 1); + }; var body_part = 0, - body_parts = [ - '123', '123456', '123456789', - '123456789ABC', '123456789ABCDEF']; + body_parts = + ['123', '123456', '123456789', '123456789ABC', '123456789ABCDEF']; - function onBody(buf, start, len) { + var onBody = function(buf, start, len) { var body = '' + buf.slice(start, start + len); assert.equal(body, body_parts[body_part++]); - } + }; + var parser = newParser(REQUEST); + parser[kOnHeadersComplete] = mustCall(onHeadersComplete); parser[kOnBody] = mustCall(onBody, body_parts.length); parser.execute(request, 0, request.length); @@ -401,23 +419,26 @@ function expectBody(expected) { '0' + CRLF); function test(a, b) { - var parser = newParser(REQUEST); - - parser[kOnHeadersComplete] = mustCall(function(info) { - assert.equal(info.method, methods.indexOf('POST')); - assert.equal(info.url || parser.url, '/helpme'); - assert.equal(info.versionMajor, 1); - assert.equal(info.versionMinor, 1); - }); + var onHeadersComplete = function(versionMajor, versionMinor, headers, + method, url, statusCode, statusMessage, + upgrade, shouldKeepAlive) { + assert.equal(method, methods.indexOf('POST')); + assert.equal(url || parser.url, '/helpme'); + assert.equal(versionMajor, 1); + assert.equal(versionMinor, 1); + }; var expected_body = '123123456123456789123456789ABC123456789ABCDEF'; - parser[kOnBody] = function(buf, start, len) { + var onBody = function(buf, start, len) { var chunk = '' + buf.slice(start, start + len); assert.equal(expected_body.indexOf(chunk), 0); expected_body = expected_body.slice(chunk.length); }; + var parser = newParser(REQUEST); + parser[kOnHeadersComplete] = mustCall(onHeadersComplete); + parser[kOnBody] = onBody; parser.execute(a, 0, a.length); parser.execute(b, 0, b.length); @@ -457,26 +478,30 @@ function expectBody(expected) { '123456789ABCDEF' + CRLF + '0' + CRLF); - var parser = newParser(REQUEST); - - parser[kOnHeadersComplete] = mustCall(function(info) { - assert.equal(info.method, methods.indexOf('POST')); - assert.equal(info.url || parser.url, '/it'); - assert.equal(info.versionMajor, 1); - assert.equal(info.versionMinor, 1); - assert.deepEqual(info.headers || parser.headers, - ['Content-Type', 'text/plain', - 'Transfer-Encoding', 'chunked']); - }); + var onHeadersComplete = function(versionMajor, versionMinor, headers, method, + url, statusCode, statusMessage, upgrade, + shouldKeepAlive) { + assert.equal(method, methods.indexOf('POST')); + assert.equal(url || parser.url, '/it'); + assert.equal(versionMajor, 1); + assert.equal(versionMinor, 1); + assert.deepEqual( + headers || parser.headers, + ['Content-Type', 'text/plain', 'Transfer-Encoding', 'chunked']); + }; var expected_body = '123123456123456789123456789ABC123456789ABCDEF'; - parser[kOnBody] = function(buf, start, len) { + var onBody = function(buf, start, len) { var chunk = '' + buf.slice(start, start + len); assert.equal(expected_body.indexOf(chunk), 0); expected_body = expected_body.slice(chunk.length); }; + var parser = newParser(REQUEST); + parser[kOnHeadersComplete] = mustCall(onHeadersComplete); + parser[kOnBody] = onBody; + for (var i = 0; i < request.length; ++i) { parser.execute(request, i, 1); } @@ -505,24 +530,27 @@ function expectBody(expected) { CRLF + 'pong'); - function onHeadersComplete1(info) { - assert.equal(info.method, methods.indexOf('PUT')); - assert.equal(info.url, '/this'); - assert.equal(info.versionMajor, 1); - assert.equal(info.versionMinor, 1); - assert.deepEqual(info.headers, - ['Content-Type', 'text/plain', - 'Transfer-Encoding', 'chunked']); + var onHeadersComplete1 = function(versionMajor, versionMinor, headers, method, + url, statusCode, statusMessage, upgrade, + shouldKeepAlive) { + assert.equal(method, methods.indexOf('PUT')); + assert.equal(url, '/this'); + assert.equal(versionMajor, 1); + assert.equal(versionMinor, 1); + assert.deepEqual( + headers, + ['Content-Type', 'text/plain', 'Transfer-Encoding', 'chunked']); }; - function onHeadersComplete2(info) { - assert.equal(info.method, methods.indexOf('POST')); - assert.equal(info.url, '/that'); - assert.equal(info.versionMajor, 1); - assert.equal(info.versionMinor, 0); - assert.deepEqual(info.headers, - ['Content-Type', 'text/plain', - 'Content-Length', '4']); + var onHeadersComplete2 = function(versionMajor, versionMinor, headers, method, + url, statusCode, statusMessage, upgrade, + shouldKeepAlive) { + assert.equal(method, methods.indexOf('POST')); + assert.equal(url, '/that'); + assert.equal(versionMajor, 1); + assert.equal(versionMinor, 0); + assert.deepEqual(headers, + ['Content-Type', 'text/plain', 'Content-Length', '4']); }; var parser = newParser(REQUEST); |