一个简单的网页性能收集平台

本文介绍了一种前端页面性能监测方案,通过window.performance API收集关键性能指标,包括页面加载时间、资源加载时间等,并将其存储在数据库中进行分析。重点讨论了如何利用这些指标优化用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

根据需求我们需要对公司网站进行性能的收集,来为我们优化页面提供支持

我们的目标是通过该平台查询公司开发的页面性能是否达到标准。

初步的标准是页面需满足: ready < 800、 readyTTFB < 600、 load < 3000、 loadTTFB < 2000

相关的性能数据是通过window.performance进行收集的

 

 

浏览器支持情况

IE9和chrome6以上的版本都支持:

•window.performance : ie9

•window.webkitPerformance : chrome6-9

•window.performance : chrome10+

 

window.performance.timing property  

 

(其属性的相关描述摘自官网https://developer.mozilla.org/en-US/docs/Web/API/PerformanceTiming)

此处篇幅略长,博主觉得作为前端人员理解这个是非常有必要的

 

PerformanceTiming.navigationStartRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, right after the prompt for unload terminates on the previous document in the same browsing context. If there is no previous document, this value will be the same as PerformanceTiming.fetchStart.

初始化页面,在同一个浏览器上下文中前一个页面unload的时间戳,如果没有前一个页面的unload,则与fetchStart值相等

 

PerformanceTiming.unloadEventStartRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, theunload event has been thrown, indicating the time at which the previous document in the window began to unload. If there is no previous document, or if the previous document or one of the needed redirects is not of the same origin, the value returned is0.

unloadEventStart 前一个页面的unload的时间戳 如果没有则为0、

 

PerformanceTiming.unloadEventEndRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, theunload event handler finishes. If there is no previous document, or if the previous document, or one of the needed redirects, is not of the same origin, the value returned is0.

unloadEventEnd 与unloadEventStart 相对应,返回的是unload函数执行完成的时间戳

 

PerformanceTiming.redirectStartRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, the first HTTP redirect starts. If there is no redirect, or if one of the redirects is not of the same origin, the value returned is 0.

第一个http重定向发生的时间 有跳转且是同域的重定向 否则为0

 

PerformanceTiming.redirectEndRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, the last HTTP redirect is completed, that is when the last byte of the HTTP response has been received. If there is no redirect, or if one of the redirects is not of the same origin, the value returned is0.

redirectEnd 最后一个重定向完成时的时间,否则为0

 

PerformanceTiming.fetchStartRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, the browser is ready to fetch the document using an HTTP request. This moment isbefore the check to any application cache.

fetchStart 浏览器准备好使用http请求抓取文档的时间,这发生在检查缓存之前

 

PerformanceTiming.domainLookupStartRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, where the domain lookup starts. If a persistent connection is used, or the information is stored in a cache or a local resource, the value will be the same as PerformanceTiming.fetchStart.

domainLookupStart DNS域名开始查询的时间,如果是持久性链接或者本地的缓存则值与fetchstart相同

 

PerformanceTiming.domainLookupEndRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, where the domain lookup is finished. If a persistent connection is used, or the information is stored in a cache or a local resource, the value will be the same as PerformanceTiming.fetchStart.

域名查询完成时间

 

PerformanceTiming.connectStartRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, where the request to open a connection is sent to the network. If the transport layer reports an error and the connection establishment is started again, the last connection establishment start time is given. If a persistent connection is used, the value will be the same asPerformanceTiming.fetchStart.

connectStart Http(TCP) 开始建立连接的时间,如果是持久连接,则与fecthStart 值相等。  (注意 如果在传输层发生了错误且重新建立了连接,则这里显示的是新建立的连接开始时间)

 

PerformanceTiming.connectEndRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, where the connection is opened network. If the transport layer reports an error and the connection establishment is started again, the last connection establishment end time is given. If a persistent connection is used, the value will be the same asPerformanceTiming.fetchStart. A connection is considered as opened when all secure connection handshake, or SOCKS authentication, is terminated.

connectEnd  Http(Tcp) 完成握手的时间,如果是持久连接则与fecthStart 值相等。(注意 如果在传输层发生了错误且重新建立了连接,则这里显示的是新连接完成的时间。 这里包括ssl等授权通过)

 

PerformanceTiming.secureConnectionStartRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, where the secure connection handshake starts. If no such connection is requested, it returns0.

安全链接握手开始时间, 如果不存在这样的链接则值为零

PerformanceTiming.requestStartRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, when the browser sent the request to obtain the actual document, from the server or from a cache. If the transport layer fails after the start of the request and the connection is reopened, this property will be set to the time corresponding to the new request.

requestStart http请求读取真实文档开始的时间(完成建立连接),包括从本地缓存读取。 连接错误时这里也显示重新建立连接的时间。

PerformanceTiming.responseStartRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, when the browser received the first byte of the response, from the server from a cache, or from a local resource.

浏览器开始接受响应值的时间

PerformanceTiming.responseEndRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, when the browser received the last byte of the response, or when the connection is closed if this happened first, from the server, the cache, or from a local resource.

响应结束时间

 

PerformanceTiming.domLoadingRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, when the parser started its work, that is when itsDocument.readyState changes to 'loading' and the correspondingreadystatechange event is thrown.

domloading 开始解析渲染dom树的时间,此时Document.readyState 变成loading ,并将抛出readyStateChange 事件

 

PerformanceTiming.domInteractiveRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, when the parser finished its work on the main document, that is when itsDocument.readyState changes to 'interactive' and the correspondingreadystatechange event is thrown.

dominteractive 完成解析DOM树的时间,Document.readyState 变成interactive,并将抛出readyStateChange事件(注意 只是DOM树解析完成,这时候并没有开始加载网页内的资源)

 

PerformanceTiming.domContentLoadedEventStartRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, right before the parser sent theDOMContentLoaded event, that is right after all the scripts that need to be executed right after parsing has been executed.

domContentLoadedEventStart  在DOM树解析完成后,网页内资源加载开始的时间。在DOMcontentLoaded事件抛出前发生

(当初始的 HTML 文档被完全加载和解析完成之后,DOMContentLoaded事件被触发,而无需等待样式表、图像和子框架的完成加载)

 

PerformanceTiming.domContentLoadedEventEndRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, right after all the scripts that need to be executed as soon as possible, in order or not, has been executed.

domContentLoadedEventEnd   DOM树解析完成后,网页内资源加载完成时间(如JS脚本加载执行完成)

 

PerformanceTiming.domCompleteRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, when the parser finished its work on the main document, that is when itsDocument.readyState changes to 'complete' and the correspondingreadystatechange event is thrown.

domCompelete Dom树解析完成,且资源也准备就绪的时间

 

一个document 的Document.readyState 属性描述了文档的加载状态。

一个文档的 readyState 可以是以下之一:

loading / 加载

document 仍在加载。

interactive / 互动

文档已经完成加载,文档已被解析,但是诸如图像,样式表和框架之类的子资源仍在加载。

 

 

complete / 完成

T文档和所有子资源已完成加载。状态表示 load 事件即将被触发。

当这个属性的值变化时,document 对象上的readystatechange 事件将被触发。

 

 

PerformanceTiming.loadEventStartRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, when theload event was sent for the current document. If this event has not yet been sent, it returns0.

loadEventStart load 事件发送给文档,也即load回调函数开始执行的时间

 

 

PerformanceTiming.loadEventEndRead only

Is an unsigned long long representing the moment, in milliseconds since the UNIX epoch, when theload event handler terminated, that is when the load event is completed. If this event has not yet been sent, or is not yet completed, it returns0.

loadEventEnd load回调函数执行完成的时间 

 

 

数据库设计

第一阶段初步分析主要展示ready、ready TTFB、load、load TTFB这四个字段

 

 

 

 

ready:即为dom_content_loaded_event_star开始的时间 在DOM树解析完成后,网页内资源加载开始的时间

 

 

ready TTFB:dom_content_loaded_event_start - response_start  作为前端性能分析我们更多的关注点页面载入耗时,这个字段是忽略服务器请求时间

 

load:   load_event_start 即为load回调函数开始执行的时间

 

loadTTFB:  load_event_start - response_start  load回调函数减去服务器开始请求时间的差值

考虑到数据量的问题,查询时仅提供一天的数据量信息,默认时间为当前时间的前一天

随着数据的增多,发现查询的速度慢了好多, 主要是相同的URL进行合并查询占用大量时间。

真的该问题增设一临时表,每天定时跑一下任务将前天合并查询后的数据进行导入

 

 

后台egg架构

 

 

controller: 用于请求响应以及返回结果

                 a.获取HTTP的请求参数

    b.对相关参数进行校验以及各式化的相关处理,

                  例如在此:将驼峰式的参数转化成下划线以便于存入数据库、将其他的参数字段        与 navigationStart进行差值处理、异常数据监测等等

                c.通过HTTP将结果响应给用户

service:    按照官方文档 简单来说,Service 就是在复杂业务场景下用于做业务逻辑封装的一个抽象层。由于我们的业务目前来说比较简单,在此用于进行相关的SQL操作

 

router: 主要用来描述请求 URL 和具体承担执行动作的 Controller 的对应关系, 框架约定了 app/router.js 文件用于统一所有路由规则

 

datasql: 用于存放数据库设计的文档

 

utils: 用于存放一些涉及到的工具类

 

antx.properties:本地开发时,对服务器、端口、数据库进行相关配置

 

 

总结

 该项目的实现方案:根据window.performance进行时间收集,传入后台存储到数据库中;然后对数据库进行相关的查询操作(简单到我都不知道该说些什么了(⊙﹏⊙)b,不过后期还会对该平台进行改进丰富的)。

 

 

某网站性能测试用例 某网站提供会员模板下载、上传、购买、支付等功能,目前进入性能测试阶段,通过性能需求可以了解到主要有以下几个性能指标需要进行测试:   ● 产品页面刷新性能   ● 产品上传性能   ● 产品下载性能   目前给出的指标为:   延迟:   测试项 响应时间 抖动 备注   产品页面刷新 <5秒 <2秒   产品下载相应时间 <4秒 <2秒   吞吐量:   编号 项 吞吐量   Perf.T.1 所有登录用户在线状态更改频率 每10分钟1次   Perf.T.2 每日页面平均访问量 60000次   Perf.T.3 每日下载量 50000   Perf.T.4 平均每日新增会员数量 500   Perf.T.5 高峰同一模板下载量 100用户并发下载   Perf.T.6 高峰不同模板下载量 150用户并发下载   容量:   编号 项 容量   Perf.C.1 用户数 <=100万   Perf.C.2 活动用户数 10000   Perf.C.3 模板中心总用户数 <=25万   根据如上性能需求及数据我们该如何设计性能测试用例及场景呢?(可以说给出的性能需求很垃圾,没有丝毫价值,但没办法还是点做啊)   首先,我不去在乎它要求的性能是什么,我只需要去做在一定的测试环境下对系统进行压力测试,找到各个性能指标的临界点就好了,至于是否达到性能指标,在和性能需求对照编写测试报告即可。   所以,针对这几个需要进行性能测试的页面,我们做一下分析,如何设计场景才能尽可能准确地体现出系统的性能:   先说一下搜索页面   搜索页面根据对项目的了解,搜索后,将所有符合条件的结果遍历出来,显示在前台,每页的显示数量是一定的,超出的部分分页显示。根据上面的描述我们可以看出搜索结果是在将符合条件的所有结果集均发送到前台页面,对于页面显示对性能的消耗我们可以忽略不计,主要的压力来自数据的传输、sql的执行及应用服务器的处理过程,所以我可以从两个方面设计场景:   a、虚拟用户一定,不同数据数量级的情况下,搜索的性能   如何确定虚拟用户的数量成为一个关键,我们可以让客户提供一个常规情况下每天访问用户数(如果没有实际数据可参考,可以根据产品方案中期望的用户数来代替),我们就用这个用户数来进行测试;再来分析一下不同的数据数量级,如果系统运营1年的产品数据量是5万条,那么我们就根据这个值分别取1W条、3W 条、5W条、10W条、20W条数据量来进行测试(具体的分法可以根据实际情况而定),所以对于这个测试目标,我们可以设计5个场景进行:   虚拟用户数 数据数量级 录制页面 并发用户数执行时间思考时间   100 10000 搜索页面 随机产生 30分钟 加入思考时间   100 30000 搜索页面 随机产生 30分钟 加入思考时间   100 50000 搜索页面 随机产生 30分钟 加入思考时间   100 100000 搜索页面 随机产生 30分钟 加入思考时间   100 200000 搜索页面 随机产生 30分钟 加入思考时间   b、一定数据数量级,不同量虚拟用户的情况下,搜索的性能   我们定下来一个常规的数据数据量,在数据量不变的情况下逐步增加虚拟用户数,测试一下不同虚拟用户压力下系统的性能   虚拟用户数 数据数量级 录制页面 并发用户数执行时间思考时间   50 50000 搜索页面 随机产生 30分钟 加入思考时间   80 50000 搜索页面 随机产生 30分钟 加入思考时间   100 50000 搜索页面 随机产生 30分钟 加入思考时间   120 50000 搜索页面 随机产生 30分钟 加入思考时间   150 50000 搜索页面 随机产生 30分钟 加入思考时间   产品上传   影响上传性能的主要因素有上传文件的大小和上传的请求数,所以我们就从这两个方面设计用例。   a、虚拟用户数一定,上传不同大小的文件   虚拟用户数 上传文件大小 录制页面 并发用户数 执行时间 思考时间   50 100k 上传页面 随机产生 30分钟 取消思考时间   50 300k 上传页面 随机产生 30分钟 取消思考时间   50 500k 上传页面 随机产生 30分钟 取消思考时间   50 800k 上传页面 随机产生 30分钟 取消思考时间   50 1M 上传页面 随机产生 30分钟 取消思考时间   b、上传文件大小一定,不同量的虚拟用户   虚拟用户数 上传文件大小 录制页面 并发用户数执行时间思考时间   20 300k 上传页面 随机产生 30分钟 取消思考时间   50 300k 上传页面 随机产生 30分钟 取消思考时间   80 300k 上传页面 随机产生 30分钟 取消思考时间   100 300k 上传页面 随机产生 30分钟 取消思考时间   产品下载   影响下载性能的主要因素有下载文件的大小和下载的请求数,所以我们就从这两个方面设计用例   a、虚拟用户数一定,下载不同大小的文件   虚拟用户数 下载文件大小 录制页面 并发用户数执行时间思考时间   50 100k 下载页面 随机产生 30分钟 取消思考时间   50 300k 下载页面 随机产生 30分钟 取消思考时间   50 500k 下载页面 随机产生 30分钟 取消思考时间   50 800k 下载页面 随机产生 30分钟 取消思考时间   50 1M 下载页面 随机产生 30分钟 取消思考时间   b、下载文件大小一定,不同量的虚拟用户   虚拟用户数 下载文件大小 录制页面 并发用户数 执行时间 思考时间   20 300k 下载页面 随机产生 30分钟 取消思考时间   50 300k 下载页面 随机产生 30分钟 取消思考时间   80 300k 下载页面 随机产生 30分钟 取消思考时间   100 300k 下载页面 随机产生 30分钟 取消思考时间
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值