@probe.gl/stats
Version:
Stats object for reporting performance statistics
8 lines (7 loc) • 10.3 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../src/index.ts", "../src/utils/hi-res-timestamp.ts", "../src/lib/stat.ts", "../src/lib/stats.ts"],
"sourcesContent": ["export {default as Stats} from './lib/stats';\nexport {default as Stat} from './lib/stat';\n\n// UTILITIES\nexport {default as _getHiResTimestamp} from './utils/hi-res-timestamp';\n", "// Copyright (c) 2017 Uber Technologies, Inc.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n\nexport default function getHiResTimestamp(): number {\n let timestamp;\n // Get best timer available.\n if (typeof window !== 'undefined' && window.performance) {\n timestamp = window.performance.now();\n } else if (typeof process !== 'undefined' && process.hrtime) {\n const timeParts = process.hrtime();\n timestamp = timeParts[0] * 1000 + timeParts[1] / 1e6;\n } else {\n timestamp = Date.now();\n }\n\n return timestamp;\n}\n", "import getHiResTimestamp from '../utils/hi-res-timestamp';\n\nexport default class Stat {\n readonly name: string;\n readonly type: string | undefined;\n sampleSize: number = 1;\n time: number = 0;\n count: number = 0;\n samples: number = 0;\n lastTiming: number = 0;\n lastSampleTime: number = 0;\n lastSampleCount: number = 0;\n\n _count: number = 0;\n _time: number = 0;\n _samples: number = 0;\n _startTime: number = 0;\n _timerPending: boolean = false;\n\n constructor(name: string, type?: string) {\n this.name = name;\n this.type = type;\n this.reset();\n }\n\n reset(): this {\n this.time = 0;\n this.count = 0;\n this.samples = 0;\n this.lastTiming = 0;\n this.lastSampleTime = 0;\n this.lastSampleCount = 0;\n this._count = 0;\n this._time = 0;\n this._samples = 0;\n this._startTime = 0;\n this._timerPending = false;\n\n return this;\n }\n\n setSampleSize(samples: number): this {\n this.sampleSize = samples;\n return this;\n }\n\n /** Call to increment count (+1) */\n incrementCount(): this {\n this.addCount(1);\n\n return this;\n }\n\n /** Call to decrement count (-1) */\n decrementCount(): this {\n this.subtractCount(1);\n\n return this;\n }\n\n /** Increase count */\n addCount(value: number): this {\n this._count += value;\n this._samples++;\n this._checkSampling();\n\n return this;\n }\n\n /** Decrease count */\n subtractCount(value: number): this {\n this._count -= value;\n this._samples++;\n this._checkSampling();\n\n return this;\n }\n\n /** Add an arbitrary timing and bump the count */\n addTime(time: number): this {\n this._time += time;\n this.lastTiming = time;\n this._samples++;\n this._checkSampling();\n\n return this;\n }\n\n /** Start a timer */\n timeStart(): this {\n this._startTime = getHiResTimestamp();\n this._timerPending = true;\n\n return this;\n }\n\n /** End a timer. Adds to time and bumps the timing count. */\n timeEnd(): this {\n if (!this._timerPending) {\n return this;\n }\n this.addTime(getHiResTimestamp() - this._startTime);\n this._timerPending = false;\n this._checkSampling();\n\n return this;\n }\n\n getSampleAverageCount(): number {\n return this.sampleSize > 0 ? this.lastSampleCount / this.sampleSize : 0;\n }\n\n /** Calculate average time / count for the previous window */\n getSampleAverageTime(): number {\n return this.sampleSize > 0 ? this.lastSampleTime / this.sampleSize : 0;\n }\n\n /** Calculate counts per second for the previous window */\n getSampleHz(): number {\n return this.lastSampleTime > 0 ? this.sampleSize / (this.lastSampleTime / 1000) : 0;\n }\n\n getAverageCount(): number {\n return this.samples > 0 ? this.count / this.samples : 0;\n }\n\n /** Calculate average time / count */\n getAverageTime(): number {\n return this.samples > 0 ? this.time / this.samples : 0;\n }\n\n /** Calculate counts per second */\n getHz(): number {\n return this.time > 0 ? this.samples / (this.time / 1000) : 0;\n }\n\n _checkSampling(): void {\n if (this._samples === this.sampleSize) {\n this.lastSampleTime = this._time;\n this.lastSampleCount = this._count;\n this.count += this._count;\n this.time += this._time;\n this.samples += this._samples;\n this._time = 0;\n this._count = 0;\n this._samples = 0;\n }\n }\n}\n", "// probe.gl, MIT license\n\nimport Stat from './stat';\n\ntype TableEntry = {\n time: number;\n count: number;\n average: number;\n hz: number;\n};\n\n/** A \"bag\" of `Stat` objects, can be visualized using `StatsWidget` */\nexport default class Stats {\n readonly id: string;\n readonly stats: Record<string, Stat> = {};\n\n constructor(options: {id: string; stats?: Stats | Stat[] | {name: string; type?: string}[]}) {\n this.id = options.id;\n this.stats = {};\n\n this._initializeStats(options.stats);\n\n Object.seal(this);\n }\n\n /** Acquire a stat. Create if it doesn't exist. */\n get(name: string, type: string = 'count'): Stat {\n return this._getOrCreate({name, type});\n }\n\n get size(): number {\n return Object.keys(this.stats).length;\n }\n\n /** Reset all stats */\n reset(): this {\n for (const stat of Object.values(this.stats)) {\n stat.reset();\n }\n\n return this;\n }\n\n forEach(fn: (stat: Stat) => void): void {\n for (const stat of Object.values(this.stats)) {\n fn(stat);\n }\n }\n\n getTable(): Record<string, TableEntry> {\n const table: Record<string, TableEntry> = {};\n this.forEach(stat => {\n table[stat.name] = {\n time: stat.time || 0,\n count: stat.count || 0,\n average: stat.getAverageTime() || 0,\n hz: stat.getHz() || 0\n };\n });\n\n return table;\n }\n\n _initializeStats(stats: Stats | Stat[] | {name: string; type?: string}[] = []): void {\n stats.forEach(stat => this._getOrCreate(stat));\n }\n\n _getOrCreate(stat: Stat | {name: string, type?: string}): Stat {\n const {name, type} = stat;\n let result = this.stats[name];\n if (!result) {\n if (stat instanceof Stat) {\n result = stat;\n } else {\n result = new Stat(name, type);\n }\n this.stats[name] = result;\n }\n return result;\n }\n}\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;ACoBc,SAAP,oBAAkC;AACvC,MAAI;AAEJ,MAAI,OAAO,WAAW,eAAe,OAAO,aAAa;AACvD,gBAAY,OAAO,YAAY,IAAG;EACpC,WAAW,OAAO,YAAY,eAAe,QAAQ,QAAQ;AAC3D,UAAM,YAAY,QAAQ,OAAM;AAChC,gBAAY,UAAU,CAAC,IAAI,MAAO,UAAU,CAAC,IAAI;EACnD,OAAO;AACL,gBAAY,KAAK,IAAG;EACtB;AAEA,SAAO;AACT;;;AC/BA,IAAqB,OAArB,MAAyB;EAiBvB,YAAY,MAAc,MAAa;AAdvC,SAAA,aAAqB;AACrB,SAAA,OAAe;AACf,SAAA,QAAgB;AAChB,SAAA,UAAkB;AAClB,SAAA,aAAqB;AACrB,SAAA,iBAAyB;AACzB,SAAA,kBAA0B;AAE1B,SAAA,SAAiB;AACjB,SAAA,QAAgB;AAChB,SAAA,WAAmB;AACnB,SAAA,aAAqB;AACrB,SAAA,gBAAyB;AAGvB,SAAK,OAAO;AACZ,SAAK,OAAO;AACZ,SAAK,MAAK;EACZ;EAEA,QAAK;AACH,SAAK,OAAO;AACZ,SAAK,QAAQ;AACb,SAAK,UAAU;AACf,SAAK,aAAa;AAClB,SAAK,iBAAiB;AACtB,SAAK,kBAAkB;AACvB,SAAK,SAAS;AACd,SAAK,QAAQ;AACb,SAAK,WAAW;AAChB,SAAK,aAAa;AAClB,SAAK,gBAAgB;AAErB,WAAO;EACT;EAEA,cAAc,SAAe;AAC3B,SAAK,aAAa;AAClB,WAAO;EACT;;EAGA,iBAAc;AACZ,SAAK,SAAS,CAAC;AAEf,WAAO;EACT;;EAGA,iBAAc;AACZ,SAAK,cAAc,CAAC;AAEpB,WAAO;EACT;;EAGA,SAAS,OAAa;AACpB,S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