All files / lib swsLongestReq.js

72.5% Statements 29/40
37.5% Branches 6/16
100% Functions 4/4
73.68% Lines 28/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                1x 1x 1x         1x     1x       1x     1x     1x       1x 1x     1x   7x   7x 2x 2x 2x 2x     5x 5x 5x 5x                                   1x   7x   7x   7x 7x 7x                             1x  
/**
 * Created by sv2 on 2/18/17.
 * Set of Longest Requests
 * Requests with longest observed handle time, since star
 */
 
'use strict';
 
var util = require('util');
var debug = require('debug')('sws:longestreq');
var swsUtil = require('./swsUtil');
 
function swsLongestRequests() {
 
    // Highest duration from all stored requests
    this.highest = -1;
 
    // Lowest duration from all stored requests
    this.lowest = -1;  // will be updated by first request
 
    // Max number of requests to keep.
    // Should not be too big as we're going keep ordered array of longest requests
    this.capacity = 100;
 
    // true if full
    this.full = false;
 
    // Store 100 longest requests
    this.longest_requests = [];
}
 
 
swsLongestRequests.prototype.getStats = function() {
    return this.longest_requests;
};
 
swsLongestRequests.prototype.placeReqResData = function(rrr) {
 
    var duration = rrr.responsetime;
 
    if( duration >= this.highest ){
        this.highest = duration;
        if(this.lowest==-1) this.lowest = duration; // this would be a case of first request
        this.longest_requests.push(rrr); // add to the end as highest
        return;
    }
 
    Eif( duration <= this.lowest ) {
        this.lowest = duration;
        this.longest_requests.unshift(rrr);  // add to the front as lowest
        return;
    }
 
    // we need to find right place for it
    // iterate over array and insert new record right before longer one
    var idx=-1;
    for(var i=0;(i<this.longest_requests.length) && (idx==-1);i++){
        if( duration < this.longest_requests[i].responsetime ){
            idx = i;
        }
    }
 
    if(idx!=-1){
        this.longest_requests.splice(idx,0,rrr);
    }
};
 
// Check if this qualifies as longest request, and store is yes
swsLongestRequests.prototype.processReqResData = function(rrr) {
 
    var duration = rrr.responsetime;
 
    Eif( !this.full ){
        // Filling up - store all requests
        this.placeReqResData(rrr);
        this.full = (this.longest_requests.length===this.capacity);
        return;
    }
 
    // Request is ignored when it's handle time < lowest, and no more space
    if( duration < this.lowest ) return;
 
    // Evict smallest one
    this.longest_requests.shift();
 
    // First entry duration is now the smallest one
    this.lowest = this.longest_requests[0].responsetime;
 
    this.placeReqResData(rrr);
};
 
module.exports = swsLongestRequests;