Flask + Jinja2: how to measure performances

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!

Answers


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

Need Your Help

C++11 Threads: Exception when called using lambdas

c++ multithreading c++11

I was reading about threads in C++11 and I did

Requiring SSL certificates and client certificates throws exception in WCF JSON service

.net iis-6 ssl-certificate wcf-security wcf-rest

I have a simple WCF service setup that uses JSON. In this service I want to use client authentication with a client certificate. I've configured IIS 6 to require SSL and to require client certifica...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.