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



我需要找到我目前正在构建的网站的加载时间。我已经在本地托管了该网站。如何使用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))`

只需将 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(

最新更新