Skip to content

Instantly share code, notes, and snippets.

@pherris
Last active August 29, 2015 14:07

Revisions

  1. pherris revised this gist Oct 22, 2014. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -10,12 +10,10 @@
    }, false);

    var Metrics = function () {
    var collectionInterval = 1000,
    intervalDelay = 200,
    var intervalDelay = 200,
    acceptableVariance = 10,
    poller,
    requested,
    metrics = {};
    requested;

    //helper to provide more precise timing if available
    function getTs(useDateTS) {
    @@ -25,7 +23,7 @@
    //tracks how long from when the 'event' was scheduled until it actually ran
    function tracker() {
    var now = getTs(), latency = now - requested - intervalDelay;
    //console.log(latency);

    if (latency > acceptableVariance) {
    var event = new CustomEvent('clientLatency', { 'detail': latency });
    document.dispatchEvent(event);
    @@ -49,16 +47,17 @@
    cancel: function () {
    if (poller) {
    clearInterval(poller);
    poller = null;
    }
    },
    track: function () {
    if (!poller) {
    //first time executing
    requested = getTs();
    poller = setTimeout(tracker, intervalDelay);
    return true;
    }

    return tracker;
    return false;
    },
    /**
    * Takes a ms param and waits for that many ms - reported latency will be
  2. pherris revised this gist Oct 22, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -73,7 +73,8 @@
    while (getTs() < startWaiting + ms) {
    //blocking
    }
    return 'watied ' + (getTs() - startWaiting);
    var doneWaiting = getTs();
    return 'watied ' + (doneWaiting - startWaiting) + ', est total latency: ' + (requested + intervalDelay - doneWaiting);
    }
    };
    }, Rally = {};
  3. pherris created this gist Oct 21, 2014.
    86 changes: 86 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    <!DOCTYPE html>
    <html lang="en">
    <head></head>
    <body>
    <script>
    //add logging for client latency (when detected).
    //to create client latency, Rally.metrics.wait(250);
    document.addEventListener('clientLatency', function (evt) {
    console.log('latency!', evt.detail);
    }, false);

    var Metrics = function () {
    var collectionInterval = 1000,
    intervalDelay = 200,
    acceptableVariance = 10,
    poller,
    requested,
    metrics = {};

    //helper to provide more precise timing if available
    function getTs(useDateTS) {
    return window.performance && !useDateTS ? window.performance.now() : new Date().getTime();
    }

    //tracks how long from when the 'event' was scheduled until it actually ran
    function tracker() {
    var now = getTs(), latency = now - requested - intervalDelay;
    //console.log(latency);
    if (latency > acceptableVariance) {
    var event = new CustomEvent('clientLatency', { 'detail': latency });
    document.dispatchEvent(event);
    }

    //schedule this method to be invoked again
    poller = setTimeout(tracker, intervalDelay);

    //time this track 'event' was scheduled
    requested = now;

    }

    function filterInt(value) {
    if(/^(\-|\+)?([0-9]+|Infinity)$/.test(value))
    return Number(value);
    return NaN;
    }

    return {
    cancel: function () {
    if (poller) {
    clearInterval(poller);
    }
    },
    track: function () {
    if (!poller) {
    //first time executing
    requested = getTs();
    poller = setTimeout(tracker, intervalDelay);
    }

    return tracker;
    },
    /**
    * Takes a ms param and waits for that many ms - reported latency will be
    * wait time minus *no more than* intervalDelay. If you want guaranteed latency
    * you need to request at least intervalDelay + acceptableVariance.
    */
    wait: function (ms) {
    if (isNaN(filterInt(ms))) {
    return NaN;
    }
    var startWaiting = getTs();
    while (getTs() < startWaiting + ms) {
    //blocking
    }
    return 'watied ' + (getTs() - startWaiting);
    }
    };
    }, Rally = {};

    Rally.metrics = new Metrics();

    Rally.metrics.track();
    </script>
    </body>
    </html>