2016-08-03 18:00:07 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-09-18 18:39:26 -04:00
|
|
|
import {Inject, Injectable} from '@angular/core';
|
2015-05-27 17:57:54 -04:00
|
|
|
|
2016-08-03 18:00:07 -04:00
|
|
|
import {Options} from './common_options';
|
|
|
|
import {MeasureValues} from './measure_values';
|
2015-05-27 17:57:54 -04:00
|
|
|
import {Metric} from './metric';
|
|
|
|
import {Reporter} from './reporter';
|
2016-08-03 18:00:07 -04:00
|
|
|
import {Validator} from './validator';
|
2015-05-27 17:57:54 -04:00
|
|
|
import {WebDriverAdapter} from './web_driver_adapter';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Sampler owns the sample loop:
|
|
|
|
* 1. calls the prepare/execute callbacks,
|
|
|
|
* 2. gets data from the metric
|
|
|
|
* 3. asks the validator for a valid sample
|
|
|
|
* 4. reports the new data to the reporter
|
|
|
|
* 5. loop until there is a valid sample
|
|
|
|
*/
|
2016-08-26 19:34:08 -04:00
|
|
|
@Injectable()
|
2015-05-27 17:57:54 -04:00
|
|
|
export class Sampler {
|
2016-08-26 19:34:08 -04:00
|
|
|
static PROVIDERS = [Sampler];
|
2015-05-27 17:57:54 -04:00
|
|
|
|
2016-08-26 19:34:08 -04:00
|
|
|
constructor(
|
|
|
|
private _driver: WebDriverAdapter, private _metric: Metric, private _reporter: Reporter,
|
|
|
|
private _validator: Validator, @Inject(Options.PREPARE) private _prepare: Function,
|
|
|
|
@Inject(Options.EXECUTE) private _execute: Function,
|
|
|
|
@Inject(Options.NOW) private _now: Function) {}
|
2015-05-27 17:57:54 -04:00
|
|
|
|
|
|
|
sample(): Promise<SampleState> {
|
2016-08-26 19:34:08 -04:00
|
|
|
const loop = (lastState: SampleState): Promise<SampleState> => {
|
2015-05-27 17:57:54 -04:00
|
|
|
return this._iterate(lastState).then((newState) => {
|
2017-03-02 12:37:01 -05:00
|
|
|
if (newState.validSample != null) {
|
2015-05-27 17:57:54 -04:00
|
|
|
return newState;
|
|
|
|
} else {
|
|
|
|
return loop(newState);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
return loop(new SampleState([], null));
|
|
|
|
}
|
|
|
|
|
2016-08-26 19:34:08 -04:00
|
|
|
private _iterate(lastState: SampleState): Promise<SampleState> {
|
2017-03-24 12:56:50 -04:00
|
|
|
let resultPromise: Promise<SampleState|null>;
|
2016-08-26 19:34:08 -04:00
|
|
|
if (this._prepare !== Options.NO_PREPARE) {
|
2015-05-27 17:57:54 -04:00
|
|
|
resultPromise = this._driver.waitFor(this._prepare);
|
|
|
|
} else {
|
2016-08-02 18:53:34 -04:00
|
|
|
resultPromise = Promise.resolve(null);
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|
2016-08-26 19:34:08 -04:00
|
|
|
if (this._prepare !== Options.NO_PREPARE || lastState.completeSample.length === 0) {
|
2015-05-27 17:57:54 -04:00
|
|
|
resultPromise = resultPromise.then((_) => this._metric.beginMeasure());
|
|
|
|
}
|
|
|
|
return resultPromise.then((_) => this._driver.waitFor(this._execute))
|
2016-08-26 19:34:08 -04:00
|
|
|
.then((_) => this._metric.endMeasure(this._prepare === Options.NO_PREPARE))
|
2015-05-27 17:57:54 -04:00
|
|
|
.then((measureValues) => this._report(lastState, measureValues));
|
|
|
|
}
|
|
|
|
|
2016-08-03 18:00:07 -04:00
|
|
|
private _report(state: SampleState, metricValues: {[key: string]: any}): Promise<SampleState> {
|
2016-11-12 08:08:58 -05:00
|
|
|
const measureValues = new MeasureValues(state.completeSample.length, this._now(), metricValues);
|
|
|
|
const completeSample = state.completeSample.concat([measureValues]);
|
|
|
|
const validSample = this._validator.validate(completeSample);
|
|
|
|
let resultPromise = this._reporter.reportMeasureValues(measureValues);
|
2017-03-02 12:37:01 -05:00
|
|
|
if (validSample != null) {
|
2015-05-27 17:57:54 -04:00
|
|
|
resultPromise =
|
2016-08-03 18:00:07 -04:00
|
|
|
resultPromise.then((_) => this._reporter.reportSample(completeSample, validSample));
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|
|
|
|
return resultPromise.then((_) => new SampleState(completeSample, validSample));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SampleState {
|
2017-03-24 12:56:50 -04:00
|
|
|
constructor(public completeSample: MeasureValues[], public validSample: MeasureValues[]|null) {}
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|