/**
 * @file performance.js
 * @author Pride Leong(liangjinping@baidu.com)
 */

define(function (require, exports, module) {

    var _ = require('underscore');
    var logger = require('./logger');
    var config = require('./config');
    var timeline = require('./performance/timeline');
    var memory = require('fc-storage/memory');

    var prefix = config.performanceTargetPrefix;

    var itemMap = {};

    /**
     * 计算性能
     * @param {string} itemKey 监控项名
     * @return {Object} exports 返回模块自身，供链式调用
     */
    exports.measure = function (itemKey) {
        if ('string' !== typeof itemKey) {
            return;
        }
        var item = itemMap[itemKey];
        if (!item || item.process.length === 0) {
            return;
        }
        var markNames = _.map(item.process, function (process) {
            return prefix + itemKey + '_' + process;
        });
        var performanceData = {};
        var measureList = _.chain(timeline.measure(
            [config.firstMark].concat(markNames)
        )).each(function (measure) {
            performanceData[measure.endMark] = measure.duration;
            // clear mark
            if (measure.endMark !== config.firstMark) {
                window.performance.clearMarks(measure.endMark);
            }
        }).value();
        performanceData[prefix + itemKey] = _.max(performanceData);

        var statis = {};
        statis[itemKey] = {
            measure: measureList,
            data: performanceData
        };
        memory.updateItem(config.storageKey, {
            performance: statis
        });

        // clear cached mark info
        delete itemMap[itemKey];

        // send performance log
        logger.log(
            _.extend({performanceId: config.performanceId}, performanceData),
            prefix + itemKey
        );
        return exports;
    };

    /**
     * 性能埋点
     * @param {string} itemKey 监控项名
     * @param {string} process 进度名
     *  为finish时表示该流程结束，开始计算性能
     * @return {Object} exports 返回模块自身，供链式调用
     */
    exports.mark = function (itemKey, process) {
        if ('string' !== typeof itemKey) {
            return;
        }
        itemMap[itemKey] = itemMap[itemKey] || {};
        var item = itemMap[itemKey];
        item.process = item.process || [];

        if ('string' !== typeof process
            || '' === process) {
            process = 'process';
        }
        process += '_' + item.process.length;
        item.process.push(process);
        var markName = prefix + itemKey + '_' + process;
        window.performance.mark(markName);
        return exports;
    };

    exports.init = function () {
    };

    return module.exports = exports;
});

@@ -226,24 +226,66 @@
 ./config');%0A
+    var recorder = require('./recorder');%0A
     var time
@@ -1774,46 +1774,175 @@
 end(
-%7BperformanceId: config.performanceId%7D,
+%0A                %7B%0A                    performanceId: config.performanceId,%0A                    pageStabled: recorder.stable ? 1 : 0%0A                %7D,%0A               
  per
@@ -1953,16 +1953,29 @@
 anceData
+%0A            
 ),%0A     

add field `pageStable` in performance log