javascriptpythonload-time

How to find load time of the website using python or javascript


I need to find the load time of a website that I am currently building. I have hosted the site locally. How can I find its load time using python or javascript? I tried the following code, but I think it is for response time.

from time import time
from urllib.request import urlopen

stream = urlopen('https://01c92730.ngrok.io/index.html')
cream = urlopen('https://01c92730.ngrok.io/Webpage.html')
start_time = time()
output = stream.read()
end_time = time()
stream.close()
print(round(end_time-start_time, 3))

start = time()
output = cream.read()
end = time()
cream.close()
print(round(end-start, 3))`

Solution

  • Just put var start = Date.now() at the start of the page and (Date.now() - start) at the end of the page to get the loading time.

    Case 1# without loading a library

    <doctype html>
    <html>
        <head>
    
            <script type="text/javascript">
                var start = Date.now();
            </script>
        </head>
        <body>
    
            <script type="text/javascript">
                document.write("Page load time " + (Date.now() - start) + "  milliseconds");
            </script>
    
    
    </body>
    </html>
    

    Output: Page load time 0 milliseconds(Sometimes 1 millisecond)

    Case 2# loading jquery library

    <doctype html>
    <html>
        <head>
    
            <script type="text/javascript">
                var start = Date.now();
            </script>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        </head>
        <body>
    
            <script type="text/javascript">
                document.write("Page load time " + (Date.now() - start) + "  milliseconds");
            </script>
    
    
    </body>
    </html>
    

    Output: Page load time 24 milliseconds( sometimes 22,23)