1
0
mirror of https://github.com/psychopy/psychojs.git synced 2025-05-10 10:40:54 +00:00

estimate of download speed

This commit is contained in:
Alain Pitiot 2022-01-14 11:41:29 +01:00
parent 874743105a
commit aeca039240
3 changed files with 76 additions and 2 deletions

View File

@ -176,7 +176,7 @@ export class PsychoJS
}
this.logger.info("[PsychoJS] Initialised.");
this.logger.info("[PsychoJS] @version 2021.3.0");
this.logger.info("[PsychoJS] @version 2022.1.0");
// hide the initialisation message:
jQuery("#root").addClass("is-ready");

View File

@ -404,7 +404,7 @@ export class ServerManager extends PsychObject
* </ul>
*
* @name module:core.ServerManager#prepareResources
* @param {Array.<{name: string, path: string, download: boolean} | Symbol>} [resources=[]] - the list of resources
* @param {Array.<{name: string, path: string, download: boolean} | String | Symbol>} [resources=[]] - the list of resources
* @function
* @public
*/
@ -471,6 +471,20 @@ export class ServerManager extends PsychObject
throw "resources must be manually specified when the experiment is running locally: ALL_RESOURCES cannot be used";
}
// convert those resources that are only a string to an object with name and path:
for (let r = 0; r < resources.length; ++r)
{
const resource = resources[r];
if (typeof resource === "string")
{
resources[r] = {
name: resource,
path: resource,
download: true
}
}
}
for (let { name, path, download } of resources)
{
if (!this._resources.has(name))
@ -1179,6 +1193,11 @@ export class ServerManager extends PsychObject
*/
_setupPreloadQueue(resources)
{
const response = {
origin: "ServerManager._downloadResources",
context: "when downloading resources for experiment: " + this._psychoJS.config.experiment.name,
};
this._preloadQueue = new createjs.LoadQueue(true, "", true);
const self = this;

View File

@ -1413,3 +1413,58 @@ export function extensionFromMimeType(mimeType)
return '.dat';
}
/**
* Get an estimate of the download speed, by repeatedly downloading an image file from a distant
* server.
*
* @name module:util.getDownloadSpeed
* @function
* @public
* @param {PsychoJS} psychoJS the instance of PsychoJS
* @param {number} [nbDownloads = 1] the number of image downloads over which to average
* the download speed
* @return {number} the download speed, in megabits per second
*/
export async function getDownloadSpeed(psychoJS, nbDownloads = 1)
{
// url of the image to download and size of the image in bits:
// TODO use a variety of files, with different sizes
const imageUrl = "https://upload.wikimedia.org/wikipedia/commons/a/a6/Brandenburger_Tor_abends.jpg";
const imageSize_b = 2707459 * 8;
return new Promise( (resolve, reject) =>
{
let downloadTimeAccumulator = 0;
let downloadCounter = 0;
const download = new Image();
download.onload = () =>
{
const toc = performance.now();
downloadTimeAccumulator += (toc-tic);
++ downloadCounter;
if (downloadCounter === nbDownloads)
{
const speed_bps = (imageSize_b * nbDownloads) / (downloadTimeAccumulator / 1000);
resolve(speed_bps / 1024 / 1024);
}
else
{
tic = performance.now();
download.src = `${imageUrl}?salt=${tic}`;
}
}
download.onerror = (event) =>
{
const errorMsg = `unable to estimate the download speed: ${JSON.stringify(event)}`;
psychoJS.logger.error(errorMsg);
reject(errorMsg);
}
let tic = performance.now();
download.src = `${imageUrl}?salt=${tic}`;
});
}