0

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))`
don don
  • 3
  • 3

1 Answers1

0

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)

suvojit_007
  • 1,690
  • 2
  • 17
  • 23
  • Does this give load time or response time? – don don Oct 11 '18 at 13:48
  • This gives loading time. 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. – suvojit_007 Oct 11 '18 at 13:50
  • But the timer does not increase though all the contents are not loaded yet. – don don Oct 11 '18 at 13:55
  • Have you tried the above two codes? Time increases when you load some external libraries. – suvojit_007 Oct 11 '18 at 13:59
  • What do you mean by external libraries? – don don Oct 11 '18 at 14:02
  • Like in the example -2 in my answer, I have used **jquery library** to compute the load time. – suvojit_007 Oct 11 '18 at 14:06
  • I want the timer to increase itself until all the contents are loaded. But, with the above code, only a particular millisecond is given. Only when I reload the page then the time is changed. – don don Oct 11 '18 at 14:10
  • Basically, the above code is measuring the time at the start of the loading and also the time at the end of the script and by subtracting the initial value from the end we can get out the result. It won't be that simple to make a timer that would increment the time while scanning the page data. – suvojit_007 Oct 11 '18 at 14:23