Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit ad41788

Browse files
committed
Adding performance measurement infrastructure.
Added measurements for getting and parsing the UnityLoader and the scene load.
1 parent a285022 commit ad41788

File tree

3 files changed

+144
-2
lines changed

3 files changed

+144
-2
lines changed

Assets/WebGLTemplates/WebVR/index.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@
1515
<script>
1616
MozillaResearch.telemetry.start({
1717
analytics: true,
18-
errorLogging: true
18+
errorLogging: true,
19+
performance: true
1920
});
21+
MozillaResearch.telemetry.performance.mark('LoaderParsingStart');
2022
</script>
2123
<script src="%UNITY_WEBGL_LOADER_URL%"></script>
2224
<script>
23-
/* global UnityLoader */
25+
/* global UnityLoader, MozillaResearch */
26+
MozillaResearch.telemetry.performance.measure('LoaderParsing', 'LoaderParsingStart');
2427
(function () {
2528
UnityLoader.SystemInfo.mobile = false; // Workaround to force `UnityLoader` to actually load on mobile.
29+
MozillaResearch.telemetry.performance.mark('LoadingStart');
2630
window.gameInstance = UnityLoader.instantiate('gameContainer', '%UNITY_WEBGL_BUILD_URL%', {
2731
Module: {
2832
// `preserveDrawingBuffer` is needed for WebVR content to be mirrored to the `<canvas>`.
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/* global localStorage, location, Raven */
2+
(function (window) {
3+
'use strict';
4+
5+
if (!('MozillaResearch' in window)) {
6+
window.MozillaResearch = {};
7+
}
8+
9+
if (!('telemetry' in window.MozillaResearch)) {
10+
window.MozillaResearch.telemetry = {};
11+
}
12+
13+
var navigator = window.navigator;
14+
var telemetry = window.MozillaResearch.telemetry;
15+
16+
telemetry.ga = {
17+
create: function (trackingId, cookieDomain, name, fieldsObject) {
18+
window.ga('create', trackingId, cookieDomain, name, fieldsObject);
19+
return function (command) {
20+
if (navigator.doNotTrack === '1') { return; }
21+
var args = Array.prototype.slice.call(arguments);
22+
if (name && command !== 'provide') {
23+
command = name + '.' + command;
24+
args[0] = command;
25+
}
26+
window.ga.apply(undefined, args);
27+
};
28+
}
29+
};
30+
31+
var NO_OP = function () {};
32+
33+
telemetry.performance = {
34+
mark: NO_OP,
35+
measure: NO_OP
36+
};
37+
38+
telemetry.start = function (config) {
39+
config = config || {};
40+
if (navigator.doNotTrack === '1') {
41+
return;
42+
}
43+
if (config.errorLogging) {
44+
startErrorLogging();
45+
}
46+
if (config.analytics) {
47+
var researchAnalytics = startAnalytics();
48+
if (config.performance) {
49+
configurePerformanceAPI(researchAnalytics);
50+
}
51+
}
52+
};
53+
54+
setupAnalytics();
55+
56+
function setupAnalytics() {
57+
window.ga = window.ga || function () {
58+
(window.ga.q = (window.ga.q || [])).push(arguments)
59+
};
60+
window.ga.l = +(new Date());
61+
62+
if (navigator.doNotTrack === '1') {
63+
return;
64+
}
65+
injectScript('https://www.google-analytics.com/analytics.js', function (err) {
66+
if (err) {
67+
console.warn('Could not load Analytics.js script:', err);
68+
return;
69+
}
70+
});
71+
}
72+
73+
function startErrorLogging() {
74+
injectScript('https://cdn.ravenjs.com/3.22.3/console/raven.min.js', function (err) {
75+
if (err) {
76+
console.warn('Could not load Raven.js script:', err);
77+
return;
78+
}
79+
if (!('Raven' in window)) {
80+
console.warn('Could not find `window.Raven` global');
81+
return;
82+
}
83+
configureRaven();
84+
});
85+
86+
function configureRaven () {
87+
console.log('Raven.js script loaded');
88+
Raven.config('https://e359be9fb9324addb0dc97b664cf5ee6@sentry.io/294878')
89+
.install();
90+
}
91+
};
92+
93+
function startAnalytics() {
94+
var CURRENT_VERSION = '1.0.1';
95+
var ga = telemetry.ga.create('UA-77033033-6', 'auto', 'mozillaResearch');
96+
ga('set', 'dimension1', CURRENT_VERSION);
97+
ga('send', 'pageview');
98+
return ga;
99+
}
100+
101+
function configurePerformanceAPI(ga) {
102+
telemetry.performance = {
103+
mark: function (name) {
104+
performance.mark(name);
105+
},
106+
107+
measure: function (name, start, end) {
108+
if (navigator.doNotTrack === '1') {
109+
return;
110+
}
111+
performance.measure(name, start, end);
112+
var performanceEntry = performance.getEntriesByName(name)[0];
113+
var duration = performanceEntry.duration;
114+
ga('send', 'event', 'Performance', name, undefined, duration);
115+
}
116+
};
117+
}
118+
119+
function injectScript (src, callback) {
120+
var script = document.createElement('script');
121+
script.src = src;
122+
script.crossorigin = 'anonymous';
123+
script.addEventListener('load', function () {
124+
if (callback) {
125+
callback(null, true);
126+
}
127+
});
128+
script.addEventListener('error', function (err) {
129+
if (callback) {
130+
callback(err);
131+
}
132+
});
133+
document.head.appendChild(script);
134+
return script;
135+
}
136+
137+
})(window);

Assets/WebGLTemplates/WebVR/webvr.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
}
3838

3939
function onUnityLoaded () {
40+
MozillaResearch.telemetry.performance.measure('LoadingTime', 'LoadingStart');
4041
canvas = document.getElementById('#canvas');
4142
document.body.dataset.unityLoaded = 'true';
4243
onResize();

0 commit comments

Comments
 (0)