烧瓶 + Jinja2:如何衡量性能

发布于 2024-12-18 12:38:27 字数 888 浏览 0 评论 0原文

我需要将使用 jinja2.Template.render 的 Flask 应用程序的性能与 jinja2.Template.stream 进行比较,以确保使用流式处理不会造成性能损失模板。

我的(非常简单)的想法是打印模板渲染之前和之后的时间戳,但是由于网页是由函数返回的,我不太确定如何实现这个......

是:

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)

我的功能 所花费的时间,

return template.render(message=message)

需要测量的是vs

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

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彼岸花ソ最美的依靠 2024-12-25 12:38:27

您需要在通话之前和之后查看系统时钟。

如果您不立即从 template.render 或 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文