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
|
|
|
|
*/
|
|
|
|
|
perf: switch angular to use StaticInjector instead of ReflectiveInjector
This change allows ReflectiveInjector to be tree shaken resulting
in not needed Reflect polyfil and smaller bundles.
Code savings for HelloWorld using Closure:
Reflective: bundle.js: 105,864(34,190 gzip)
Static: bundle.js: 154,889(33,555 gzip)
645( 2%)
BREAKING CHANGE:
`platformXXXX()` no longer accepts providers which depend on reflection.
Specifically the method signature when from `Provider[]` to
`StaticProvider[]`.
Example:
Before:
```
[
MyClass,
{provide: ClassA, useClass: SubClassA}
]
```
After:
```
[
{provide: MyClass, deps: [Dep1,...]},
{provide: ClassA, useClass: SubClassA, deps: [Dep1,...]}
]
```
NOTE: This only applies to platform creation and providers for the JIT
compiler. It does not apply to `@Compotent` or `@NgModule` provides
declarations.
Benchpress note: Previously Benchpress also supported reflective
provides, which now require static providers.
DEPRECATION:
- `ReflectiveInjector` is now deprecated as it will be remove. Use
`Injector.create` as a replacement.
closes #18496
2017-08-03 15:33:29 -04:00
|
|
|
import {Inject, Injectable, StaticProvider} 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 {
|
perf: switch angular to use StaticInjector instead of ReflectiveInjector
This change allows ReflectiveInjector to be tree shaken resulting
in not needed Reflect polyfil and smaller bundles.
Code savings for HelloWorld using Closure:
Reflective: bundle.js: 105,864(34,190 gzip)
Static: bundle.js: 154,889(33,555 gzip)
645( 2%)
BREAKING CHANGE:
`platformXXXX()` no longer accepts providers which depend on reflection.
Specifically the method signature when from `Provider[]` to
`StaticProvider[]`.
Example:
Before:
```
[
MyClass,
{provide: ClassA, useClass: SubClassA}
]
```
After:
```
[
{provide: MyClass, deps: [Dep1,...]},
{provide: ClassA, useClass: SubClassA, deps: [Dep1,...]}
]
```
NOTE: This only applies to platform creation and providers for the JIT
compiler. It does not apply to `@Compotent` or `@NgModule` provides
declarations.
Benchpress note: Previously Benchpress also supported reflective
provides, which now require static providers.
DEPRECATION:
- `ReflectiveInjector` is now deprecated as it will be remove. Use
`Injector.create` as a replacement.
closes #18496
2017-08-03 15:33:29 -04:00
|
|
|
static PROVIDERS = <StaticProvider[]>[{
|
|
|
|
provide: Sampler,
|
|
|
|
deps: [
|
|
|
|
WebDriverAdapter, Metric, Reporter, Validator, Options.PREPARE, Options.EXECUTE, Options.NOW
|
|
|
|
]
|
|
|
|
}];
|
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))
|
2018-01-08 14:22:15 -05:00
|
|
|
.then((measureValues) => {
|
|
|
|
if (!!measureValues['invalid']) {
|
|
|
|
return lastState;
|
|
|
|
}
|
|
|
|
return this._report(lastState, measureValues);
|
|
|
|
});
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|