Avatar

Blogging tips

Avatar

Python, Django and Flask

Avatar

Latest tech posts

Friday, December 13, 2019

Find page loading time using python

We will be using an in-built python library timeit.




So now, let’s start exploring this handy library!

Timeit function

The module function timeit.timeit(stmt, setup, timer, number) accepts four arguments:

1. stmt which is the statement you want to measure.
2. setup which is the code that you run before running the stmt. We generally use this to import the required modules for our code.
3. timer which is a timeit.Timer object; it usually has a sensible default value so you don’t have to worry about it.
4. number which is the number of executions you’d like to run the stmt.

 Where the timeit.timeit() function returns the number of seconds it took to execute the code.

Idea

We are going to use the urlopen function to open the webpage. This will return a response after the page has loaded. So finding the execution time of the urlopen function gives us the loading time of the webpage.

Snippet

This is the code snippet. The output will be in seconds.



#timeit #python #webpageloadingtime

No comments:

Post a Comment