squeaky-pl / japronto

Screaming-fast Python 3.5+ HTTP toolkit integrated with pipelining HTTP server based on uvloop and picohttpparser.
MIT License
8.61k stars 581 forks source link

Jinja2 template rendering causes a crash #48

Closed talaikis closed 7 years ago

talaikis commented 7 years ago

Japronto template rendering is 10x slower compared to Sanic:

screenshot from 2017-02-22 21-48-54

Japronto:

async def index(request):
    with open('templates/index.html') as template_file:
        template = Template(template_file.read())

        return request.Response(
            text=template.render(
                ping='pong'
                ),
            mime_type='text/html')

Sanic:

def template(tpl, **kwargs):
template = env.get_template(tpl)
return html(template.render(kwargs))

@app.route("/")
async def home(request):
    return template('index.html', ping='pong')
squeaky-pl commented 7 years ago

You misinterpretted results. Look at the last column, it's not slower but it's for some reason crashing workers.

imbolc commented 7 years ago

@xenu256 i think your error occurs because you don't close template files, try to move return outside of open context manager. And to be fair you should initialize jijna env once in the global scope with japrondo as you did with sanic.

talaikis commented 7 years ago

OK, had a bit of time to return to this. You're right, now results are comparable:

dxenu@dxenu-VirtualBox:~$ wrk -t200 -c400 -d240s http://0.0.0.0:8080 Running 4m test @ http://0.0.0.0:8080 200 threads and 400 connections Thread Stats Avg Stdev Max +/- Stdev Latency 193.01ms 81.52ms 845.32ms 77.26% Req/Sec 11.81 5.52 40.00 55.53% 503354 requests in 4.00m, 322.58MB read Requests/sec: 2097.13 Transfer/sec: 1.34MB

Better with removed prints:

Thread Stats Avg Stdev Max +/- Stdev Latency 32.31ms 12.11ms 501.18ms 94.66% Req/Sec 63.37 12.07 215.00 66.06% 3022081 requests in 4.00m, 1.89GB read Requests/sec: 12588.35 Transfer/sec: 8.07MB