Question

I need to compare the performances of a Flask application that uses jinja2.Template.render against jinja2.Template.stream to be sure that there is no loss of performances using the streaming of templates.

My (very simple) idea was to print the timestamp before and after the rendering of the template, but since the webpage is returned by a function, I'm not quite sure how to implement this...

My function is:

def index():
    """main function"""
    env = Environment(loader=FileSystemLoader(basedir+'templates'))
    #I modify the global variables
    env.globals['foo'] = 'bar'
    env.globals['build_response']=build_response
    get_url = request.args.get('to_print', None)
    message = "the input was \"%s\"" % (get_url,)
    template = env.get_template('body.html')
    return Response(template.stream(message=message))
    #return template.render(message=message)

And what I need to measure is the time spent by

return template.render(message=message)

vs

return Response(template.stream(message=message))

thanks!

Was it helpful?

Solution

You need to look at the system clock before and after the call.

It's easier to do this if you don't immediately return the response from template.render or template.stream.

IE:

from time import time

def index():
    # THE REST OF YOUR CODE GOES HERE
    start = time.time()
    resp = template.render(message=message)
    end = time.time()
    processing_time = end - start
    # LOG PROCESSING TIME SOMEPLACE
    return resp
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top