some title
descr
by Vu Nguyen
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.13.0/polyfill.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.0.2/mocha.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.0.2/mocha.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.js"></script>
<!-- No need to change this -->
<!-- Mocha test output goes here. -->
<div id="mocha"></div>
JavaScript
/*
* Our application servers receive approximately 20 000
* http requests per second. Response timeout is 19000ms.
* Implement a statistics collector that calculates the
* median and average request response times for a 7 day
* dataset.
*
* Assigment:
* 1. Implement StatsCollector
* 2. Write tests (below StatsCollector)
*/
'use strict';
const TIMEOUT = 19000;
// State collector
class StatsCollector {
constructor(/*void*/) {
this.responseCount = 0;
this.totalResponseTimeMs = 0;
// create an array of 19000 elements
// Count resTimeMs using arr[resTimeMs].
// For example push resTime = 230: arr[230] += 1
this.resCountArr = new Array(TIMEOUT + 1).fill(0);
}
pushValue(responseTimeMs /*number*/) /*void*/ {
// TODO implement
let resTime;
this.responseCount += 1;
resTime = responseTimeMs > TIMEOUT ? TIMEOUT : responseTimeMs;
resTime = Math.round(resTime);
this.totalResponseTimeMs += resTime;
this.resCountArr[resTime] += 1;
}
getMedian() /*number*/ {
// TODO implement
let median = 0;
if (this.responseCount === 0) {
return median;
}
const halfResponseCount = Math.floor(this.responseCount / 2);
let leftCount = 0;
let medianIndex;
for (let i=0; i < this.resCountArr.length ; i++) {
leftCount += this.resCountArr[i];
if (leftCount >= halfResponseCount) {
medianIndex = i;
break;
}
}
const nextNumber = this.getNextNoneZeroNumber(medianIndex);
if(this.responseCount%2 === 1) {
median = nextNumber;
} else if(this.responseCount % 2 === 0 && this.resCountArr[medianIndex] === 1) {
median = (medianIndex + nextNumber)/2;
}
return median;
}
getNextNoneZeroNumber(index) {
for (let i=index + 1; i < this.resCountArr.length ; i++) {
if (this.resCountArr[i] > 0) {
return i;
}
}
}
getAverage() /*number*/ {
// TODO implement
if (this.responseCount === 0) {
return 0;
}
...