UNPKG

2.42 kBPlain TextView Raw
1import {ListWrapper} from 'angular2/src/facade/collection';
2import {bind, provide, Provider, OpaqueToken} from 'angular2/src/core/di';
3
4import {Validator} from '../validator';
5import {Statistic} from '../statistic';
6import {MeasureValues} from '../measure_values';
7
8/**
9 * A validator that checks the regression slope of a specific metric.
10 * Waits for the regression slope to be >=0.
11 */
12export class RegressionSlopeValidator extends Validator {
13 // TODO(tbosch): use static values when our transpiler supports them
14 static get SAMPLE_SIZE(): OpaqueToken { return _SAMPLE_SIZE; }
15 // TODO(tbosch): use static values when our transpiler supports them
16 static get METRIC(): OpaqueToken { return _METRIC; }
17 // TODO(tbosch): use static values when our transpiler supports them
18 static get BINDINGS(): Provider[] { return _PROVIDERS; }
19
20 _sampleSize: number;
21 _metric: string;
22
23 constructor(sampleSize, metric) {
24 super();
25 this._sampleSize = sampleSize;
26 this._metric = metric;
27 }
28
29 describe(): {[key: string]: any} {
30 return {'sampleSize': this._sampleSize, 'regressionSlopeMetric': this._metric};
31 }
32
33 validate(completeSample: MeasureValues[]): MeasureValues[] {
34 if (completeSample.length >= this._sampleSize) {
35 var latestSample = ListWrapper.slice(completeSample, completeSample.length - this._sampleSize,
36 completeSample.length);
37 var xValues = [];
38 var yValues = [];
39 for (var i = 0; i < latestSample.length; i++) {
40 // For now, we only use the array index as x value.
41 // TODO(tbosch): think about whether we should use time here instead
42 xValues.push(i);
43 yValues.push(latestSample[i].values[this._metric]);
44 }
45 var regressionSlope = Statistic.calculateRegressionSlope(
46 xValues, Statistic.calculateMean(xValues), yValues, Statistic.calculateMean(yValues));
47 return regressionSlope >= 0 ? latestSample : null;
48 } else {
49 return null;
50 }
51 }
52}
53
54var _SAMPLE_SIZE = new OpaqueToken('RegressionSlopeValidator.sampleSize');
55var _METRIC = new OpaqueToken('RegressionSlopeValidator.metric');
56var _PROVIDERS = [
57 bind(RegressionSlopeValidator)
58 .toFactory((sampleSize, metric) => new RegressionSlopeValidator(sampleSize, metric),
59 [_SAMPLE_SIZE, _METRIC]),
60 provide(_SAMPLE_SIZE, {useValue: 10}),
61 provide(_METRIC, {useValue: 'scriptTime'})
62];