javascript頁面渲染速度測試指令碼分享

來源:互聯網
上載者:User

複製代碼 代碼如下:/* 擷取渲染開始的時間戳記, 儲存在數組PAGE_SPEED_TIME中 */
<html><script type="text/javascript">/*tag*/PAGE_SPEED_TIME = [new Date().getTime()];</script><head>
......
</head>

複製代碼 代碼如下:
......
/* 頁面最末端,計算頁面載入耗用的時間 */
</body>
<script src="http://ossweb-img.qq.com/images/js/pagespeed/page_speed_v2.js"></script></html>


下面來分析一下page_speed_v2.js檔案的內容。對這個指令碼進行重新格式化,以便更加方便閱讀。
複製代碼 代碼如下:
/* 執行匿名函數構建對象PageSpeed */
;(function() {
    PageSpeed = {};/* 這裡沒有 var 關鍵字, 相當於引用的是 window.PageSpeed */

 /**
     * 綁定createScript方法
  *
  * @param String a script標籤的src屬性的值
  * @param String b script標籤的id屬性的值
  */
    PageSpeed.createScript = function(a, b) {
        var c = null;

        if (document.getElementById(b)) { /* script標籤已經存在 */
            c = document.getElementById(b)
        } else { /* 不存在, 建立script標籤 */
            c = document.createElement('script');
            var d = null;
            if (document.getElementsByTagName) {
                d = document.getElementsByTagName('head')[0] || document.documentElement;
            } else {
                d = document.documentElement;
            }
            d.insertBefore(c, d.firstChild); /* 調用insertBefore方法, 將新建立的script標籤插入為第一個子項目 */
        }

  /* 這裡的花括弧起到程式碼片段分組功能 */
  {
            c.setAttribute('type', 'text/html');
            c.setAttribute('style', 'display:none;');
            c.setAttribute('charset', 'gb2312');
            c.setAttribute('id', b);
            c.setAttribute('src', a);
        }

        return c; /* 返回建立成功的script標籤元素 */
    };

 /**
     * 綁定 submitDataForPageSpeed 方法
  *
  * @param Object a Map對象
  */
    PageSpeed.submitDataForPageSpeed = function(a) {
        var b = {
            'name': '',
            'rate': 0
        };
        b.name = a.name;
        b.rate = a.rate;

  /* a.PAGE_SPEED_TIME數組的最後一個元素減去第一個元素 */
        b['1'] = a.PAGE_SPEED_TIME[a.PAGE_SPEED_TIME.length - 1] - a.PAGE_SPEED_TIME[0];

        for (var i = 1; i < a.PAGE_SPEED_TIME.length - 1; i++) {
            b[(i + 1) + ''] = a.PAGE_SPEED_TIME[i] - a.PAGE_SPEED_TIME[0]
        }

  /* 提交速度測試結果的目的指令碼 */
        var c = 'http://pagespeed-ied.qq.com/r.cgi';

  /* 遍曆 b 數組的元素, 構建 query string */
        var d = [];
        for (var p in b) {
            d.push(p + '=' + b[p])
        }
        c += '?' + d.join('&');

  /* 建立script標籤提交測速結果 */
        PageSpeed.createScript(c, 'submitForPageSpeed')
    };

 /**
  * 綁定 defaultSubmit 方法
  *
  */
    PageSpeed.defaultSubmit = function() {
        var a = '';
        try {
            a = location.host
        } catch(e) {}

        var b = {
            'name': a,
            'rate': 1,
            'PAGE_SPEED_TIME': []
        };

        if (!b.name) {
            return
        }

        if (! (typeof(PAGE_SPEED_TIME) == 'object' && PAGE_SPEED_TIME instanceof Array)) {
            return
        }

  /* 擷取新的時間戳記 */
        PAGE_SPEED_TIME.push(new Date().getTime());

        b.PAGE_SPEED_TIME = PAGE_SPEED_TIME;

  /*
   * 沒看懂這裡為啥需要一個新的變數 aaa, 直接使用 a 不可以麼?
   * try ... catch 結構中使用的卻是一個新的變數 aaaa 但後面卻從未使用, 為何? 是否應該是 aaa?
   */
        var aaa = '';
        try {
            aaaa = location.host
        } catch(e) {}

        var c = Math.floor(Math.random() * 10000);

        if (aaa == "ktv.qq.com" || aaa == "ttd.qq.com" || aaa == "tian.qq.com" || aaa == "sura.qq.com" || aaa == "gw.tnt.qq.com" || aaa == "007.qq.com") {
            c = Math.floor(Math.random() * 1000);
        }

  /* 隨機機率提交資料 */
        if (c <= b.rate * 1) {
            PageSpeed.submitDataForPageSpeed(b);
        }
    };

 /**
  * 綁定submit方法(用於CDN測速?)
  *
  * @param String a 名稱
  */
    PageSpeed.submit = function(a) {
        var b = PageSpeed.cdn_page_speed_submitData;
        var c = {};
        for (var p in b) {
            c[p] = b[p]
        }
        c.name = a || c.name;
        PageSpeed.submitDataForPageSpeed(c);
    }
})();

try {
    /* 1秒後嘗試提交資料 */
    setTimeout(function() {
        PageSpeed.defaultSubmit()
    },
    1000);
} catch(e) {}

/* 最後這段注釋, 第二段是32位16進位數字, 應該是類似於 ETag 用於標記內容版本的 */
/*  |xGv00|ca82276cd78ac911d3d4310ba1408236 */

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.