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-04-20 20:01:51 -04:00
|
|
|
|
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 {Injector, StaticProvider} from '@angular/core';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {AsyncTestCompleter, describe, expect, inject, it} from '@angular/core/testing/src/testing_internal';
|
2016-08-26 19:34:08 -04:00
|
|
|
|
2016-09-27 20:12:25 -04:00
|
|
|
import {Options, PerfLogEvent, PerfLogFeatures, UserMetric, WebDriverAdapter} from '../../index';
|
2016-04-20 20:01:51 -04:00
|
|
|
|
2017-12-17 18:10:54 -05:00
|
|
|
(function() {
|
2020-04-13 19:40:21 -04:00
|
|
|
let wdAdapter: MockDriverAdapter;
|
2016-04-20 20:01:51 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
function createMetric(
|
|
|
|
perfLogs: PerfLogEvent[], perfLogFeatures: PerfLogFeatures,
|
|
|
|
{userMetrics}: {userMetrics?: {[key: string]: string}} = {}): UserMetric {
|
|
|
|
if (!perfLogFeatures) {
|
|
|
|
perfLogFeatures =
|
|
|
|
new PerfLogFeatures({render: true, gc: true, frameCapture: true, userTiming: true});
|
2016-04-20 20:01:51 -04:00
|
|
|
}
|
2020-04-13 19:40:21 -04:00
|
|
|
if (!userMetrics) {
|
|
|
|
userMetrics = {};
|
|
|
|
}
|
|
|
|
wdAdapter = new MockDriverAdapter();
|
|
|
|
const providers: StaticProvider[] = [
|
|
|
|
Options.DEFAULT_PROVIDERS, UserMetric.PROVIDERS,
|
|
|
|
{provide: Options.USER_METRICS, useValue: userMetrics},
|
|
|
|
{provide: WebDriverAdapter, useValue: wdAdapter}
|
|
|
|
];
|
|
|
|
return Injector.create(providers).get(UserMetric);
|
|
|
|
}
|
2016-04-20 20:01:51 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
describe('user metric', () => {
|
|
|
|
it('should describe itself based on userMetrics', () => {
|
|
|
|
expect(createMetric([[]], new PerfLogFeatures(), {
|
|
|
|
userMetrics: {'loadTime': 'time to load'}
|
|
|
|
}).describe())
|
|
|
|
.toEqual({'loadTime': 'time to load'});
|
|
|
|
});
|
2016-04-20 20:01:51 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
describe('endMeasure', () => {
|
|
|
|
it('should stop measuring when all properties have numeric values',
|
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
|
|
|
const metric = createMetric(
|
|
|
|
[[]], new PerfLogFeatures(),
|
|
|
|
{userMetrics: {'loadTime': 'time to load', 'content': 'time to see content'}});
|
|
|
|
metric.beginMeasure().then(() => metric.endMeasure(true)).then(values => {
|
|
|
|
expect(values['loadTime']).toBe(25);
|
|
|
|
expect(values['content']).toBe(250);
|
|
|
|
async.done();
|
|
|
|
});
|
2016-04-20 20:01:51 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
wdAdapter.data['loadTime'] = 25;
|
|
|
|
// Wait before setting 2nd property.
|
|
|
|
setTimeout(() => {
|
|
|
|
wdAdapter.data['content'] = 250;
|
|
|
|
}, 50);
|
|
|
|
}), 600);
|
2016-04-20 20:01:51 -04:00
|
|
|
});
|
2020-04-13 19:40:21 -04:00
|
|
|
});
|
2017-12-16 17:42:55 -05:00
|
|
|
})();
|
2016-04-20 20:01:51 -04:00
|
|
|
|
|
|
|
class MockDriverAdapter extends WebDriverAdapter {
|
|
|
|
data: any = {};
|
|
|
|
|
|
|
|
executeScript(script: string): any {
|
|
|
|
// Just handles `return window.propName` ignores `delete window.propName`.
|
|
|
|
if (script.indexOf('return window.') == 0) {
|
2016-11-12 08:08:58 -05:00
|
|
|
const metricName = script.substring('return window.'.length);
|
2016-08-02 18:53:34 -04:00
|
|
|
return Promise.resolve(this.data[metricName]);
|
2016-04-20 20:01:51 -04:00
|
|
|
} else if (script.indexOf('delete window.') == 0) {
|
2016-08-02 18:53:34 -04:00
|
|
|
return Promise.resolve(null);
|
2016-04-20 20:01:51 -04:00
|
|
|
} else {
|
2016-08-02 18:53:34 -04:00
|
|
|
return Promise.reject(`Unexpected syntax: ${script}`);
|
2016-04-20 20:01:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|