MarkReedZ / mrhttp

The fastest python web framework - 8.5m requests per second!
MIT License
24 stars 6 forks source link

Raise 404 is slow #8

Open MarkReedZ opened 6 years ago

MarkReedZ commented 6 years ago

Raising a 404 is a lot slower than a natural 404. I got this idea from sanic so suggest they change theirs as well.

@app.route('/404/')
def notFound(r):
  raise mrhttp.HTTPError(404)
  return 'Hello World!'

I suggest app.err404 be the 404 page so it can just be returned. Performance would be equal to the hello world benchmark. The user can modify this page as well.

  return app.err404
404             326789.47 Requests/second
404 natural     466788.18 Requests/second

  print ("404            ", run_wrk(loop, 'http://localhost:8080/404/'), "Requests/second" )
  print ("404 natural    ", run_wrk(loop, 'http://localhost:8080/dfads404/'), "Requests/second" )
MarkReedZ commented 5 years ago

app.err404 was added so that a custom 404 can be displayed. However just directly returning it would return a 404 message with a 200 http response code. So to 404 I'm looking at these options. I can speed up the raise a bit, but it is still significantly slower than the second option as if you raise then the exception information has to be fetched before returning the 404.

  raise mrhttp.NotFound
  return r.NotFound()
MarkReedZ commented 5 years ago

Also the custom error pages could be specified by app.err[404] to allow for other codes. Perhaps the custom error page example should also show how to load an html file in case someone wants to do that.