首先安装webdriver(要与浏览器版本对应,这方面帖子很多,就不介绍了)
web时间的获取模型如下图:
其中参数的意义:
navigationStart 加载起始时间
redirectStart 重定向开始时间(如果发生了HTTP重定向,每次重定向都和当前文档同域的话,就返回开始重定向的fetchStart的值。其他情况,则返回0)
redirectEnd 重定向结束时间(如果发生了HTTP重定向,每次重定向都和当前文档同域的话,就返回最后一次重定向接受完数据的时间。其他情况则返回0)
fetchStart 浏览器发起资源请求时,如果有缓存,则返回读取缓存的开始时间
domainLookupStart 查询DNS的开始时间。如果请求没有发起DNS请求,如keep-alive,缓存等,则返回fetchStart
domainLookupEnd 查询DNS的结束时间。如果没有发起DNS请求,同上
connectStart 开始建立TCP请求的时间。如果请求是keep-alive,缓存等,则返回domainLookupEnd
(secureConnectionStart) 如果在进行TLS或SSL,则返回握手时间
connectEnd 完成TCP链接的时间。如果是keep-alive,缓存等,同connectStart
requestStart 发起请求的时间
responseStart 服务器开始响应的时间
domLoading 从图中看是开始渲染dom的时间,具体未知
domInteractive 未知
domContentLoadedEventStart 开始触发DomContentLoadedEvent事件的时间
domContentLoadedEventEnd DomContentLoadedEvent事件结束的时间
domComplete 从图中看是dom渲染完成时间,具体未知
loadEventStart 触发load的时间,如没有则返回0
loadEventEnd load事件执行完的时间,如没有则返回0
unloadEventStart unload事件触发的时间
unloadEventEnd unload事件执行完的时间
以下是获取时间的代码:
page=[]
a = webdriver.Firefox()
获取的时间是一个javascript类型,需要用execute_script函数来获取
page.append(a.execute_script("return performance.timing.navigationStart"))
page.append(a.execute_script("return performance.timing.redirectStart"))
page.append(a.execute_script("return performance.timing.redirectEnd"))
page.append(a.execute_script("return performance.timing.fetchStart"))
page.append(a.execute_script("return performance.timing.domainLookupStart"))
page.append(a.execute_script("return performance.timing.domainLookupEnd"))
page.append(a.execute_script("return performance.timing.connectStart"))
page.append(a.execute_script("return performance.timing.connenctEnd"))
page.append(a.execute_script("return performance.timing.responseStart"))
page.append(a.execute_script("return performance.timing.requestStart"))
page.append(a.execute_script("return performance.timing.domLoading"))
page.append(a.execute_script("return performance.timing.domComplete"))
page.append(a.execute_script("return performance.timing.loadEventStart"))
page.append(a.execute_script("return performance.timing.loadEventEnd "))
page.append(a.execute_script("return performance.timing.responseStart") - a.execute_script("return performance.timing.requestStart")) # 请求到响应时间
page.append(a.execute_script("return performance.timing.redirectStart") - a.execute_script("return performance.timing.redirectEnd")) # 重定向时间
page.append(a.execute_script("return performance.timing.domainLookupEnd") - a.execute_script("return performance.timing.domainLookupStart")) # DNS解析时间
page.append(a.execute_script("return performance.timing.domComplete") - a.execute_script("return performance.timing.domLoading")) # 页面渲染时间
page.append(a.execute_script("return performance.timing.responseStart") - a.execute_script("return performance.timing.navigationStart")) # 白屏时间
然后 print page 就可以获取所有时间了
这里获取的时间是毫秒戳