如何使用python或javascript查找网站的加载时间

时间:2018-10-11 13:25:17

标签: javascript python load-time

我需要找到当前正在构建的网站的加载时间。我已经在本地托管了该网站。如何使用python或javascript找到其加载时间?我尝试了以下代码,但我认为这是为了响应时间。

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))`

1 个答案:

答案 0 :(得分:0)

只需将var start = Date.now()放在页面的开头,将(Date.now()-start)放在页面的末尾即可获取加载时间。

在没有加载库的情况下进行1#情况

<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>

输出:页面加载时间0毫秒(有时1毫秒)

案例2#加载jquery库

<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>

输出:页面加载时间为24毫秒(有时为22,23)