logger.es6 2.6 KB
Newer Older
Y
yangmingming 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
/**
 * file tools/logger logger工具
 * author saniac(snailsword@gmail.com)
 */

export default class Logger {
    constructor() {
        this.timeTable = {};
        this.countTable = {};
        this.duringTable = {};
        this.lastStopTable = {};
    }

    start(key) {
        let arr = this.timeTable[key];
        if (!arr) {
            arr = [{}];
        }
        else {
            if (!arr[arr.length - 1].endTime) {
                console.error('[logger] key:' + key + ' duplicate start logger');
                return;
            }
            arr.push({});
        }
        arr[arr.length - 1].startTime = this.time;
        this.timeTable[key] = arr;
        return this;
    }

    end(key) {
        // console.log(this.timeTable[key]);
        if (!this.timeTable[key]) {
            console.log(this.timeTable[key]);
            console.error('[logger] key:' + key + ' no matching start logger');
            return;
        }
        let currentObj = this.timeTable[key][this.timeTable[key].length - 1];
        if (currentObj.endTime) {
            console.error('[logger] key:' + key + ' duplicate end logger');
            return;
        }
        currentObj.endTime = this.time;
        currentObj.during = currentObj.endTime - currentObj.startTime;
        return this;
    }

    // 数次数
    count(key) {
        if (this.countTable[key]) {
            this.countTable[key]++;
        }
        else {
            this.countTable[key] = 1;
        }
        return this;
    }

    // 看每次执行的时间间隔
    during(key) {
        if (this.lastStopTable[key]) {
            this.duringTable[key].push(this.time - this.lastStopTable[key]);
            this.lastStopTable[key] = this.time;
        }
        else {
            this.lastStopTable[key] = this.time;
            this.duringTable[key] = [];
        }
        return this;
    }

    get time() {
        return +new Date().getTime();
    }
    get statistics() {
        // time
        let timeResult = [];
        let item;
        for (let key in this.timeTable) {
            item = this.timeTable[key];
            let len = item.length;
            let max = 0;
            let min = Number.MAX_VALUE;
            let sum = 0;
            for (let i = 0; i < len; i++) {
                max = Math.max(max, item[i].during);
                min = Math.min(min, item[i].during);
                sum += item[i].during;
            }
            timeResult.push({
                name: key,
                length: len,
                avg: sum / len,
                max,
                min
            });
        }
        console.table(timeResult);
        return {timeResult};
    }
}