2014-02-06 16:17:38 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MediaWiki extension MultimediaViewer.
|
|
|
|
*
|
|
|
|
* MultimediaViewer is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MultimediaViewer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
( function () {
|
2014-11-21 10:07:40 +00:00
|
|
|
var PL;
|
2014-02-06 16:17:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Measures the network performance
|
2014-02-19 02:27:30 +00:00
|
|
|
* See <https://meta.wikimedia.org/wiki/Schema:MultimediaViewerNetworkPerformance>
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-11-21 10:07:40 +00:00
|
|
|
* @class mw.mmv.logging.PerformanceLogger
|
2014-09-04 23:00:55 +00:00
|
|
|
* @extends mw.mmv.logging.Logger
|
2014-02-06 16:17:38 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
2014-11-21 10:07:40 +00:00
|
|
|
function PerformanceLogger() {}
|
2014-02-06 16:17:38 +00:00
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
OO.inheritClass( PerformanceLogger, mw.mmv.logging.Logger );
|
2014-05-19 09:24:54 +00:00
|
|
|
|
2014-11-21 10:07:40 +00:00
|
|
|
PL = PerformanceLogger.prototype;
|
2014-02-06 16:17:38 +00:00
|
|
|
|
2014-05-19 09:24:54 +00:00
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2014-11-21 10:07:40 +00:00
|
|
|
PL.samplingFactor = mw.config.get( 'wgMultimediaViewer' ).networkPerformanceSamplingFactor;
|
2014-05-19 09:24:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2014-11-21 10:07:40 +00:00
|
|
|
PL.schema = 'MultimediaViewerNetworkPerformance';
|
2014-05-19 09:24:54 +00:00
|
|
|
|
2014-02-15 02:15:54 +00:00
|
|
|
/**
|
|
|
|
* Global setup that should be done while the page loads
|
|
|
|
*/
|
2014-11-21 10:07:40 +00:00
|
|
|
PL.init = function () {
|
2014-02-15 02:15:54 +00:00
|
|
|
var performance = this.getWindowPerformance();
|
|
|
|
|
|
|
|
// by default logging is cut off after 150 resources, which is not enough in debug mode
|
|
|
|
// only supported by IE
|
2014-05-07 04:49:15 +00:00
|
|
|
if ( mw.config.get( 'debug' ) && performance && performance.setResourceTimingBufferSize ) {
|
2014-02-15 02:15:54 +00:00
|
|
|
performance.setResourceTimingBufferSize( 500 );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-06 16:17:38 +00:00
|
|
|
/**
|
|
|
|
* Gather network performance for a given URL
|
|
|
|
* Will only run on a sample of users/requests. Avoid using this on URLs that aren't
|
|
|
|
* cached by the browser, as it will consume unnecessary bandwidth for the user.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-02-06 16:17:38 +00:00
|
|
|
* @param {string} type the type of request to be measured
|
|
|
|
* @param {string} url URL to be measured
|
2014-12-02 10:05:12 +00:00
|
|
|
* @param {jQuery.Deferred.<string>} [extraStatsDeferred] A promise which resolves to the extra stats.
|
2016-07-18 13:49:27 +00:00
|
|
|
* @return {jQuery.Promise} A promise that resolves when the contents of the URL have been fetched
|
2014-02-06 16:17:38 +00:00
|
|
|
*/
|
2014-12-02 10:05:12 +00:00
|
|
|
PL.record = function ( type, url, extraStatsDeferred ) {
|
2014-02-06 16:17:38 +00:00
|
|
|
var deferred = $.Deferred(),
|
|
|
|
request,
|
|
|
|
perf = this,
|
|
|
|
start;
|
|
|
|
|
2014-02-13 15:48:02 +00:00
|
|
|
try {
|
Use cross-origin img attribute instead of data URI
After lots of experimenting with Wireshark and
current Chrome + Firefox on Ubuntu 13.10, this is my
current understanding of the caching when preloading images
with AJAX requests:
* on Chrome, the image request always comes from browser cache
* Firefox makes two separate requests by default
* Firefox with img.crossOrigin = 'anonymous' makes two separate
requests, but the second one is a 304 (does not load the
image twice)
* when the image has already been cached by the browser (but not in
this session), Chrome skips both requests; Firefox skips the AJAX
request, but sends the normal one, and it returns with 304.
"wish I knew this when I started" things:
* the Chrome DevTools has an option to disable cache. When this is
enabled, requests in the same document context still come from
cache (so if I load the page, fire an AJAX request, then without
reloading the page, fire an AJAX request to the same URL, then the
second request will be cached), but an AJAX request - image request
pair is an exception from this.
* when using Ctrl-F5 in Firefox, requests on that page will never hit
the cache (even AJAX request fired after user activity; even if
two identical requests follow each other). When using clear cache
+ normal reload, this is not the case.
* if the image does not have an Allow-Origin header and is loaded
with crossOrigin=true, Firefox will refuse to load it. Chrome will
log an error in the console saying it refused to load it, but will
actually load it.
* Wireshark rocks.
Pushed some tech debt (browser + domain whitelist) into other tickets:
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/232
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/233
Reverted commits:
8a8d74f01d3dbd6d0c43b7fadc5284d204091761.
63021d0b0e95442cce101f9f92de8f0ff97d5f49.
Change-Id: I84ab2f3ac0a9706926adf7fe8726ecd9e9f843e0
Bug: 61542
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/207
2014-02-23 21:46:18 +00:00
|
|
|
request = this.newXHR();
|
2014-02-25 13:43:22 +00:00
|
|
|
|
|
|
|
request.onprogress = function ( e ) {
|
|
|
|
var percent;
|
|
|
|
|
|
|
|
if ( e.lengthComputable ) {
|
|
|
|
percent = ( e.loaded / e.total ) * 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
deferred.notify( request.response, percent );
|
|
|
|
};
|
|
|
|
|
Use cross-origin img attribute instead of data URI
After lots of experimenting with Wireshark and
current Chrome + Firefox on Ubuntu 13.10, this is my
current understanding of the caching when preloading images
with AJAX requests:
* on Chrome, the image request always comes from browser cache
* Firefox makes two separate requests by default
* Firefox with img.crossOrigin = 'anonymous' makes two separate
requests, but the second one is a 304 (does not load the
image twice)
* when the image has already been cached by the browser (but not in
this session), Chrome skips both requests; Firefox skips the AJAX
request, but sends the normal one, and it returns with 304.
"wish I knew this when I started" things:
* the Chrome DevTools has an option to disable cache. When this is
enabled, requests in the same document context still come from
cache (so if I load the page, fire an AJAX request, then without
reloading the page, fire an AJAX request to the same URL, then the
second request will be cached), but an AJAX request - image request
pair is an exception from this.
* when using Ctrl-F5 in Firefox, requests on that page will never hit
the cache (even AJAX request fired after user activity; even if
two identical requests follow each other). When using clear cache
+ normal reload, this is not the case.
* if the image does not have an Allow-Origin header and is loaded
with crossOrigin=true, Firefox will refuse to load it. Chrome will
log an error in the console saying it refused to load it, but will
actually load it.
* Wireshark rocks.
Pushed some tech debt (browser + domain whitelist) into other tickets:
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/232
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/233
Reverted commits:
8a8d74f01d3dbd6d0c43b7fadc5284d204091761.
63021d0b0e95442cce101f9f92de8f0ff97d5f49.
Change-Id: I84ab2f3ac0a9706926adf7fe8726ecd9e9f843e0
Bug: 61542
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/207
2014-02-23 21:46:18 +00:00
|
|
|
request.onreadystatechange = function () {
|
2019-02-05 23:59:52 +00:00
|
|
|
var total = ( new Date() ).getTime() - start;
|
2014-02-13 15:48:02 +00:00
|
|
|
|
Use cross-origin img attribute instead of data URI
After lots of experimenting with Wireshark and
current Chrome + Firefox on Ubuntu 13.10, this is my
current understanding of the caching when preloading images
with AJAX requests:
* on Chrome, the image request always comes from browser cache
* Firefox makes two separate requests by default
* Firefox with img.crossOrigin = 'anonymous' makes two separate
requests, but the second one is a 304 (does not load the
image twice)
* when the image has already been cached by the browser (but not in
this session), Chrome skips both requests; Firefox skips the AJAX
request, but sends the normal one, and it returns with 304.
"wish I knew this when I started" things:
* the Chrome DevTools has an option to disable cache. When this is
enabled, requests in the same document context still come from
cache (so if I load the page, fire an AJAX request, then without
reloading the page, fire an AJAX request to the same URL, then the
second request will be cached), but an AJAX request - image request
pair is an exception from this.
* when using Ctrl-F5 in Firefox, requests on that page will never hit
the cache (even AJAX request fired after user activity; even if
two identical requests follow each other). When using clear cache
+ normal reload, this is not the case.
* if the image does not have an Allow-Origin header and is loaded
with crossOrigin=true, Firefox will refuse to load it. Chrome will
log an error in the console saying it refused to load it, but will
actually load it.
* Wireshark rocks.
Pushed some tech debt (browser + domain whitelist) into other tickets:
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/232
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/233
Reverted commits:
8a8d74f01d3dbd6d0c43b7fadc5284d204091761.
63021d0b0e95442cce101f9f92de8f0ff97d5f49.
Change-Id: I84ab2f3ac0a9706926adf7fe8726ecd9e9f843e0
Bug: 61542
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/207
2014-02-23 21:46:18 +00:00
|
|
|
if ( request.readyState === 4 ) {
|
2014-02-25 13:43:22 +00:00
|
|
|
deferred.notify( request.response, 100 );
|
Use cross-origin img attribute instead of data URI
After lots of experimenting with Wireshark and
current Chrome + Firefox on Ubuntu 13.10, this is my
current understanding of the caching when preloading images
with AJAX requests:
* on Chrome, the image request always comes from browser cache
* Firefox makes two separate requests by default
* Firefox with img.crossOrigin = 'anonymous' makes two separate
requests, but the second one is a 304 (does not load the
image twice)
* when the image has already been cached by the browser (but not in
this session), Chrome skips both requests; Firefox skips the AJAX
request, but sends the normal one, and it returns with 304.
"wish I knew this when I started" things:
* the Chrome DevTools has an option to disable cache. When this is
enabled, requests in the same document context still come from
cache (so if I load the page, fire an AJAX request, then without
reloading the page, fire an AJAX request to the same URL, then the
second request will be cached), but an AJAX request - image request
pair is an exception from this.
* when using Ctrl-F5 in Firefox, requests on that page will never hit
the cache (even AJAX request fired after user activity; even if
two identical requests follow each other). When using clear cache
+ normal reload, this is not the case.
* if the image does not have an Allow-Origin header and is loaded
with crossOrigin=true, Firefox will refuse to load it. Chrome will
log an error in the console saying it refused to load it, but will
actually load it.
* Wireshark rocks.
Pushed some tech debt (browser + domain whitelist) into other tickets:
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/232
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/233
Reverted commits:
8a8d74f01d3dbd6d0c43b7fadc5284d204091761.
63021d0b0e95442cce101f9f92de8f0ff97d5f49.
Change-Id: I84ab2f3ac0a9706926adf7fe8726ecd9e9f843e0
Bug: 61542
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/207
2014-02-23 21:46:18 +00:00
|
|
|
deferred.resolve( request.response );
|
2014-12-02 10:05:12 +00:00
|
|
|
perf.recordEntryDelayed( type, total, url, request, extraStatsDeferred );
|
Use cross-origin img attribute instead of data URI
After lots of experimenting with Wireshark and
current Chrome + Firefox on Ubuntu 13.10, this is my
current understanding of the caching when preloading images
with AJAX requests:
* on Chrome, the image request always comes from browser cache
* Firefox makes two separate requests by default
* Firefox with img.crossOrigin = 'anonymous' makes two separate
requests, but the second one is a 304 (does not load the
image twice)
* when the image has already been cached by the browser (but not in
this session), Chrome skips both requests; Firefox skips the AJAX
request, but sends the normal one, and it returns with 304.
"wish I knew this when I started" things:
* the Chrome DevTools has an option to disable cache. When this is
enabled, requests in the same document context still come from
cache (so if I load the page, fire an AJAX request, then without
reloading the page, fire an AJAX request to the same URL, then the
second request will be cached), but an AJAX request - image request
pair is an exception from this.
* when using Ctrl-F5 in Firefox, requests on that page will never hit
the cache (even AJAX request fired after user activity; even if
two identical requests follow each other). When using clear cache
+ normal reload, this is not the case.
* if the image does not have an Allow-Origin header and is loaded
with crossOrigin=true, Firefox will refuse to load it. Chrome will
log an error in the console saying it refused to load it, but will
actually load it.
* Wireshark rocks.
Pushed some tech debt (browser + domain whitelist) into other tickets:
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/232
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/233
Reverted commits:
8a8d74f01d3dbd6d0c43b7fadc5284d204091761.
63021d0b0e95442cce101f9f92de8f0ff97d5f49.
Change-Id: I84ab2f3ac0a9706926adf7fe8726ecd9e9f843e0
Bug: 61542
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/207
2014-02-23 21:46:18 +00:00
|
|
|
}
|
|
|
|
};
|
2014-02-13 15:48:02 +00:00
|
|
|
|
2019-02-05 23:59:52 +00:00
|
|
|
start = ( new Date() ).getTime();
|
Use cross-origin img attribute instead of data URI
After lots of experimenting with Wireshark and
current Chrome + Firefox on Ubuntu 13.10, this is my
current understanding of the caching when preloading images
with AJAX requests:
* on Chrome, the image request always comes from browser cache
* Firefox makes two separate requests by default
* Firefox with img.crossOrigin = 'anonymous' makes two separate
requests, but the second one is a 304 (does not load the
image twice)
* when the image has already been cached by the browser (but not in
this session), Chrome skips both requests; Firefox skips the AJAX
request, but sends the normal one, and it returns with 304.
"wish I knew this when I started" things:
* the Chrome DevTools has an option to disable cache. When this is
enabled, requests in the same document context still come from
cache (so if I load the page, fire an AJAX request, then without
reloading the page, fire an AJAX request to the same URL, then the
second request will be cached), but an AJAX request - image request
pair is an exception from this.
* when using Ctrl-F5 in Firefox, requests on that page will never hit
the cache (even AJAX request fired after user activity; even if
two identical requests follow each other). When using clear cache
+ normal reload, this is not the case.
* if the image does not have an Allow-Origin header and is loaded
with crossOrigin=true, Firefox will refuse to load it. Chrome will
log an error in the console saying it refused to load it, but will
actually load it.
* Wireshark rocks.
Pushed some tech debt (browser + domain whitelist) into other tickets:
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/232
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/233
Reverted commits:
8a8d74f01d3dbd6d0c43b7fadc5284d204091761.
63021d0b0e95442cce101f9f92de8f0ff97d5f49.
Change-Id: I84ab2f3ac0a9706926adf7fe8726ecd9e9f843e0
Bug: 61542
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/207
2014-02-23 21:46:18 +00:00
|
|
|
request.open( 'GET', url, true );
|
2014-02-13 15:48:02 +00:00
|
|
|
request.send();
|
|
|
|
} catch ( e ) {
|
Use cross-origin img attribute instead of data URI
After lots of experimenting with Wireshark and
current Chrome + Firefox on Ubuntu 13.10, this is my
current understanding of the caching when preloading images
with AJAX requests:
* on Chrome, the image request always comes from browser cache
* Firefox makes two separate requests by default
* Firefox with img.crossOrigin = 'anonymous' makes two separate
requests, but the second one is a 304 (does not load the
image twice)
* when the image has already been cached by the browser (but not in
this session), Chrome skips both requests; Firefox skips the AJAX
request, but sends the normal one, and it returns with 304.
"wish I knew this when I started" things:
* the Chrome DevTools has an option to disable cache. When this is
enabled, requests in the same document context still come from
cache (so if I load the page, fire an AJAX request, then without
reloading the page, fire an AJAX request to the same URL, then the
second request will be cached), but an AJAX request - image request
pair is an exception from this.
* when using Ctrl-F5 in Firefox, requests on that page will never hit
the cache (even AJAX request fired after user activity; even if
two identical requests follow each other). When using clear cache
+ normal reload, this is not the case.
* if the image does not have an Allow-Origin header and is loaded
with crossOrigin=true, Firefox will refuse to load it. Chrome will
log an error in the console saying it refused to load it, but will
actually load it.
* Wireshark rocks.
Pushed some tech debt (browser + domain whitelist) into other tickets:
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/232
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/233
Reverted commits:
8a8d74f01d3dbd6d0c43b7fadc5284d204091761.
63021d0b0e95442cce101f9f92de8f0ff97d5f49.
Change-Id: I84ab2f3ac0a9706926adf7fe8726ecd9e9f843e0
Bug: 61542
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/207
2014-02-23 21:46:18 +00:00
|
|
|
// old browser not supporting XMLHttpRequest or CORS, or CORS is not permitted
|
2014-02-13 15:48:02 +00:00
|
|
|
return deferred.reject();
|
|
|
|
}
|
2014-02-06 16:17:38 +00:00
|
|
|
|
|
|
|
return deferred;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Records network performance results for a given url
|
|
|
|
* Will record if enough data is present and it's not a local cache hit
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-02-06 16:17:38 +00:00
|
|
|
* @param {string} type the type of request to be measured
|
|
|
|
* @param {number} total the total load time tracked with a basic technique
|
|
|
|
* @param {string} url URL of that was measured
|
|
|
|
* @param {XMLHttpRequest} request HTTP request that just completed
|
2014-12-02 10:05:12 +00:00
|
|
|
* @param {jQuery.Deferred.<string>} [extraStatsDeferred] A promise which resolves to extra stats to be included.
|
2017-05-15 13:24:32 +00:00
|
|
|
* @return {jQuery.Promise}
|
2014-02-06 16:17:38 +00:00
|
|
|
*/
|
2014-12-02 10:05:12 +00:00
|
|
|
PL.recordEntry = function ( type, total, url, request, extraStatsDeferred ) {
|
2014-02-15 02:15:54 +00:00
|
|
|
var matches,
|
2014-12-02 10:05:12 +00:00
|
|
|
logger = this,
|
2014-02-06 16:17:38 +00:00
|
|
|
stats = { type: type,
|
|
|
|
contentHost: window.location.host,
|
|
|
|
isHttps: window.location.protocol === 'https:',
|
|
|
|
total: total },
|
|
|
|
connection = this.getNavigatorConnection();
|
|
|
|
|
|
|
|
if ( !this.performanceChecked ) {
|
|
|
|
this.performanceChecked = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( url && url.length ) {
|
|
|
|
// There is no need to measure the same url more than once
|
|
|
|
if ( url in this.performanceChecked ) {
|
2017-05-15 13:24:32 +00:00
|
|
|
return $.Deferred().reject();
|
2014-02-06 16:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.performanceChecked[ url ] = true;
|
|
|
|
|
2017-10-31 17:49:29 +00:00
|
|
|
matches = url.match( /^https?:\/\/([^/?#]+)(?:[/?#]|$)/i );
|
2014-02-06 16:17:38 +00:00
|
|
|
stats.isHttps = url.indexOf( 'https' ) === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !matches || matches.length !== 2 ) {
|
|
|
|
stats.urlHost = stats.contentHost;
|
|
|
|
} else {
|
|
|
|
stats.urlHost = matches[ 1 ];
|
|
|
|
}
|
|
|
|
|
2014-02-15 02:15:54 +00:00
|
|
|
this.populateStatsFromXhr( stats, request );
|
|
|
|
this.populateStatsFromPerformance( stats, url );
|
2014-02-06 16:17:38 +00:00
|
|
|
|
|
|
|
// Add connection information if there's any
|
|
|
|
if ( connection ) {
|
|
|
|
if ( connection.bandwidth ) {
|
2014-02-15 02:15:54 +00:00
|
|
|
if ( connection.bandwidth === Infinity ) {
|
|
|
|
stats.bandwidth = -1;
|
|
|
|
} else {
|
|
|
|
stats.bandwidth = Math.round( connection.bandwidth );
|
|
|
|
}
|
2014-02-06 16:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( connection.metered ) {
|
|
|
|
stats.metered = connection.metered;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 13:24:32 +00:00
|
|
|
return ( extraStatsDeferred || $.Deferred().reject() ).done( function ( extraStats ) {
|
2014-12-02 10:05:12 +00:00
|
|
|
stats = $.extend( stats, extraStats );
|
|
|
|
} ).always( function () {
|
|
|
|
logger.log( stats );
|
|
|
|
} );
|
2014-02-06 16:17:38 +00:00
|
|
|
};
|
|
|
|
|
2014-02-15 02:15:54 +00:00
|
|
|
/**
|
|
|
|
* Processes an XMLHttpRequest (or jqXHR) object
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-02-15 02:15:54 +00:00
|
|
|
* @param {Object} stats stats object to extend with additional statistics fields
|
|
|
|
* @param {XMLHttpRequest} request
|
|
|
|
*/
|
2014-11-21 10:07:40 +00:00
|
|
|
PL.populateStatsFromXhr = function ( stats, request ) {
|
2014-02-15 02:15:54 +00:00
|
|
|
var age,
|
|
|
|
contentLength,
|
|
|
|
xcache,
|
|
|
|
xvarnish,
|
2014-12-17 10:31:45 +00:00
|
|
|
varnishXCache,
|
|
|
|
lastModified;
|
2014-02-15 02:15:54 +00:00
|
|
|
|
|
|
|
if ( !request ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
stats.status = request.status;
|
|
|
|
|
|
|
|
// Chrome disallows header access for CORS image requests, even if the responose has the
|
|
|
|
// proper header :-/
|
|
|
|
contentLength = request.getResponseHeader( 'Content-Length' );
|
|
|
|
if ( contentLength === null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
xcache = request.getResponseHeader( 'X-Cache' );
|
|
|
|
if ( xcache ) {
|
|
|
|
stats.XCache = xcache;
|
|
|
|
varnishXCache = this.parseVarnishXCacheHeader( xcache );
|
|
|
|
|
2019-02-20 19:22:21 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-each-util
|
2015-01-23 12:48:27 +00:00
|
|
|
$.each( varnishXCache, function ( key, value ) {
|
2014-02-15 02:15:54 +00:00
|
|
|
stats[ key ] = value;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
xvarnish = request.getResponseHeader( 'X-Varnish' );
|
|
|
|
if ( xvarnish ) {
|
|
|
|
stats.XVarnish = xvarnish;
|
|
|
|
}
|
|
|
|
|
|
|
|
stats.contentLength = parseInt( contentLength, 10 );
|
|
|
|
|
|
|
|
age = parseInt( request.getResponseHeader( 'Age' ), 10 );
|
|
|
|
if ( !isNaN( age ) ) {
|
|
|
|
stats.age = age;
|
|
|
|
}
|
|
|
|
|
|
|
|
stats.timestamp = new Date( request.getResponseHeader( 'Date' ) ).getTime() / 1000;
|
2014-12-17 10:31:45 +00:00
|
|
|
|
|
|
|
lastModified = request.getResponseHeader( 'Last-Modified' );
|
|
|
|
if ( lastModified ) {
|
|
|
|
stats.lastModified = new Date( lastModified ).getTime() / 1000;
|
|
|
|
}
|
2014-02-15 02:15:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populates statistics based on the Request Timing API
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-02-15 02:15:54 +00:00
|
|
|
* @param {Object} stats
|
|
|
|
* @param {string} url
|
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
PL.populateStatsFromPerformance = function ( stats, url ) {
|
2014-02-15 02:15:54 +00:00
|
|
|
var performance = this.getWindowPerformance(),
|
|
|
|
timingEntries, timingEntry;
|
|
|
|
|
|
|
|
// If we're given an xhr and we have access to the Navigation Timing API, use it
|
|
|
|
if ( performance && performance.getEntriesByName ) {
|
|
|
|
// This could be tricky as we need to match encoding (the Request Timing API uses
|
|
|
|
// percent-encoded UTF-8). The main use case we are interested in is thumbnails and
|
|
|
|
// jQuery AJAX. jQuery uses encodeURIComponent to construct URL parameters, and
|
|
|
|
// thumbnail URLs come from MediaWiki API which also encodes them, so both should be
|
|
|
|
// all right.
|
|
|
|
timingEntries = performance.getEntriesByName( url );
|
|
|
|
|
|
|
|
if ( timingEntries.length ) {
|
|
|
|
// Let's hope it's the first request for the given URL we are interested in.
|
|
|
|
// This could fail in exotic cases (e.g. we send an AJAX request for a thumbnail,
|
|
|
|
// but it exists on the page as a normal thumbnail with the exact same size),
|
|
|
|
// but it's unlikely.
|
2016-07-18 13:49:27 +00:00
|
|
|
timingEntry = timingEntries[ 0 ];
|
2014-02-15 02:15:54 +00:00
|
|
|
|
|
|
|
stats.total = Math.round( timingEntry.duration );
|
|
|
|
stats.redirect = Math.round( timingEntry.redirectEnd - timingEntry.redirectStart );
|
|
|
|
stats.dns = Math.round( timingEntry.domainLookupEnd - timingEntry.domainLookupStart );
|
|
|
|
stats.tcp = Math.round( timingEntry.connectEnd - timingEntry.connectStart );
|
|
|
|
stats.request = Math.round( timingEntry.responseStart - timingEntry.requestStart );
|
|
|
|
stats.response = Math.round( timingEntry.responseEnd - timingEntry.responseStart );
|
|
|
|
stats.cache = Math.round( timingEntry.domainLookupStart - timingEntry.fetchStart );
|
2015-02-19 23:30:29 +00:00
|
|
|
} else if ( performance.getEntriesByType( 'resource' ).length === 150 && this.isEnabled() ) {
|
2014-02-15 02:15:54 +00:00
|
|
|
// browser stops logging after 150 entries
|
|
|
|
mw.log( 'performance buffer full, results are probably incorrect' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like recordEntry, but takes a jqXHR argument instead of a normal XHR one.
|
|
|
|
* Due to the way some parameters are retrieved, this will work best if the context option
|
|
|
|
* for the ajax request was not used.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-02-15 02:15:54 +00:00
|
|
|
* @param {string} type the type of request to be measured
|
|
|
|
* @param {number} total the total load time tracked with a basic technique
|
|
|
|
* @param {jqXHR} jqxhr
|
|
|
|
*/
|
2014-11-21 10:07:40 +00:00
|
|
|
PL.recordJQueryEntry = function ( type, total, jqxhr ) {
|
2014-02-15 02:15:54 +00:00
|
|
|
var perf = this;
|
|
|
|
|
|
|
|
// We take advantage of the fact that the context of the jqXHR deferred is the AJAX
|
|
|
|
// settings object. The deferred has already resolved so chaining to it does not influence
|
|
|
|
// the timing.
|
|
|
|
jqxhr.done( function () {
|
|
|
|
var url;
|
|
|
|
|
|
|
|
if ( !this.url ) {
|
|
|
|
mw.log.warn( 'Cannot find URL - did you use context option?' );
|
|
|
|
} else {
|
|
|
|
url = this.url;
|
|
|
|
// The performance API returns absolute URLs, but the one in the settings object is
|
|
|
|
// usually relative.
|
|
|
|
if ( !url.match( /^(\w+:)?\/\// ) ) {
|
|
|
|
url = location.protocol + '//' + location.host + url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.crossDomain && this.dataType === 'jsonp' ) {
|
|
|
|
// Cross-domain jQuery requests return a fake jqXHR object which is useless and
|
|
|
|
// would only cause logging errors.
|
|
|
|
jqxhr = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
// jQuery does not expose the original XHR object, but the jqXHR wrapper is similar
|
|
|
|
// enogh that we will probably get away by passing it instead.
|
|
|
|
perf.recordEntry( type, total, url, jqxhr );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Records network performance results for a given url
|
|
|
|
* Will record if enough data is present and it's not a local cache hit
|
|
|
|
* Will run after a delay to make sure the window.performance entry is present
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-02-15 02:15:54 +00:00
|
|
|
* @param {string} type the type of request to be measured
|
|
|
|
* @param {number} total the total load time tracked with a basic technique
|
|
|
|
* @param {string} url URL of that was measured
|
|
|
|
* @param {XMLHttpRequest} request HTTP request that just completed
|
2014-12-02 10:05:12 +00:00
|
|
|
* @param {jQuery.Promise.<string>} extraStatsDeferred A promise which resolves to extra stats.
|
2014-02-15 02:15:54 +00:00
|
|
|
*/
|
2014-12-02 10:05:12 +00:00
|
|
|
PL.recordEntryDelayed = function ( type, total, url, request, extraStatsDeferred ) {
|
2014-02-15 02:15:54 +00:00
|
|
|
var perf = this;
|
|
|
|
|
|
|
|
// The timeout is necessary because if there's an entry in window.performance,
|
|
|
|
// it hasn't been added yet at this point
|
2015-01-23 12:48:27 +00:00
|
|
|
setTimeout( function () {
|
2014-12-02 10:05:12 +00:00
|
|
|
perf.recordEntry( type, total, url, request, extraStatsDeferred );
|
Use cross-origin img attribute instead of data URI
After lots of experimenting with Wireshark and
current Chrome + Firefox on Ubuntu 13.10, this is my
current understanding of the caching when preloading images
with AJAX requests:
* on Chrome, the image request always comes from browser cache
* Firefox makes two separate requests by default
* Firefox with img.crossOrigin = 'anonymous' makes two separate
requests, but the second one is a 304 (does not load the
image twice)
* when the image has already been cached by the browser (but not in
this session), Chrome skips both requests; Firefox skips the AJAX
request, but sends the normal one, and it returns with 304.
"wish I knew this when I started" things:
* the Chrome DevTools has an option to disable cache. When this is
enabled, requests in the same document context still come from
cache (so if I load the page, fire an AJAX request, then without
reloading the page, fire an AJAX request to the same URL, then the
second request will be cached), but an AJAX request - image request
pair is an exception from this.
* when using Ctrl-F5 in Firefox, requests on that page will never hit
the cache (even AJAX request fired after user activity; even if
two identical requests follow each other). When using clear cache
+ normal reload, this is not the case.
* if the image does not have an Allow-Origin header and is loaded
with crossOrigin=true, Firefox will refuse to load it. Chrome will
log an error in the console saying it refused to load it, but will
actually load it.
* Wireshark rocks.
Pushed some tech debt (browser + domain whitelist) into other tickets:
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/232
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/233
Reverted commits:
8a8d74f01d3dbd6d0c43b7fadc5284d204091761.
63021d0b0e95442cce101f9f92de8f0ff97d5f49.
Change-Id: I84ab2f3ac0a9706926adf7fe8726ecd9e9f843e0
Bug: 61542
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/207
2014-02-23 21:46:18 +00:00
|
|
|
}, 0 );
|
2014-02-15 02:15:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like recordEntryDelayed, but for jQuery AJAX requests.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-02-15 02:15:54 +00:00
|
|
|
* @param {string} type the type of request to be measured
|
|
|
|
* @param {number} total the total load time tracked with a basic technique
|
|
|
|
* @param {jqXHR} jqxhr
|
|
|
|
*/
|
2014-11-21 10:07:40 +00:00
|
|
|
PL.recordJQueryEntryDelayed = function ( type, total, jqxhr ) {
|
2014-02-15 02:15:54 +00:00
|
|
|
var perf = this;
|
|
|
|
|
|
|
|
// The timeout is necessary because if there's an entry in window.performance,
|
|
|
|
// it hasn't been added yet at this point
|
2015-01-23 12:48:27 +00:00
|
|
|
setTimeout( function () {
|
2014-02-15 02:15:54 +00:00
|
|
|
perf.recordJQueryEntry( type, total, jqxhr );
|
|
|
|
}, 0 );
|
|
|
|
};
|
|
|
|
|
2014-02-06 16:17:38 +00:00
|
|
|
/**
|
|
|
|
* Parses an X-Cache header from Varnish and extracts varnish information
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-02-06 16:17:38 +00:00
|
|
|
* @param {string} header The X-Cache header from the request
|
2016-07-18 13:49:27 +00:00
|
|
|
* @return {Object} The parsed X-Cache data
|
2014-02-06 16:17:38 +00:00
|
|
|
*/
|
2014-11-21 10:07:40 +00:00
|
|
|
PL.parseVarnishXCacheHeader = function ( header ) {
|
2014-02-06 16:17:38 +00:00
|
|
|
var parts,
|
|
|
|
part,
|
|
|
|
subparts,
|
|
|
|
i,
|
|
|
|
results = {},
|
|
|
|
matches;
|
|
|
|
|
|
|
|
if ( !header || !header.length ) {
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
parts = header.split( ',' );
|
|
|
|
|
|
|
|
for ( i = 0; i < parts.length; i++ ) {
|
|
|
|
part = parts[ i ];
|
|
|
|
subparts = part.trim().split( ' ' );
|
|
|
|
|
|
|
|
// If the subparts aren't space-separated, it's an unknown format, skip
|
|
|
|
if ( subparts.length < 2 ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
matches = part.match( /\(([0-9]+)\)/ );
|
|
|
|
|
|
|
|
// If there is no number between parenthesis for a given server
|
|
|
|
// it's an unknown format, skip
|
|
|
|
if ( !matches || matches.length !== 2 ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
results[ 'varnish' + ( i + 1 ) ] = subparts[ 0 ];
|
|
|
|
results[ 'varnish' + ( i + 1 ) + 'hits' ] = parseInt( matches[ 1 ], 10 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the window's Performance object
|
|
|
|
* Allows us to override for unit tests
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
|
|
|
* @return {Object} The window's Performance object
|
2014-02-06 16:17:38 +00:00
|
|
|
*/
|
2014-11-21 10:07:40 +00:00
|
|
|
PL.getWindowPerformance = function () {
|
2014-02-06 16:17:38 +00:00
|
|
|
return window.performance;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the navigator's Connection object
|
|
|
|
* Allows us to override for unit tests
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
|
|
|
* @return {Object} The navigator's Connection object
|
2014-02-06 16:17:38 +00:00
|
|
|
*/
|
2014-11-21 10:07:40 +00:00
|
|
|
PL.getNavigatorConnection = function () {
|
2014-02-06 16:17:38 +00:00
|
|
|
return navigator.connection || navigator.mozConnection || navigator.webkitConnection;
|
|
|
|
};
|
|
|
|
|
2014-02-13 15:48:02 +00:00
|
|
|
/**
|
|
|
|
* Returns a new XMLHttpRequest object
|
|
|
|
* Allows us to override for unit tests
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
|
|
|
* @return {XMLHttpRequest} New XMLHttpRequest
|
2014-02-13 15:48:02 +00:00
|
|
|
*/
|
2014-11-21 10:07:40 +00:00
|
|
|
PL.newXHR = function () {
|
2014-02-13 15:48:02 +00:00
|
|
|
return new XMLHttpRequest();
|
|
|
|
};
|
|
|
|
|
2014-12-02 10:05:12 +00:00
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
PL.log = function ( data ) {
|
2016-07-18 13:49:27 +00:00
|
|
|
var trackedWidths = mw.mmv.ThumbnailWidthCalculator.prototype.defaultOptions.widthBuckets.slice( 0 );
|
2015-08-05 09:01:14 +00:00
|
|
|
trackedWidths.push( 600 ); // Most common non-bucket size
|
|
|
|
|
2016-10-26 09:03:18 +00:00
|
|
|
// Track thumbnail load time with statsv, sampled
|
|
|
|
if ( this.isInSample() &&
|
|
|
|
data.type === 'image' &&
|
2016-07-18 13:49:27 +00:00
|
|
|
data.imageWidth > 0 &&
|
|
|
|
data.total > 20 &&
|
2018-09-10 05:57:57 +00:00
|
|
|
trackedWidths.indexOf( data.imageWidth ) !== -1
|
2016-07-18 13:49:27 +00:00
|
|
|
) {
|
|
|
|
mw.track( 'timing.media.thumbnail.client.' + data.imageWidth, data.total );
|
2015-07-23 16:48:25 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 23:30:29 +00:00
|
|
|
if ( this.isEnabled() ) {
|
|
|
|
mw.log( 'mw.mmv.logging.PerformanceLogger', data );
|
|
|
|
}
|
2014-12-02 10:05:12 +00:00
|
|
|
return mw.mmv.logging.Logger.prototype.log.call( this, data );
|
|
|
|
};
|
|
|
|
|
2014-11-21 10:07:40 +00:00
|
|
|
new PerformanceLogger().init();
|
2014-02-15 02:15:54 +00:00
|
|
|
|
2014-11-21 10:07:40 +00:00
|
|
|
mw.mmv.logging.PerformanceLogger = PerformanceLogger;
|
Use cross-origin img attribute instead of data URI
After lots of experimenting with Wireshark and
current Chrome + Firefox on Ubuntu 13.10, this is my
current understanding of the caching when preloading images
with AJAX requests:
* on Chrome, the image request always comes from browser cache
* Firefox makes two separate requests by default
* Firefox with img.crossOrigin = 'anonymous' makes two separate
requests, but the second one is a 304 (does not load the
image twice)
* when the image has already been cached by the browser (but not in
this session), Chrome skips both requests; Firefox skips the AJAX
request, but sends the normal one, and it returns with 304.
"wish I knew this when I started" things:
* the Chrome DevTools has an option to disable cache. When this is
enabled, requests in the same document context still come from
cache (so if I load the page, fire an AJAX request, then without
reloading the page, fire an AJAX request to the same URL, then the
second request will be cached), but an AJAX request - image request
pair is an exception from this.
* when using Ctrl-F5 in Firefox, requests on that page will never hit
the cache (even AJAX request fired after user activity; even if
two identical requests follow each other). When using clear cache
+ normal reload, this is not the case.
* if the image does not have an Allow-Origin header and is loaded
with crossOrigin=true, Firefox will refuse to load it. Chrome will
log an error in the console saying it refused to load it, but will
actually load it.
* Wireshark rocks.
Pushed some tech debt (browser + domain whitelist) into other tickets:
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/232
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/233
Reverted commits:
8a8d74f01d3dbd6d0c43b7fadc5284d204091761.
63021d0b0e95442cce101f9f92de8f0ff97d5f49.
Change-Id: I84ab2f3ac0a9706926adf7fe8726ecd9e9f843e0
Bug: 61542
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/207
2014-02-23 21:46:18 +00:00
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
}() );
|