干貨 | H5性能分析實(shí)戰(zhàn)來(lái)啦~
H5性能該如何測(cè)試呢?很多人不知道該如何下手。其實(shí)可以借用W3C協(xié)議完成自動(dòng)化H5性能測(cè)試。
因?yàn)閃3C標(biāo)準(zhǔn)是瀏覽器標(biāo)準(zhǔn),一般瀏覽器都支持W3C標(biāo)準(zhǔn),它規(guī)定使用者可以通過(guò)api查詢(xún)性能信息。W3C官網(wǎng):Navigation Timing
更多關(guān)于專(zhuān)項(xiàng)測(cè)試的文章,可以點(diǎn)擊公眾號(hào)關(guān)注哦~
前文使用chrome瀏覽器對(duì)webview進(jìn)行手工查看,伴隨著業(yè)務(wù)增多,數(shù)量加大,手工操作的速度會(huì)無(wú)法跟上業(yè)務(wù)增長(zhǎng),此時(shí)需要自動(dòng)化方法測(cè)試webview性能。
當(dāng)頁(yè)面加載時(shí),會(huì)渲染一系列內(nèi)容,渲染過(guò)程可分為多個(gè)階段,比如下圖:

Prompt for unload 訪問(wèn)一個(gè)新頁(yè)面時(shí),舊頁(yè)面卸載完成的時(shí)間 ? redirect 重定向,用戶注銷(xiāo)登陸時(shí)返回主頁(yè)面和跳轉(zhuǎn)到其它的網(wǎng)站等 ? App cache 檢查緩存,是否打開(kāi) ? DNS 表示 DNS 查詢(xún)的時(shí)間,如果是長(zhǎng)連接或者請(qǐng)求文件來(lái)自緩存等本地存儲(chǔ)則返回fetchStart時(shí)間點(diǎn) ? TCP 與服務(wù)器建立鏈接的時(shí)間 ? Requests 客戶端發(fā)起請(qǐng)求的時(shí)間 ? Response 拿到服務(wù)器第一個(gè)響應(yīng)字節(jié)到最后一個(gè)響應(yīng)字節(jié)的時(shí)間 ? Processing 各種狀態(tài)的時(shí)間點(diǎn),例如加載狀態(tài)等等 ? onLoad 觸發(fā)load事件執(zhí)行的時(shí)間
在chrome瀏覽器中,執(zhí)行js代碼可獲取各個(gè)階段的內(nèi)容:
window.performance.timing

上面的時(shí)間只是一個(gè)時(shí)間點(diǎn),如果想獲取各階段的具體時(shí)間,就需要對(duì)兩個(gè)時(shí)間點(diǎn)進(jìn)行相減運(yùn)算,比如計(jì)算domContent加載事件時(shí)間:
window.performance.timing.\?
domContentLoadedEventEnd -\
?window.performance.timing.\?
domContentLoadedEventStart

appium/selenium可以執(zhí)行js,借用appium/selenium工具可實(shí)現(xiàn)自動(dòng)化獲取能指標(biāo),調(diào)用appium/selenium的ExecuteScriptapi,可向頁(yè)面注入下面代碼:
//顯示所有階段的時(shí)間點(diǎn)
return?
JSON.stringify(window.performance.timing)?
//顯示指定資源的時(shí)間,比如img?
return
JSON.stringify(window.performance.\?
getEntriesByName (document.querySelector("img").src)[0], null, 2)
使用python+selenium進(jìn)行js注入:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://home.testing-studio.com/")
print(driver.execute_script(
? ?"return JSON.stringify(window.performance.timing)"))
執(zhí)行后會(huì)返回一個(gè)json數(shù)據(jù),包含了簡(jiǎn)介中的各個(gè)性能指標(biāo),可對(duì)性能指標(biāo)做二次處理或可視化展示:
{"navigationStart":1585043212714,?
"unloadEventStart":0,
"unloadEventEnd":0,"redirectStart":0,
"redirectEnd":0,
?"fetchStart":1585043212717,?
"domainLookupStart":1585043212747,
"domainLookupEnd":1585043212747,
?"connectStart":1585043212747,?
"connectEnd":1585043212835,
?"secureConnectionStart":1585043212787,
"requestStart":1585043212836,?
"responseStart":1585043212918,?
"responseEnd":1585043212921,
?"domLoading":1585043212929,?
"domInteractive":1585043214972,
"domContentLoadedEventStart":1585043214972,
"domContentLoadedEventEnd":1585043214972,
"domComplete":1585043215976,?
"loadEventStart":1585043215976,?
"loadEventEnd":1585043215976}