mafintosh / turbo-http

Blazing fast low level http server
MIT License
1k stars 47 forks source link

Error and bad performance if response is chunked. #23

Open c7x43t opened 5 years ago

c7x43t commented 5 years ago

OS: Linux 4.15.0-34-generic #37-Ubuntu SMP x86_64 GNU/Linux

Running the example below gives the expected high performance of 65k rps:

const hello = Buffer.from('hello world\n')
turbo.createServer(function (req, res) {
  res.setHeader('Content-Length', hello.length);
  res.write(hello);
}).listen(8081);

Running an example where content length is not set, like having transform stream middleware, like compression throws an error:

const hello = Buffer.from('hello world\n')
turbo.createServer(function (req, res) {
  res.write(hello);
  res.end();
}).listen(8081);

Error output:

TypeError: Cannot read property '_allocSmall' of undefined
    at Response._writeHeaderChunked (/home/c7x43t/Desktop/turboparallel/node_modules/turbo-http/lib/response.js:102:37)
    at Response._write (/home/c7x43t/Desktop/turboparallel/node_modules/turbo-http/lib/response.js:136:17)
    at Response.write (/home/c7x43t/Desktop/turboparallel/node_modules/turbo-http/lib/response.js:115:15)
    at Server.<anonymous> (/home/c7x43t/Desktop/turboparallel/node_modules/turbo-http/example.js:8:7)
    at emitTwo (events.js:126:13)
    at Server.emit (events.js:214:7)
    at HTTPParser.onhead (/home/c7x43t/Desktop/turboparallel/node_modules/turbo-http/lib/server.js:37:12)
    at HTTPParser.HEADER (/home/c7x43t/Desktop/turboparallel/node_modules/http-parser-js/http-parser.js:335:58)
    at HTTPParser.execute (/home/c7x43t/Desktop/turboparallel/node_modules/http-parser-js/http-parser.js:111:27)
    at onread (/home/c7x43t/Desktop/turboparallel/node_modules/turbo-http/lib/server.js:54:14)

This is propably a bug but can be fixed:

const hello = Buffer.from('hello world\n')
turbo.createServer(function (req, res) {
  res.server=this;
  res.write(hello);
  res.end();
}).listen(8081);

This executes correctly, but with chunked transfer encoding I only have 2.2k rps close to 30 x performance drop. Is this behaviour correct?

Edit: I also made a performance profile with node --prof: https://gist.github.com/c7x43t/1c1b27933f03377a19a186f69a01d8ed