This is the second day of my participation in the August More text Challenge. For details, see:August is more challenging
preface
This article shows how to collect performance indicators/error messages and location/resource monitoring data at the front end.
Why do front-end monitoring?
- Faster than users to find problems, and can quickly locate the problem and timely repair
- Collect load/access metrics to optimize user experience in the right direction
- Collect user behavior on the user-oriented website to support the important basis of decision
Third-party Monitoring Service
There are Sentry, Magic, Fundebug and so on.
The general flow of monitoring
- Implementation Monitoring The SDK can monitor performance indicators and errors (collect sources)
- Upload interface
- Data cleaning and log storage
- Reports show
- Monitoring alarm
Subsequent…
- Problem processing
- Auxiliary analysis
- External Platform Access
- .
Performance monitoring
Performance monitoring refers to the data metrics that the pages we write reflect on the user’s side
Performance monitoring is mainly through the window.performance API. Processing this API we can get
- The uninstall time of the previous page
p.fetchStart - p.navigationStart
- The redirection time of the page
p.redirectEnd - p.redirectStart
- DNS lookup time
p.domainLookupEnd - p.domainLookupStart
- TCP connection establishment time
p.connectEnd - p.connectStart
- Total time spent at the network layer
p.connectEnd - p.navigationStart
- The time when chrom sends data to the server and receives it
p.responseStart - p.requestStart
- The time it took to request the data
p.responseEnd - p.responseStart
- The total time taken to request pages
p.responseEnd - p.requestStart
- Dom parsing time
p.domComplete - p.domLoading
- LoadEvent time
p.loadEventEnd - p.loadEventStart
- Total time at the front end
p.loadEventEnd - p.domLoading
- Page full load time
p.loadEventEnd - p.navigationStart
- The time it takes for the DOM to start rendering
p.domContentLoadedEventStart - p.navigationStart
- The time a user action can be responded to
p.domInteractive - p.navigationStart
- First byte time
p.responseStart - p.navigationStart
getPerfData: p= > {
let data = {
// Network connection
prevPage: p.fetchStart - p.navigationStart, // Uninstall time of last page
redirect: p.redirectEnd - p.redirectStart, // The redirect time is 0 if the two pages are not in the same domain
dns: p.domainLookupEnd - p.domainLookupStart, // DNS lookup time
connect: p.connectEnd - p.connectStart, // TCP connection setup time
network: p.connectEnd - p.navigationStart, // The total network time
// Network reception
send: p.responseStart - p.requestStart, // Front end from send to receive time
receive: p.responseEnd - p.responseStart, // Time to request data
request: p.responseEnd - p.requestStart, // The total time of the requested page
// Front-end rendering
dom: p.domComplete - p.domLoading, // Dom parsing time
loadEvent: p.loadEventEnd - p.loadEventStart, / / loadEvent time
frontend: p.loadEventEnd - p.domLoading, // Total front-end time
// Critical stage
load: p.loadEventEnd - p.navigationStart, // Page full load time
domReady: p.domContentLoadedEventStart - p.navigationStart, // Dom preparation time, white screen time
interactive: p.domInteractive - p.navigationStart, // Operable time, when a user's click can be responded to
ttfb: p.responseStart - p.navigationStart // First byte time
}
return data
}
Copy the code
- When the performance. The timing. DomComplete complete explain the dom is loaded, we can report to the server data
// Monitor dom loading
let runCheck = () = > {
if (performance.timing.domComplete) {
// If the DOM parsing is complete, stop the loop and run the callback
clearTimeout(timer)
getPerfData(window.performance.timing) // Get the performance index
perfData.type = 'domready'
callback(perfData)
isDOMReady = true
} else {
// Check the loop again
timer = setTimeout(runCheck, 100)}}Copy the code
- When the performance. The timing. LoadEventEnd complete show page is loaded, we can report to the server data
let runCheck = () = > {
if (performance.timing.loadEventEnd) {
// This determines that the DOM is loaded
// If the DOM parsing is complete, stop the loop and run the callback
clearTimeout(timer)
getPerfData(window.performance.timing) // Get the performance index
perfData.type = 'onload'
callback(perfData)
isOnload = true
} else {
// Check the loop again
timer = setTimeout(runCheck, 100)}}Copy the code
Some indicators of performance
field | describe | note |
---|---|---|
FP | First Paint | Includes any user-defined background drawing, which is the moment the pixels are first drawn to the screen |
FCP | First Content Paint | Is the time it takes the browser to render the first DOM to the screen, be it text, image, SVG, etc., which is essentially white screen time |
FMP | The First Meaningful Paint | Time to render meaningful content of the page |
LCP | Largest Contentful Paint | Represents the loading time of the largest page element in the viewport |
DCL | (DomContentLoaded) | When the HTML document is fully loaded and parsed,DOMContentLoaded Events are triggered without waiting for the stylesheet, images, and subframes to complete loading |
L | (onLoad) | This is triggered when all the dependent resources have been loaded |
TTI | (Time to Interactive) Time that can be exchanged | The point at which an application is visually rendered and can reliably respond to user input |
FID | First Input Delay | The time between the user’s first interaction with the page (clicking a link, clicking a button, etc.) and the page’s response interaction |
Refer to the article
PerformanceObserver.observe
paint-timing
event-timing
LCP
FMP
time-to-interactive