forked from tgandrews/tal-performance-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
talperformancemonitor.js
174 lines (157 loc) · 5.08 KB
/
talperformancemonitor.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
(function (window, document, undefined) {
var startTime = new Date();
var config = {
server : '10.10.14.27:3000'
};
var utils = {
sendStatistic: function (statName, statValue) {
var appVersion = window.antie.framework.applicationVersion;
var unixTime = Math.round(new Date().getTime() / 1000);
var url = 'http://' + config.server + '?' + statName + '=' + statValue + '&date=' + unixTime + '&appversion=' + appVersion;
var body = document.getElementsByTagName('body')[0];
var statsCallScript = document.createElement('script');
statsCallScript.type = 'text/javascript';
statsCallScript.src = url;
body.appendChild(statsCallScript);
// console.log('Sent: ' + statName + ' ' + statValue);
},
timeFromStart: function () {
return new Date() - startTime;
},
formatId: function (id) {
return id.replace(/[ -]/, '_');
}
};
var frameCount = 0;
var talObjectModifications = {
'antie/application': function (object) {
var originalReady = object.prototype.ready;
object.prototype.ready = function () {
originalReady.apply(this, arguments);
var timeElapsed = utils.timeFromStart();
utils.sendStatistic('applicationstart', timeElapsed);
};
},
'antie/devices/browserdevice': function (object) {
var originalCreateElement = object.prototype._createElement;
object.prototype._createElement = function (tagName) {
var element = originalCreateElement.apply(this, arguments);
if (tagName === 'video') {
var loadstartTime;
element.addEventListener('loadstart', function () {
loadstartTime = new Date();
});
element.addEventListener('canplay', function () {
var timeElapsed = new Date() - loadstartTime;
utils.sendStatistic('canplay', timeElapsed);
});
}
return element;
};
},
'antie/widgets/carousel/binder': function (object) {
var original = object.prototype._getCallbacks;
object.prototype._getCallbacks = function (widget, processItemFn, postBindFn) {
var callbacks = original.call(this, widget, processItemFn, postBindFn);
var originalOnSuccess = callbacks.onSuccess;
callbacks.onSuccess = function (data) {
var start = new Date();
originalOnSuccess(data);
var forceUpdate = window.getComputedStyle(widget.outputElement, null).width;
var end = new Date();
utils.sendStatistic('bind_success_time_for_' + utils.formatId(widget.id), end - start);
};
return callbacks;
};
},
'bigscreen/antietemp/widgets/carousel/binder': function (object) {
this['antie/widgets/carousel/binder'](object);
},
'antie/lib/tween': function (TWEEN) {
var originalUpdate = TWEEN.update;
TWEEN.update = function (time) {
frameCount += 1;
originalUpdate(time);
}
var OriginalTween = TWEEN.Tween;
TWEEN.Tween = function (object) {
var tweenInstance = new OriginalTween(object);
tweenInstance.startCount = frameCount;
var onCompleteAssigner = tweenInstance.onComplete;
tweenInstance.onComplete = function (callback) {
var wrappedCallback = function () {
callback(tweenInstance)
}
onCompleteAssigner.call(tweenInstance, wrappedCallback);
}
return tweenInstance;
}
},
'antie/devices/anim/tween': function (object, dependencies) {
var Device = dependencies[0];
var originalDeviceTween = Device.prototype._tween;
Device.prototype._tween = function (options) {
var originalOnComplete = options.onComplete;
var id = options.el.id;
var duration = options.duration;
options.onComplete = function (tween) {
if (originalOnComplete) {
originalOnComplete();
}
if (duration > 0) {
var fps = (frameCount - tween.startCount) / (duration / 1000)
utils.sendStatistic('elementAnimationFPS_' + utils.formatId(id), fps)
}
}
originalDeviceTween.call(this, options)
}
}
};
var statEvents = {
registerCallbacksForStatistics: function () {
this.windowOnLoad();
this.requireReady();
this.interceptRequire();
},
windowOnLoad: function () {
window.onload = function () {
var onloadTime = utils.timeFromStart();
utils.sendStatistic('onload', onloadTime);
};
},
requireReady: function () {
var originalMethod = require.ready;
require.ready = function (callback) {
var self = this;
var newCallback = function () {
var timeElapsed = utils.timeFromStart();
utils.sendStatistic('requireready', timeElapsed);
callback.call(self);
};
originalMethod.call(this, newCallback);
}
},
interceptRequire: function () {
var originalMethod = require.execCb;
require.execCb = function (name, method, args) {
var object = originalMethod.apply(this, arguments);
for (var prop in talObjectModifications) {
if (talObjectModifications.hasOwnProperty(prop)) {
if (name === prop) {
talObjectModifications[prop](object, args);
}
}
}
return object;
};
}
};
var tpm = function (userConfig) {
if (userConfig && userConfig.server) {
config.server = userConfig.server
}
statEvents.registerCallbacksForStatistics();
}
window.tpm = tpm;
})(window, document);
window.tpm();