原文出处

前端相关数据监察和控制

2015/08/16 · HTML5 ·
数码监察和控制

初稿出处:
AlloyTeam   

花色开拓到位外发后,未有一个监察系统,大家很难驾驭到公布出去的代码在客商机器上进行是还是不是精确,所以供给树立前端代码品质相关的监督检查系统。

因此我们供给做以下的有的模块:

一、采撷脚本执行错误

JavaScript

function error(msg,url,line){ var REPORT_U索罗德L = “xxxx/cgi”; //
搜罗报告数据的新闻 var m =[msg, url, line, navigator.userAgent, +new
Date];// 收罗错误音信,发生错误的本子文件互连网地址,客商代理消息,时间
var url = REPORT_U传祺L + m.join(‘||’);// 组装错误反馈新闻内容UMuranoL var img
= new Image; img.onload = img.onerror = function(){ img = null; };
img.src = url;// 发送数据到后台cgi } // 监听错误上报 window.onerror =
function(msg,url,line){ error(msg,url,line); }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function error(msg,url,line){
   var REPORT_URL = "xxxx/cgi"; // 收集上报数据的信息
   var m =[msg, url, line, navigator.userAgent, +new Date];// 收集错误信息,发生错误的脚本文件网络地址,用户代理信息,时间
   var url = REPORT_URL + m.join(‘||’);// 组装错误上报信息内容URL
   var img = new Image;
   img.onload = img.onerror = function(){
      img = null;
   };
   img.src = url;// 发送数据到后台cgi
}
// 监听错误上报
window.onerror = function(msg,url,line){
   error(msg,url,line);
}

通过管理后台系统,大家能够看看页面上每一趟错误的音信,通过那几个信息我们能够火速定位同一时候化解难点。

二、收集html5 performance信息

performance
包罗页面加载到推行到位的总体生命周期中分裂施行步骤的推行时间。performance相关文章点击如下:采纳performance
API
监测页面质量

计量不一致步骤时间相对于navigationStart的时光差,能够因而相应后台CGI搜罗。

JavaScript

function _performance(){ var REPORT_URL = “xxxx/cgi?perf=”; var perf =
(window.webkitPerformance ? window.webkitPerformance :
window.msPerformance ), points =
[‘navigationStart’,’unloadEventStart’,’unloadEventEnd’,’redirectStart’,’redirectEnd’,’fetchStart’,’domainLookupStart’,’connectStart’,’requestStart’,’responseStart’,’responseEnd’,’domLoading’,’domInteractive’,’domContentLoadedEventEnd’,’domComplete’,’loadEventStart’,’loadEventEnd’];
var timing = pref.timing; perf = perf ? perf : window.performance; if(
perf && timing ) { var arr = []; var navigationStart =
timing[points[0]]; for(var i=0,l=points.length;i<l;i++){ arr[i]
= timing[points[i]] – navigationStart; } var url = REPORT_URL +
arr.join(“-“); var img = new Image; img.onload = img.onerror =
function(){ img=null; } img.src = url; }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function _performance(){
   var REPORT_URL = "xxxx/cgi?perf=";
   var perf = (window.webkitPerformance ? window.webkitPerformance : window.msPerformance ),
      points = [‘navigationStart’,’unloadEventStart’,’unloadEventEnd’,’redirectStart’,’redirectEnd’,’fetchStart’,’domainLookupStart’,’connectStart’,’requestStart’,’responseStart’,’responseEnd’,’domLoading’,’domInteractive’,’domContentLoadedEventEnd’,’domComplete’,’loadEventStart’,’loadEventEnd’];
   var timing = pref.timing;
   perf = perf  ? perf : window.performance;
   if( perf  && timing ) {
      var arr = [];
      var navigationStart = timing[points[0]];
      for(var i=0,l=points.length;i<l;i++){
         arr[i] = timing[points[i]] – navigationStart;
      }
   var url = REPORT_URL + arr.join("-");
   var img = new Image;
   img.onload = img.onerror = function(){
      img=null;
   }
   img.src = url;
}

透过后台接口搜聚和总结,我们得以对页面施行质量有很详细的摸底。

三、总括各样页面的JS和CSS加载时间

在JS只怕CSS加载此前打上时间戳,加载之后打上时间戳,何况将数据反馈到后台。加载时间反映了页面白屏,可操作的等候时间。

XHTML

<script>var cssLoadStart = +new Date</script> <link
rel=”stylesheet” href=”xxx.css” type=”text/css” media=”all”> <link
rel=”stylesheet” href=”xxx1.css” type=”text/css” media=”all”>
<link rel=”stylesheet” href=”xxx2.css” type=”text/css”
media=”all”> <sript> var cssLoadTime = (+new Date) –
cssLoadStart; var jsLoadStart = +new Date; </script> <script
type=”text/javascript” src=”xx1.js”></script> <script
type=”text/javascript” src=”xx2.js”></script> <script
type=”text/javascript” src=”xx3.js”></script> <script>
var jsLoadTime = (+new Date) – jsLoadStart; var REPORT_URL =
‘xxx/cgi?data=’ var img = new Image; img.onload = img.onerror =
function(){ img = null; }; img.src = REPORT_URL + cssLoadTime + ‘-‘ +
jsLoadTime; </script>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<script>var cssLoadStart = +new Date</script>
<link rel="stylesheet" href="xxx.css" type="text/css" media="all">
<link rel="stylesheet" href="xxx1.css" type="text/css" media="all">
<link rel="stylesheet" href="xxx2.css" type="text/css" media="all">
<sript>
   var cssLoadTime = (+new Date) – cssLoadStart;
   var jsLoadStart = +new Date;
</script>
<script type="text/javascript" src="xx1.js"></script>
<script type="text/javascript" src="xx2.js"></script>
<script type="text/javascript" src="xx3.js"></script>
<script>
   var jsLoadTime = (+new Date) – jsLoadStart;
   var REPORT_URL = ‘xxx/cgi?data=’
   var img = new Image;
   img.onload = img.onerror = function(){
      img = null;
   };
   img.src = REPORT_URL + cssLoadTime + ‘-‘ + jsLoadTime;
</script>

XHTML

<a href=””
target=”_blank”> </a>

1
<a href="https://github.com/perltzhu/js-data-report" target="_blank"> </a>

仿照效法资料:

  • html5 performance
    en
  • html5 performance
    cn
  • javascript onerror
    api

    1 赞 1 收藏
    评论

图片 1

相关文章