Files
cocos4/cocos/core/utils/profiler/counter.ts
T
Yun Hsiao Wu 1146b322a3 update effects (#5722)
* update effects

* fix profiler aspect
2019-11-13 15:45:19 +08:00

70 lines
1.7 KiB
TypeScript

export interface ICounterOption {
desc: string;
counter: Counter;
min?: number;
max?: number;
average?: number;
below?: number;
over?: number;
color?: string;
isInteger?: boolean;
}
export class Counter {
get value () {
return this._value;
}
set value (val) {
this._value = val;
}
protected _id: string;
protected _opts: ICounterOption;
protected _accumStart: number;
protected _total = 0;
protected _value = 0;
protected _averageValue = 0;
protected _accumValue = 0;
protected _accumSamples = 0;
constructor (id: string, opts: ICounterOption, now: number) {
this._id = id;
this._opts = opts;
this._accumStart = now;
}
public sample (now: number) {
this._average(this._value, now);
}
public human () {
const { average, isInteger } = this._opts;
const v = average ? this._averageValue : this._value;
return isInteger ? Math.round(v) : Math.round(v * 100) / 100;
}
public alarm () {
return (
(this._opts.below && this._value < this._opts.below) ||
(this._opts.over && this._value > this._opts.over)
);
}
protected _average (v: number, now: number = 0) {
if (this._opts.average) {
this._accumValue += v;
++this._accumSamples;
const t = now;
if (t - this._accumStart >= this._opts.average) {
this._averageValue = this._accumValue / this._accumSamples;
this._accumValue = 0;
this._accumStart = t;
this._accumSamples = 0;
}
}
}
}