Skip to content

Commit

Permalink
perf: switch angular to use StaticInjector instead of ReflectiveInjector
Browse files Browse the repository at this point in the history
This change allows ReflectiveInjector to be tree shaken resulting
in not needed Reflect polyfil and smaller bundles.

BREAKING CHANGE:
  • Loading branch information
mhevery committed Aug 4, 2017
1 parent a785949 commit fd8649c
Show file tree
Hide file tree
Showing 93 changed files with 374 additions and 271 deletions.
4 changes: 2 additions & 2 deletions modules/e2e_util/perf_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const yargs = require('yargs');
const nodeUuid = require('node-uuid');
import * as fs from 'fs-extra';

import {SeleniumWebDriverAdapter, Options, JsonFileReporter, Validator, RegressionSlopeValidator, ConsoleReporter, SizeValidator, MultiReporter, MultiMetric, Runner, Provider} from '@angular/benchpress';
import {SeleniumWebDriverAdapter, Options, JsonFileReporter, Validator, RegressionSlopeValidator, ConsoleReporter, SizeValidator, MultiReporter, MultiMetric, Runner, StaticProvider} from '@angular/benchpress';
import {readCommandLine as readE2eCommandLine, openBrowser} from './e2e_util';

let cmdArgs: {'sample-size': number, 'force-gc': boolean, 'dryrun': boolean, 'bundles': boolean};
Expand Down Expand Up @@ -59,7 +59,7 @@ function createBenchpressRunner(): Runner {
}
const resultsFolder = './dist/benchmark_results';
fs.ensureDirSync(resultsFolder);
const providers: Provider[] = [
const providers: StaticProvider[] = [
SeleniumWebDriverAdapter.PROTRACTOR_PROVIDERS,
{provide: Options.FORCE_GC, useValue: cmdArgs['force-gc']},
{provide: Options.DEFAULT_DESCRIPTION, useValue: {'runId': runId}}, JsonFileReporter.PROVIDERS,
Expand Down
2 changes: 2 additions & 0 deletions modules/playground/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# How to run the examples locally

```
$ cp -r ./modules/playground ./dist/all/
$ ./node_modules/.bin/tsc -p modules --emitDecoratorMetadata -w
$ gulp serve
$ open http://localhost:8000/all/playground/src/hello_world/index.html?bundles=false
```
2 changes: 1 addition & 1 deletion packages/benchpress/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// Must be imported first, because Angular decorators throw on load.
import 'reflect-metadata';

export {InjectionToken, Injector, Provider, ReflectiveInjector} from '@angular/core';
export {InjectionToken, Injector, Provider, ReflectiveInjector, StaticProvider} from '@angular/core';
export {Options} from './src/common_options';
export {MeasureValues} from './src/measure_values';
export {Metric} from './src/metric';
Expand Down
9 changes: 8 additions & 1 deletion packages/benchpress/src/metric/perflog_metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ import {PerfLogEvent, PerfLogFeatures, WebDriverExtension} from '../web_driver_e
export class PerflogMetric extends Metric {
static SET_TIMEOUT = new InjectionToken('PerflogMetric.setTimeout');
static PROVIDERS = [
PerflogMetric, {
{
provide: PerflogMetric,
deps: [
WebDriverExtension, PerflogMetric.SET_TIMEOUT, Options.MICRO_METRICS, Options.FORCE_GC,
Options.CAPTURE_FRAMES, Options.RECEIVED_DATA, Options.REQUEST_COUNT
]
},
{
provide: PerflogMetric.SET_TIMEOUT,
useValue: (fn: Function, millis: number) => <any>setTimeout(fn, millis)
}
Expand Down
5 changes: 3 additions & 2 deletions packages/benchpress/src/metric/user_metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Inject, Injectable} from '@angular/core';
import {Inject, Injectable, StaticProvider} from '@angular/core';

import {Options} from '../common_options';
import {Metric} from '../metric';
import {WebDriverAdapter} from '../web_driver_adapter';

@Injectable()
export class UserMetric extends Metric {
static PROVIDERS = [UserMetric];
static PROVIDERS =
<StaticProvider[]>[{provide: UserMetric, deps: [Options.USER_METRICS, WebDriverAdapter]}];

constructor(
@Inject(Options.USER_METRICS) private _userMetrics: {[key: string]: string},
Expand Down
6 changes: 5 additions & 1 deletion packages/benchpress/src/reporter/console_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ export class ConsoleReporter extends Reporter {
static PRINT = new InjectionToken('ConsoleReporter.print');
static COLUMN_WIDTH = new InjectionToken('ConsoleReporter.columnWidth');
static PROVIDERS = [
ConsoleReporter, {provide: ConsoleReporter.COLUMN_WIDTH, useValue: 18}, {
{
provide: ConsoleReporter,
deps: [ConsoleReporter.COLUMN_WIDTH, SampleDescription, ConsoleReporter.PRINT]
},
{provide: ConsoleReporter.COLUMN_WIDTH, useValue: 18}, {
provide: ConsoleReporter.PRINT,
useValue: function(v: any) {
// tslint:disable-next-line:no-console
Expand Down
8 changes: 7 additions & 1 deletion packages/benchpress/src/reporter/json_file_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ import {formatStats, sortedProps} from './util';
@Injectable()
export class JsonFileReporter extends Reporter {
static PATH = new InjectionToken('JsonFileReporter.path');
static PROVIDERS = [JsonFileReporter, {provide: JsonFileReporter.PATH, useValue: '.'}];
static PROVIDERS = [
{
provide: JsonFileReporter,
deps: [SampleDescription, JsonFileReporter.PATH, Options.WRITE_FILE, Options.NOW]
},
{provide: JsonFileReporter.PATH, useValue: '.'}
];

constructor(
private _description: SampleDescription, @Inject(JsonFileReporter.PATH) private _path: string,
Expand Down
12 changes: 6 additions & 6 deletions packages/benchpress/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Provider, ReflectiveInjector} from '@angular/core';
import {Injector, StaticProvider} from '@angular/core';

import {Options} from './common_options';
import {Metric} from './metric';
Expand Down Expand Up @@ -34,17 +34,17 @@ import {IOsDriverExtension} from './webdriver/ios_driver_extension';
* It provides defaults, creates the injector and calls the sampler.
*/
export class Runner {
constructor(private _defaultProviders: Provider[] = []) {}
constructor(private _defaultProviders: StaticProvider[] = []) {}

sample({id, execute, prepare, microMetrics, providers, userMetrics}: {
id: string,
execute?: Function,
prepare?: Function,
microMetrics?: {[key: string]: string},
providers?: Provider[],
providers?: StaticProvider[],
userMetrics?: {[key: string]: string}
}): Promise<SampleState> {
const sampleProviders: Provider[] = [
const sampleProviders: StaticProvider[] = [
_DEFAULT_PROVIDERS, this._defaultProviders, {provide: Options.SAMPLE_ID, useValue: id},
{provide: Options.EXECUTE, useValue: execute}
];
Expand All @@ -61,7 +61,7 @@ export class Runner {
sampleProviders.push(providers);
}

const inj = ReflectiveInjector.resolveAndCreate(sampleProviders);
const inj = Injector.create(sampleProviders);
const adapter: WebDriverAdapter = inj.get(WebDriverAdapter);

return Promise
Expand All @@ -75,7 +75,7 @@ export class Runner {
// Only WebDriverAdapter is reused.
// TODO vsavkin consider changing it when toAsyncFactory is added back or when child
// injectors are handled better.
const injector = ReflectiveInjector.resolveAndCreate([
const injector = Injector.create([
sampleProviders, {provide: Options.CAPABILITIES, useValue: capabilities},
{provide: Options.USER_AGENT, useValue: userAgent},
{provide: WebDriverAdapter, useValue: adapter}
Expand Down
10 changes: 7 additions & 3 deletions packages/benchpress/src/sampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Inject, Injectable} from '@angular/core';
import {Inject, Injectable, StaticProvider} from '@angular/core';

import {Options} from './common_options';
import {MeasureValues} from './measure_values';
Expand All @@ -26,8 +26,12 @@ import {WebDriverAdapter} from './web_driver_adapter';
*/
@Injectable()
export class Sampler {
static PROVIDERS = [Sampler];

static PROVIDERS = <StaticProvider[]>[{
provide: Sampler,
deps: [
WebDriverAdapter, Metric, Reporter, Validator, Options.PREPARE, Options.EXECUTE, Options.NOW
]
}];
constructor(
private _driver: WebDriverAdapter, private _metric: Metric, private _reporter: Reporter,
private _validator: Validator, @Inject(Options.PREPARE) private _prepare: Function,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export class RegressionSlopeValidator extends Validator {
static SAMPLE_SIZE = new InjectionToken('RegressionSlopeValidator.sampleSize');
static METRIC = new InjectionToken('RegressionSlopeValidator.metric');
static PROVIDERS = [
RegressionSlopeValidator, {provide: RegressionSlopeValidator.SAMPLE_SIZE, useValue: 10},
{
provide: RegressionSlopeValidator,
deps: [RegressionSlopeValidator.SAMPLE_SIZE, RegressionSlopeValidator.METRIC]
},
{provide: RegressionSlopeValidator.SAMPLE_SIZE, useValue: 10},
{provide: RegressionSlopeValidator.METRIC, useValue: 'scriptTime'}
];

Expand Down
5 changes: 4 additions & 1 deletion packages/benchpress/src/validator/size_validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import {Validator} from '../validator';
@Injectable()
export class SizeValidator extends Validator {
static SAMPLE_SIZE = new InjectionToken('SizeValidator.sampleSize');
static PROVIDERS = [SizeValidator, {provide: SizeValidator.SAMPLE_SIZE, useValue: 10}];
static PROVIDERS = [
{provide: SizeValidator, deps: [SizeValidator.SAMPLE_SIZE]},
{provide: SizeValidator.SAMPLE_SIZE, useValue: 10}
];

constructor(@Inject(SizeValidator.SAMPLE_SIZE) private _sampleSize: number) { super(); }

Expand Down
7 changes: 5 additions & 2 deletions packages/benchpress/src/webdriver/chrome_driver_extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Inject, Injectable} from '@angular/core';
import {Inject, Injectable, StaticProvider} from '@angular/core';

import {Options} from '../common_options';
import {WebDriverAdapter} from '../web_driver_adapter';
Expand All @@ -21,7 +21,10 @@ import {PerfLogEvent, PerfLogFeatures, WebDriverExtension} from '../web_driver_e
*/
@Injectable()
export class ChromeDriverExtension extends WebDriverExtension {
static PROVIDERS = [ChromeDriverExtension];
static PROVIDERS = <StaticProvider>[{
provide: ChromeDriverExtension,
deps: [WebDriverAdapter, Options.USER_AGENT]
}];

private _majorChromeVersion: number;
private _firstRun = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {PerfLogEvent, PerfLogFeatures, WebDriverExtension} from '../web_driver_e

@Injectable()
export class FirefoxDriverExtension extends WebDriverExtension {
static PROVIDERS = [FirefoxDriverExtension];
static PROVIDERS = [{provide: FirefoxDriverExtension, deps: [WebDriverAdapter]}];

private _profilerStarted: boolean;

Expand Down
2 changes: 1 addition & 1 deletion packages/benchpress/src/webdriver/ios_driver_extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {PerfLogEvent, PerfLogFeatures, WebDriverExtension} from '../web_driver_e

@Injectable()
export class IOsDriverExtension extends WebDriverExtension {
static PROVIDERS = [IOsDriverExtension];
static PROVIDERS = [{provide: IOsDriverExtension, deps: [WebDriverAdapter]}];

constructor(private _driver: WebDriverAdapter) { super(); }

Expand Down
7 changes: 4 additions & 3 deletions packages/benchpress/test/metric/multi_metric_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
*/

import {AsyncTestCompleter, describe, expect, inject, it} from '@angular/core/testing/src/testing_internal';
import {Metric, MultiMetric, ReflectiveInjector} from '../../index';

import {Injector, Metric, MultiMetric} from '../../index';

export function main() {
function createMetric(ids: any[]) {
const m = ReflectiveInjector
.resolveAndCreate([
const m = Injector
.create([
ids.map(id => ({provide: id, useValue: new MockMetric(id)})),
MultiMetric.provideWith(ids)
])
Expand Down
8 changes: 4 additions & 4 deletions packages/benchpress/test/metric/perflog_metric_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Provider} from '@angular/core';
import {StaticProvider} from '@angular/core';
import {AsyncTestCompleter, beforeEach, describe, expect, inject, it} from '@angular/core/testing/src/testing_internal';

import {Metric, Options, PerfLogEvent, PerfLogFeatures, PerflogMetric, ReflectiveInjector, WebDriverExtension} from '../../index';
import {Injector, Metric, Options, PerfLogEvent, PerfLogFeatures, PerflogMetric, WebDriverExtension} from '../../index';
import {TraceEventFactory} from '../trace_event_factory';

export function main() {
Expand All @@ -33,7 +33,7 @@ export function main() {
if (!microMetrics) {
microMetrics = {};
}
const providers: Provider[] = [
const providers: StaticProvider[] = [
Options.DEFAULT_PROVIDERS, PerflogMetric.PROVIDERS,
{provide: Options.MICRO_METRICS, useValue: microMetrics}, {
provide: PerflogMetric.SET_TIMEOUT,
Expand All @@ -59,7 +59,7 @@ export function main() {
if (requestCount != null) {
providers.push({provide: Options.REQUEST_COUNT, useValue: requestCount});
}
return ReflectiveInjector.resolveAndCreate(providers).get(PerflogMetric);
return Injector.create(providers).get(PerflogMetric);
}

describe('perflog metric', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/benchpress/test/metric/user_metric_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Provider, ReflectiveInjector} from '@angular/core';
import {Injector, StaticProvider} from '@angular/core';
import {AsyncTestCompleter, describe, expect, inject, it} from '@angular/core/testing/src/testing_internal';

import {Options, PerfLogEvent, PerfLogFeatures, UserMetric, WebDriverAdapter} from '../../index';
Expand All @@ -25,12 +25,12 @@ export function main() {
userMetrics = {};
}
wdAdapter = new MockDriverAdapter();
const providers: Provider[] = [
const providers: StaticProvider[] = [
Options.DEFAULT_PROVIDERS, UserMetric.PROVIDERS,
{provide: Options.USER_METRICS, useValue: userMetrics},
{provide: WebDriverAdapter, useValue: wdAdapter}
];
return ReflectiveInjector.resolveAndCreate(providers).get(UserMetric);
return Injector.create(providers).get(UserMetric);
}

describe('user metric', () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/benchpress/test/reporter/console_reporter_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Provider} from '@angular/core';
import {StaticProvider} from '@angular/core';
import {describe, expect, it} from '@angular/core/testing/src/testing_internal';

import {ConsoleReporter, MeasureValues, ReflectiveInjector, SampleDescription} from '../../index';
import {ConsoleReporter, Injector, MeasureValues, SampleDescription} from '../../index';

export function main() {
describe('console reporter', () => {
Expand All @@ -30,7 +30,7 @@ export function main() {
if (sampleId == null) {
sampleId = 'null';
}
const providers: Provider[] = [
const providers: StaticProvider[] = [
ConsoleReporter.PROVIDERS, {
provide: SampleDescription,
useValue: new SampleDescription(sampleId, descriptions, metrics !)
Expand All @@ -40,7 +40,7 @@ export function main() {
if (columnWidth != null) {
providers.push({provide: ConsoleReporter.COLUMN_WIDTH, useValue: columnWidth});
}
reporter = ReflectiveInjector.resolveAndCreate(providers).get(ConsoleReporter);
reporter = Injector.create(providers).get(ConsoleReporter);
}

it('should print the sample id, description and table header', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/benchpress/test/reporter/json_file_reporter_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import {AsyncTestCompleter, describe, expect, inject, it} from '@angular/core/testing/src/testing_internal';

import {JsonFileReporter, MeasureValues, Options, ReflectiveInjector, SampleDescription} from '../../index';
import {Injector, JsonFileReporter, MeasureValues, Options, SampleDescription} from '../../index';

export function main() {
describe('file reporter', () => {
Expand All @@ -34,7 +34,7 @@ export function main() {
}
}
];
return ReflectiveInjector.resolveAndCreate(providers).get(JsonFileReporter);
return Injector.create(providers).get(JsonFileReporter);
}

it('should write all data into a file',
Expand Down
6 changes: 3 additions & 3 deletions packages/benchpress/test/reporter/multi_reporter_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

import {AsyncTestCompleter, describe, expect, inject, it} from '@angular/core/testing/src/testing_internal';

import {MeasureValues, MultiReporter, ReflectiveInjector, Reporter} from '../../index';
import {Injector, MeasureValues, MultiReporter, Reporter} from '../../index';

export function main() {
function createReporters(ids: any[]) {
const r = ReflectiveInjector
.resolveAndCreate([
const r = Injector
.create([
ids.map(id => ({provide: id, useValue: new MockReporter(id)})),
MultiReporter.provideWith(ids)
])
Expand Down
6 changes: 3 additions & 3 deletions packages/benchpress/test/runner_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

import {AsyncTestCompleter, describe, expect, inject, it} from '@angular/core/testing/src/testing_internal';

import {Injector, Metric, Options, ReflectiveInjector, Runner, SampleDescription, SampleState, Sampler, Validator, WebDriverAdapter} from '../index';
import {Injector, Metric, Options, Runner, SampleDescription, SampleState, Sampler, Validator, WebDriverAdapter} from '../index';

export function main() {
describe('runner', () => {
let injector: ReflectiveInjector;
let injector: Injector;
let runner: Runner;

function createRunner(defaultProviders?: any[]): Runner {
Expand All @@ -22,7 +22,7 @@ export function main() {
runner = new Runner([
defaultProviders, {
provide: Sampler,
useFactory: (_injector: ReflectiveInjector) => {
useFactory: (_injector: Injector) => {
injector = _injector;
return new MockSampler();
},
Expand Down
Loading

0 comments on commit fd8649c

Please sign in to comment.