This repository has been archived by the owner on Aug 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
metrics.ts
208 lines (203 loc) · 5.85 KB
/
metrics.ts
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* Copyright 2014 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
module J2ME.Metrics {
export class Timer {
private static _base: Timer = new Timer(null, "Total");
private static _top = Timer._base;
private static _flat = new Timer(null, "Flat");
private static _flatStack = [];
private _parent: Timer;
private _name: string;
private _begin: number;
private _last: number;
private _total: number;
private _count: number;
private _timers: Map<string, Timer>;
constructor(parent: Timer, name: string) {
this._parent = parent;
this._timers = ObjectUtilities.createMap<string, Timer>();
this._name = name;
this._begin = 0;
this._last = 0;
this._total = 0;
this._count = 0;
}
public static time(name, fn: Function) {
Timer.start(name);
fn();
Timer.stop();
}
public static start(name) {
Timer._top = Timer._top._timers[name] || (Timer._top._timers[name] = new Timer(Timer._top, name));
Timer._top.start();
var tmp = Timer._flat._timers[name] || (Timer._flat._timers[name] = new Timer(Timer._flat, name));
tmp.start();
Timer._flatStack.push(tmp);
}
public static stop() {
Timer._top.stop();
Timer._top = Timer._top._parent;
Timer._flatStack.pop().stop();
}
public static stopStart(name) {
Timer.stop();
Timer.start(name);
}
public start() {
this._begin = getTicks();
}
public stop() {
this._last = getTicks() - this._begin;
this._total += this._last;
this._count += 1;
}
public toJSON() {
return {name: this._name, total: this._total, timers: this._timers};
}
public trace(writer: IndentingWriter) {
writer.enter (
this._name + ": " + this._total.toFixed(2) + " ms" +
", count: " + this._count +
", average: " + (this._total / this._count).toFixed(2) + " ms"
);
for (var name in this._timers) {
this._timers[name].trace(writer);
}
writer.outdent();
}
public static trace(writer: IndentingWriter) {
Timer._base.trace(writer);
Timer._flat.trace(writer);
}
}
/**
* Quick way to count named events.
*/
export class Counter {
public static instance: Counter = new Counter(true);
private _enabled: boolean;
private _counts: Map<string, number>;
private _times: Map<string, number>;
get counts(): Map<string, number> {
return this._counts;
}
constructor(enabled: boolean) {
this._enabled = enabled;
this.clear();
}
public setEnabled(enabled:boolean) {
this._enabled = enabled;
}
public clear() {
this._counts = ObjectUtilities.createMap<string, number>();
this._times = ObjectUtilities.createMap<string, number>();
}
public toJSON() {
return {
counts: this._counts,
times: this._times
};
}
public count(name: string, increment: number = 1, time: number = 0) {
if (!this._enabled) {
return;
}
if (this._counts[name] === undefined) {
this._counts[name] = 0;
this._times[name] = 0;
}
this._counts[name] += increment;
this._times[name] += time;
return this._counts[name];
}
public trace(writer: IndentingWriter) {
for (var name in this._counts) {
writer.writeLn(name + ": " + this._counts[name]);
}
}
private _pairToString(times, pair): string {
var name = pair[0];
var count = pair[1];
var time = times[name];
var line = count + ": " + name;
if (time) {
line += ", " + time.toFixed(4);
if (count > 1) {
line += " (" + (time / count).toFixed(4) + ")";
}
}
return line;
}
public toStringSorted(): string {
var self = this;
var times = this._times;
var pairs = [];
for (var name in this._counts) {
pairs.push([name, this._counts[name]]);
}
pairs.sort(function (a, b) {
return b[1] - a[1];
});
return (pairs.map(function (pair) {
return self._pairToString(times, pair);
}).join(", "));
}
public traceSorted(writer: IndentingWriter, inline = false) {
var self = this;
var times = this._times;
var pairs = [];
for (var name in this._counts) {
pairs.push([name, this._counts[name]]);
}
pairs.sort(function (a, b) {
return b[1] - a[1];
});
if (inline) {
writer.writeLn(pairs.map(function (pair) {
return self._pairToString(times, pair);
}).join(", "));
} else {
pairs.forEach(function (pair) {
writer.writeLn(self._pairToString(times, pair));
});
}
}
}
export class Average {
private _samples: Float64Array;
private _count: number;
private _index: number;
constructor(max) {
this._samples = new Float64Array(max);
this._count = 0;
this._index = 0;
}
public push(sample: number) {
if (this._count < this._samples.length) {
this._count ++;
}
this._index ++;
this._samples[this._index % this._samples.length] = sample;
}
public average(): number {
var sum = 0;
for (var i = 0; i < this._count; i++) {
sum += this._samples[i];
}
return sum / this._count;
}
}
}