librelist archives

« back to archive

How can I simple get page generation time?

How can I simple get page generation time?

From:
Tark
Date:
2013-01-12 @ 22:40
Hello, {{ user.name }}!

How can I simple and more honestly get page generation time in Flask and 
show this info on the page?
OK, @app.before_request helps, but it is not a start time of processing, 
isn’t it? 
And what about the end of processing? 
And how can I show this info? I mean, I need to change some strings in response.

In PHP it is very simple: $t = microtime(); in the begin ... echo 
(microtime() – $t); in the end. It is true info.

What about Flask?

Thank you! :)

Re: [flask] How can I simple get page generation time?

From:
Steven Kryskalla
Date:
2013-01-13 @ 00:24
On Sat, Jan 12, 2013 at 2:40 PM, Tark <tark.hidden@gmail.com> wrote:
> How can I simple and more honestly get page generation time in Flask and
> show this info on the page?
> OK, @app.before_request helps, but it is not a start time of processing,
> isn’t it?
> And what about the end of processing?
> And how can I show this info? I mean, I need to change some strings in
> response.
>
> In PHP it is very simple: $t = microtime(); in the begin ... echo
> (microtime() – $t); in the end. It is true info.

It depends what you want to measure (or how accurate you want to be)
and where you want to display it.

before_request is a good place to record the start time before your
view runs, and after_request is a good place to record the end time
(including template rendering), but since after_request runs after
render_template, you can't display the time in the template, you'd
have to log it instead.

This is a very basic way to display the time in the template:

https://gist.github.com/4521102

It's similar to Markus's approach, but you don't need to put any code
into your view functions.

There are a few caveats:
* it doesn't record some of the time flask/werkzeug spends internally
* it doesn't include time spent in after_request
* it measures the time from before_request up to the *time the
function was called in the template*, there could be more template
code that runs after it was called

This is probably good enough if all you want is a rough number.

To get a really accurate and detailed per request time you can use a
profiler, e.g. cProfile in the stdlib, or use a WSGI profiler like
these:

http://werkzeug.pocoo.org/docs/contrib/profiler/
http://pypi.python.org/pypi/linesman/0.2.2

But note that running a profiler will slow your app down, so use it
only during development or to debug performance problems.

Re: [flask] How can I simple get page generation time?

From:
Owein Reese
Date:
2013-01-13 @ 00:35
There's also the flask debugtoolbar extension which is wonderful for many
things and very informative. It requires near zero effort to install and
run. I use it but don't recommend for production stats.
On Jan 12, 2013 7:29 PM, "Steven Kryskalla" <skryskalla@gmail.com> wrote:

> On Sat, Jan 12, 2013 at 2:40 PM, Tark <tark.hidden@gmail.com> wrote:
> > How can I simple and more honestly get page generation time in Flask and
> > show this info on the page?
> > OK, @app.before_request helps, but it is not a start time of processing,
> > isn’t it?
> > And what about the end of processing?
> > And how can I show this info? I mean, I need to change some strings in
> > response.
> >
> > In PHP it is very simple: $t = microtime(); in the begin ... echo
> > (microtime() – $t); in the end. It is true info.
>
> It depends what you want to measure (or how accurate you want to be)
> and where you want to display it.
>
> before_request is a good place to record the start time before your
> view runs, and after_request is a good place to record the end time
> (including template rendering), but since after_request runs after
> render_template, you can't display the time in the template, you'd
> have to log it instead.
>
> This is a very basic way to display the time in the template:
>
> https://gist.github.com/4521102
>
> It's similar to Markus's approach, but you don't need to put any code
> into your view functions.
>
> There are a few caveats:
> * it doesn't record some of the time flask/werkzeug spends internally
> * it doesn't include time spent in after_request
> * it measures the time from before_request up to the *time the
> function was called in the template*, there could be more template
> code that runs after it was called
>
> This is probably good enough if all you want is a rough number.
>
> To get a really accurate and detailed per request time you can use a
> profiler, e.g. cProfile in the stdlib, or use a WSGI profiler like
> these:
>
> http://werkzeug.pocoo.org/docs/contrib/profiler/
> http://pypi.python.org/pypi/linesman/0.2.2
>
> But note that running a profiler will slow your app down, so use it
> only during development or to debug performance problems.
>

Re: [flask] How can I simple get page generation time?

From:
Markus Unterwaditzer
Date:
2013-01-12 @ 22:52
It is kind of similar to PHP.

The thing is that you don't return from render_template immediately 
like this:

@app.route('/')
def index():
     return render_template('index.htm')

but rather like:

import time

@app.route('/')
def index():
     start = time.time()
     html = render_template('index.htm')
     processing_time = time.time() - start
     # do something with the processing_time
     return html

You get the idea. The downside of this is that you can't include the 
time in your template, but you can still log it or send it in a header. 
Or you just don't time the template rendering process, but just the 
stuff in the view function.

On 2013-01-12 23:40, Tark wrote:
> Hello, {{ user.name }}!
>
> How can I simple and more honestly get page generation time in Flask
> and show this info on the page?
> OK, @app.before_request helps, but it is not a start time of
> processing, isn’t it?
> And what about the end of processing?
> And how can I show this info? I mean, I need to change some strings
> in response.
>
> In PHP it is very simple: $t = microtime(); in the begin ... echo
> (microtime() – $t); in the end. It is true info.
>
> What about Flask?
>
> Thank you! :)

Re: [flask] How can I simple get page generation time?

From:
Tark
Date:
2013-01-13 @ 00:18
Hm...
But when request processing is started, Flask doing following things:
1) WSGI-server process request
2) Werkzeug routing system
3) Inner view function
4) Jinja2 template processing
5) Responce sending

I need determine the start time in case 1 and the end time in case 4. In 
another case it would be not so honest information.

-----Исходное сообщение----- 
From: Markus Unterwaditzer
Sent: Sunday, January 13, 2013 2:52 AM
To: flask@librelist.com
Subject: Re: [flask] How can I simple get page generation time?

It is kind of similar to PHP.

The thing is that you don't return from render_template immediately
like this:

@app.route('/')
def index():
     return render_template('index.htm')

but rather like:

import time

@app.route('/')
def index():
     start = time.time()
     html = render_template('index.htm')
     processing_time = time.time() - start
     # do something with the processing_time
     return html

You get the idea. The downside of this is that you can't include the
time in your template, but you can still log it or send it in a header.
Or you just don't time the template rendering process, but just the
stuff in the view function.

On 2013-01-12 23:40, Tark wrote:
> Hello, {{ user.name }}!
>
> How can I simple and more honestly get page generation time in Flask
> and show this info on the page?
> OK, @app.before_request helps, but it is not a start time of
> processing, isn’t it?
> And what about the end of processing?
> And how can I show this info? I mean, I need to change some strings
> in response.
>
> In PHP it is very simple: $t = microtime(); in the begin ... echo
> (microtime() – $t); in the end. It is true info.
>
> What about Flask?
>
> Thank you! :) 

Re: [flask] How can I simple get page generation time?

From:
Steven Kryskalla
Date:
2013-01-13 @ 00:31
On Sat, Jan 12, 2013 at 4:18 PM, Tark <tark.hidden@gmail.com> wrote:
> Hm...
> But when request processing is started, Flask doing following things:
> 1) WSGI-server process request
> 2) Werkzeug routing system
> 3) Inner view function
> 4) Jinja2 template processing
> 5) Responce sending
>
> I need determine the start time in case 1 and the end time in case 4. In
> another case it would be not so honest information.

To get #1, #2, and #5 you need to go to the WSGI layer.

An easy way to get #1 and #2 would be to inject time.time() into the
WSGI environ.

To get #5 is more tricky, afaik you'd have to 'buffer' the response of
the inner WSGI app, record the time, then send the response back.
That is inefficient and tricky to get correct, so unless you have a
specific problem with #5 being slow, I would ignore it. Or you can go
one more level up and measure it there (e.g. at the reverse proxy
level, like $request_time in nginx).

Re: [flask] How can I simple get page generation time?

From:
Tark
Date:
2013-01-13 @ 03:12
Thank you all for your help. If I will decide this problem with some tricks, 
I'll show my code.
Sure, it would be useful :)

-----Исходное сообщение----- 
From: Steven Kryskalla
Sent: Sunday, January 13, 2013 4:31 AM
To: flask@librelist.com
Subject: Re: [flask] How can I simple get page generation time?

On Sat, Jan 12, 2013 at 4:18 PM, Tark <tark.hidden@gmail.com> wrote:
> Hm...
> But when request processing is started, Flask doing following things:
> 1) WSGI-server process request
> 2) Werkzeug routing system
> 3) Inner view function
> 4) Jinja2 template processing
> 5) Responce sending
>
> I need determine the start time in case 1 and the end time in case 4. In
> another case it would be not so honest information.

To get #1, #2, and #5 you need to go to the WSGI layer.

An easy way to get #1 and #2 would be to inject time.time() into the
WSGI environ.

To get #5 is more tricky, afaik you'd have to 'buffer' the response of
the inner WSGI app, record the time, then send the response back.
That is inefficient and tricky to get correct, so unless you have a
specific problem with #5 being slow, I would ignore it. Or you can go
one more level up and measure it there (e.g. at the reverse proxy
level, like $request_time in nginx). 

Re: [flask] How can I simple get page generation time?

From:
Raj Nathani
Date:
2013-01-13 @ 00:29
Hey Tark!

You can achieve that by using the @before_request and @after_request decorators.

On 2013-01-12, at 7:25 PM, "Tark" <tark.hidden@gmail.com> wrote:

> Hm...
> But when request processing is started, Flask doing following things:
> 1) WSGI-server process request
> 2) Werkzeug routing system
> 3) Inner view function
> 4) Jinja2 template processing
> 5) Responce sending
> 
> I need determine the start time in case 1 and the end time in case 4. In 
> another case it would be not so honest information.
> 
> -----Исходное сообщение----- 
> From: Markus Unterwaditzer
> Sent: Sunday, January 13, 2013 2:52 AM
> To: flask@librelist.com
> Subject: Re: [flask] How can I simple get page generation time?
> 
> It is kind of similar to PHP.
> 
> The thing is that you don't return from render_template immediately
> like this:
> 
> @app.route('/')
> def index():
>     return render_template('index.htm')
> 
> but rather like:
> 
> import time
> 
> @app.route('/')
> def index():
>     start = time.time()
>     html = render_template('index.htm')
>     processing_time = time.time() - start
>     # do something with the processing_time
>     return html
> 
> You get the idea. The downside of this is that you can't include the
> time in your template, but you can still log it or send it in a header.
> Or you just don't time the template rendering process, but just the
> stuff in the view function.
> 
> On 2013-01-12 23:40, Tark wrote:
>> Hello, {{ user.name }}!
>> 
>> How can I simple and more honestly get page generation time in Flask
>> and show this info on the page?
>> OK, @app.before_request helps, but it is not a start time of
>> processing, isn’t it?
>> And what about the end of processing?
>> And how can I show this info? I mean, I need to change some strings
>> in response.
>> 
>> In PHP it is very simple: $t = microtime(); in the begin ... echo
>> (microtime() – $t); in the end. It is true info.
>> 
>> What about Flask?
>> 
>> Thank you! :) 
>