Skip to content

Commit

Permalink
test: add integration test for component benchmark rule
Browse files Browse the repository at this point in the history
Adds a test for the component benchmark rule.
  • Loading branch information
devversion committed Jan 27, 2022
1 parent 98b0de7 commit f57a99a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bazel/benchmark/component_benchmark/defaults/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'zone.js/dist/zone';
import {enableProdMode} from '@angular/core';
import {platformBrowser} from '@angular/platform-browser';

// @ts-ignore This file needs to be defined by the consumer.
// @ts-ignore This file will be provided by the consumer.
import {AppModule} from './app.module';

enableProdMode();
Expand Down
18 changes: 18 additions & 0 deletions bazel/benchmark/component_benchmark/test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
load("//bazel/benchmark/component_benchmark:component_benchmark.bzl", "component_benchmark")

component_benchmark(
name = "perf_test",
driver = "app.perf.spec.ts",
driver_deps = [
"//bazel/benchmark/driver-utilities",
"@npm//protractor",
"@npm//@types/jasmine",
"@npm//@types/node",
],
ng_deps = [
"@npm//@angular/core",
"@npm//@angular/platform-browser",
],
ng_srcs = ["app.module.ts"],
prefix = "",
)
28 changes: 28 additions & 0 deletions bazel/benchmark/component_benchmark/test/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @license
* Copyright Google LLC 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
*/

import {Component, NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';

@Component({
selector: 'app-root',
template: `<button (click)="updateText()">{{ text }}</button>`,
})
class AppComponent {
text = 'Hello';

updateText() {
this.text = 'New';
}
}

@NgModule({
imports: [BrowserModule],
bootstrap: [AppComponent],
})
export class AppModule {}
29 changes: 29 additions & 0 deletions bazel/benchmark/component_benchmark/test/app.perf.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @license
* Copyright Google LLC 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
*/

import {element, By, browser} from 'protractor';
import {runBenchmark} from '../../driver-utilities/index';

describe('app performance', () => {
it('should change text when pressing the button', async () => {
await browser.get('/');

const button = element(By.css('button'));
expect(await button.getText()).toBe('Hello');

await button.click();
expect(await button.getText()).toBe('New');
});

it('should measure pressing on the button', async () => {
await runBenchmark({
id: 'button-press',
work: () => element(By.css('button')).click(),
});
});
});
14 changes: 9 additions & 5 deletions bazel/benchmark/driver-utilities/perf_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@
* 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
*/
import {resolve} from 'path';

export {verifyNoBrowserErrors} from './e2e_util';

/** Absolute path to the directory where benchmark results should be written to. */
const testOutputDirectory =
process.env.TEST_UNDECLARED_OUTPUTS_DIR ?? resolve('./dist/benchmark_results');

import {
SeleniumWebDriverAdapter,
Options,
Expand Down Expand Up @@ -76,6 +71,15 @@ function createBenchpressRunner(): Runner {
runId = process.env.GIT_SHA + ' ' + runId;
}

const testOutputDirectory = process.env.TEST_UNDECLARED_OUTPUTS_DIR;

if (testOutputDirectory === undefined) {
throw new Error(
'Unexpected execution outside of a Bazel test. ' +
'Missing `TEST_UNDECLARED_OUTPUTS_DIR` environment variable.',
);
}

const providers: StaticProvider[] = [
SeleniumWebDriverAdapter.PROTRACTOR_PROVIDERS,
{provide: Options.FORCE_GC, useValue: globalOptions.forceGc},
Expand Down

0 comments on commit f57a99a

Please sign in to comment.