实战演示 H5 性能分析(h5页面性能测试)

307阅读模式

W3C标准是浏览器标准,一般浏览器都支持W3C标准,它规定使用者可以通过api查询性能信息,可借用W3C协议完成自动化H5性能测试。
W3C官网: https://www.w3.org/TR/navigation-timing/

使用chrome浏览器对webview进行手工查看,伴随着业务增多,数量加大,手工操作的速度会无法跟上业务增长,此时需要自动化方法测试webview性能。文章源自懂站帝-http://www.sfdkj.com/13894.html

当页面加载时,会渲染一系列内容,渲染过程可分为多个阶段,比如下图:文章源自懂站帝-http://www.sfdkj.com/13894.html

实战演示 H5 性能分析(h5页面性能测试)

文章源自懂站帝-http://www.sfdkj.com/13894.html

• Prompt for unload 访问一个新页面时,旧页面卸载完成的时间文章源自懂站帝-http://www.sfdkj.com/13894.html

• redirect 重定向,用户注销登陆时返回主页面和跳转到其它的网站等文章源自懂站帝-http://www.sfdkj.com/13894.html

• App cache 检查缓存,是否打开文章源自懂站帝-http://www.sfdkj.com/13894.html

• DNS 表示 DNS 查询的时间,如果是长连接或者请求文件来自缓存等本地存储则返回fetchStart时间点文章源自懂站帝-http://www.sfdkj.com/13894.html

• TCP 与服务器建立链接的时间文章源自懂站帝-http://www.sfdkj.com/13894.html

• Requests 客户端发起请求的时间文章源自懂站帝-http://www.sfdkj.com/13894.html

• Response 拿到服务器第一个响应字节到最后一个响应字节的时间文章源自懂站帝-http://www.sfdkj.com/13894.html

• Processing 各种状态的时间点,例如加载状态等等文章源自懂站帝-http://www.sfdkj.com/13894.html

• onLoad 触发load事件执行的时间文章源自懂站帝-http://www.sfdkj.com/13894.html

实战演示 H5 性能分析(h5页面性能测试)

文章源自懂站帝-http://www.sfdkj.com/13894.html

在chrome浏览器中,执行js代码可获取各个阶段的内容:文章源自懂站帝-http://www.sfdkj.com/13894.html

window.performance.timing
实战演示 H5 性能分析(h5页面性能测试)

文章源自懂站帝-http://www.sfdkj.com/13894.html

上面的时间只是一个时间点,如果想获取各阶段的具体时间,就需要对两个时间点进行相减运算,比如计算domContent加载事件时间:文章源自懂站帝-http://www.sfdkj.com/13894.html

window.performance.timing.\
domContentLoadedEventEnd -\
window.performance.timing.\
domContentLoadedEventStart
实战演示 H5 性能分析(h5页面性能测试)

文章源自懂站帝-http://www.sfdkj.com/13894.html

appium/selenium可以执行js,借用appium/selenium工具可实现自动化获取能指标,调用appium/selenium的ExecuteScriptapi,可向页面注入下面代码:文章源自懂站帝-http://www.sfdkj.com/13894.html

//显示所有阶段的时间点

return

JSON.stringify(window.performance.timing)

//显示指定资源的时间,比如img

return

JSON.stringify(window.performance.\

getEntriesByName (document.querySelector("img").src)[0],null,2)

案 例文章源自懂站帝-http://www.sfdkj.com/13894.html

实战演示 H5 性能分析(h5页面性能测试)

文章源自懂站帝-http://www.sfdkj.com/13894.html

H5性能测试需要配合自动化测试工具使用,比如selenium或者appium,通过js代码注入实现自动化调用api。

使用 python + selenium 进行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)"))

执行后会返回一个json数据,包含了简介中的各个性能指标,可对性能指标做二次处理或可视化展示:

{"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,
懂站帝
  • 本文由 发表于 2022年6月6日 22:42:11
  • 版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至395045033@qq.com举报,一经查实,本站将立刻删除。
评论  0  访客  0