forked from catapult-project/catapult
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeline_data_series.js
366 lines (313 loc) · 10.4 KB
/
timeline_data_series.js
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* Different data types that each require their own labelled axis.
*/
var TimelineDataType = {SOURCE_COUNT: 0, BYTES_PER_SECOND: 1};
/**
* A TimelineDataSeries collects an ordered series of (time, value) pairs,
* and converts them to graph points. It also keeps track of its color and
* current visibility state. DataSeries are solely responsible for tracking
* data, and do not send notifications on state changes.
*
* Abstract class, doesn't implement onReceivedLogEntry.
*/
var TimelineDataSeries = (function() {
'use strict';
/**
* @constructor
*/
function TimelineDataSeries(dataType) {
// List of DataPoints in chronological order.
this.dataPoints_ = [];
// Data type of the DataSeries. This is used to scale all values with
// the same units in the same way.
this.dataType_ = dataType;
// Default color. Should always be overridden prior to display.
this.color_ = 'red';
// Whether or not the data series should be drawn.
this.isVisible_ = false;
this.cacheStartTime_ = null;
this.cacheStepSize_ = 0;
this.cacheValues_ = [];
}
TimelineDataSeries.prototype = {
/**
* Adds a DataPoint to |this| with the specified time and value.
* DataPoints are assumed to be received in chronological order.
*/
addPoint: function(timeTicks, value) {
var time = timeutil.convertTimeTicksToDate(timeTicks).getTime();
this.dataPoints_.push(new DataPoint(time, value));
},
isVisible: function() {
return this.isVisible_;
},
show: function(isVisible) {
this.isVisible_ = isVisible;
},
getColor: function() {
return this.color_;
},
setColor: function(color) {
this.color_ = color;
},
getDataType: function() {
return this.dataType_;
},
/**
* Returns a list containing the values of the data series at |count|
* points, starting at |startTime|, and |stepSize| milliseconds apart.
* Caches values, so showing/hiding individual data series is fast, and
* derived data series can be efficiently computed, if we add any.
*/
getValues: function(startTime, stepSize, count) {
// Use cached values, if we can.
if (this.cacheStartTime_ == startTime &&
this.cacheStepSize_ == stepSize &&
this.cacheValues_.length == count) {
return this.cacheValues_;
}
// Do all the work.
this.cacheValues_ = this.getValuesInternal_(startTime, stepSize, count);
this.cacheStartTime_ = startTime;
this.cacheStepSize_ = stepSize;
return this.cacheValues_;
},
/**
* Does all the work of getValues when we can't use cached data.
*
* The default implementation just uses the |value| of the most recently
* seen DataPoint before each time, but other DataSeries may use some
* form of interpolation.
* TODO(mmenke): Consider returning the maximum value over each interval
* to create graphs more stable with respect to zooming.
*/
getValuesInternal_: function(startTime, stepSize, count) {
var values = [];
var nextPoint = 0;
var currentValue = 0;
var time = startTime;
for (var i = 0; i < count; ++i) {
while (nextPoint < this.dataPoints_.length &&
this.dataPoints_[nextPoint].time < time) {
currentValue = this.dataPoints_[nextPoint].value;
++nextPoint;
}
values[i] = currentValue;
time += stepSize;
}
return values;
}
};
/**
* A single point in a data series. Each point has a time, in the form of
* milliseconds since the Unix epoch, and a numeric value.
* @constructor
*/
function DataPoint(time, value) {
this.time = time;
this.value = value;
}
return TimelineDataSeries;
})();
/**
* Tracks how many sources of the given type have seen a begin
* event of type |eventType| more recently than an end event.
*/
var SourceCountDataSeries = (function() {
'use strict';
var superClass = TimelineDataSeries;
/**
* @constructor
*/
function SourceCountDataSeries(sourceType, eventType) {
superClass.call(this, TimelineDataType.SOURCE_COUNT);
this.sourceType_ = sourceType;
this.eventType_ = eventType;
// Map of sources for which we've seen a begin event more recently than an
// end event. Each such source has a value of "true". All others are
// undefined.
this.activeSources_ = {};
// Number of entries in |activeSources_|.
this.activeCount_ = 0;
}
SourceCountDataSeries.prototype = {
// Inherit the superclass's methods.
__proto__: superClass.prototype,
onReceivedLogEntry: function(entry) {
if (entry.source.type != this.sourceType_ ||
entry.type != this.eventType_) {
return;
}
if (entry.phase == EventPhase.PHASE_BEGIN) {
this.onBeginEvent(entry.source.id, entry.time);
return;
}
if (entry.phase == EventPhase.PHASE_END)
this.onEndEvent(entry.source.id, entry.time);
},
/**
* Called when the source with the specified id begins doing whatever we
* care about. If it's not already an active source, we add it to the map
* and add a data point.
*/
onBeginEvent: function(id, time) {
if (this.activeSources_[id])
return;
this.activeSources_[id] = true;
++this.activeCount_;
this.addPoint(time, this.activeCount_);
},
/**
* Called when the source with the specified id stops doing whatever we
* care about. If it's an active source, we remove it from the map and add
* a data point.
*/
onEndEvent: function(id, time) {
if (!this.activeSources_[id])
return;
delete this.activeSources_[id];
--this.activeCount_;
this.addPoint(time, this.activeCount_);
}
};
return SourceCountDataSeries;
})();
/**
* Tracks the number of sockets currently in use. Needs special handling of
* SSL sockets, so can't just use a normal SourceCountDataSeries.
*/
var SocketsInUseDataSeries = (function() {
'use strict';
var superClass = SourceCountDataSeries;
/**
* @constructor
*/
function SocketsInUseDataSeries() {
superClass.call(this, EventSourceType.SOCKET, EventType.SOCKET_IN_USE);
}
SocketsInUseDataSeries.prototype = {
// Inherit the superclass's methods.
__proto__: superClass.prototype,
onReceivedLogEntry: function(entry) {
// SSL sockets have two nested SOCKET_IN_USE events. This is needed to
// mark SSL sockets as unused after SSL negotiation.
if (entry.type == EventType.SSL_CONNECT &&
entry.phase == EventPhase.PHASE_END) {
this.onEndEvent(entry.source.id, entry.time);
return;
}
superClass.prototype.onReceivedLogEntry.call(this, entry);
}
};
return SocketsInUseDataSeries;
})();
/**
* Tracks approximate data rate using individual data transfer events.
* Abstract class, doesn't implement onReceivedLogEntry.
*/
var TransferRateDataSeries = (function() {
'use strict';
var superClass = TimelineDataSeries;
/**
* @constructor
*/
function TransferRateDataSeries() {
superClass.call(this, TimelineDataType.BYTES_PER_SECOND);
}
TransferRateDataSeries.prototype = {
// Inherit the superclass's methods.
__proto__: superClass.prototype,
/**
* Returns the average data rate over each interval, only taking into
* account transfers that occurred within each interval.
* TODO(mmenke): Do something better.
*/
getValuesInternal_: function(startTime, stepSize, count) {
// Find the first DataPoint after |startTime| - |stepSize|.
var nextPoint = 0;
while (nextPoint < this.dataPoints_.length &&
this.dataPoints_[nextPoint].time < startTime - stepSize) {
++nextPoint;
}
var values = [];
var time = startTime;
for (var i = 0; i < count; ++i) {
// Calculate total bytes transferred from |time| - |stepSize|
// to |time|. We look at the transfers before |time| to give
// us generally non-varying values for a given time.
var transferred = 0;
while (nextPoint < this.dataPoints_.length &&
this.dataPoints_[nextPoint].time < time) {
transferred += this.dataPoints_[nextPoint].value;
++nextPoint;
}
// Calculate bytes per second.
values[i] = 1000 * transferred / stepSize;
time += stepSize;
}
return values;
}
};
return TransferRateDataSeries;
})();
/**
* Tracks TCP and UDP transfer rate.
*/
var NetworkTransferRateDataSeries = (function() {
'use strict';
var superClass = TransferRateDataSeries;
/**
* |tcpEvent| and |udpEvent| are the event types for data transfers using
* TCP and UDP, respectively.
* @constructor
*/
function NetworkTransferRateDataSeries(tcpEvent, udpEvent) {
superClass.call(this);
this.tcpEvent_ = tcpEvent;
this.udpEvent_ = udpEvent;
}
NetworkTransferRateDataSeries.prototype = {
// Inherit the superclass's methods.
__proto__: superClass.prototype,
onReceivedLogEntry: function(entry) {
if (entry.type != this.tcpEvent_ && entry.type != this.udpEvent_)
return;
this.addPoint(entry.time, entry.params.byte_count);
},
};
return NetworkTransferRateDataSeries;
})();
/**
* Tracks disk cache read or write rate. Doesn't include clearing, opening,
* or dooming entries, as they don't have clear size values.
*/
var DiskCacheTransferRateDataSeries = (function() {
'use strict';
var superClass = TransferRateDataSeries;
/**
* @constructor
*/
function DiskCacheTransferRateDataSeries(eventType) {
superClass.call(this);
this.eventType_ = eventType;
}
DiskCacheTransferRateDataSeries.prototype = {
// Inherit the superclass's methods.
__proto__: superClass.prototype,
onReceivedLogEntry: function(entry) {
if (entry.source.type != EventSourceType.DISK_CACHE_ENTRY ||
entry.type != this.eventType_ ||
entry.phase != EventPhase.PHASE_END) {
return;
}
// The disk cache has a lot of 0-length writes, when truncating entries.
// Ignore those.
if (entry.params.bytes_copied != 0)
this.addPoint(entry.time, entry.params.bytes_copied);
}
};
return DiskCacheTransferRateDataSeries;
})();