Skip to content

Commit e9a76b1

Browse files
committed
Revert "build: delete unused benchmark tests which do not build (#25384)"
This reverts commit 79a96c1.
1 parent 79a96c1 commit e9a76b1

File tree

86 files changed

+3886
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+3886
-1
lines changed

test/BUILD.bazel

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
load("//tools:defaults.bzl", "ts_library")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
exports_files(["bazel-karma-local-config.js"])
6+
7+
# Common set-up for all Angular Material and CDK tests.
8+
ts_library(
9+
name = "angular_test_init",
10+
testonly = True,
11+
# This file *must* end with "spec" in order for "karma_web_test_suite" to load it.
12+
srcs = ["angular-test-init-spec.ts"],
13+
deps = [
14+
"@npm//@angular/core",
15+
"@npm//@angular/platform-browser-dynamic",
16+
"@npm//@types/jasmine",
17+
],
18+
)

test/angular-test-init-spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {TestBed} from '@angular/core/testing';
2+
import {
3+
BrowserDynamicTestingModule,
4+
platformBrowserDynamicTesting,
5+
} from '@angular/platform-browser-dynamic/testing';
6+
7+
/*
8+
* Common setup / initialization for all unit tests in Angular Material and CDK.
9+
*/
10+
11+
TestBed.initTestEnvironment([BrowserDynamicTestingModule], platformBrowserDynamicTesting());
12+
13+
(window as any).module = {};
14+
(window as any).isNode = false;
15+
(window as any).isBrowser = true;
16+
(window as any).global = window;

test/bazel-karma-local-config.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Karma configuration that is used by Bazel karma_web_test targets which do not
3+
* want to launch any browser and just enable manual browser debugging.
4+
*/
5+
6+
module.exports = config => {
7+
const overwrites = {};
8+
9+
// By default "@bazel/concatjs" configures Chrome as browser. Since we don't want
10+
// to launch any browser at all, we overwrite the "browsers" option. Since the
11+
// default config tries to extend the browsers array with "Chrome", we need to
12+
// always return a new empty array.
13+
Object.defineProperty(overwrites, 'browsers', {
14+
get: () => [],
15+
set: () => {},
16+
enumerable: true,
17+
});
18+
19+
// Ensures that tests start executing once browsers have been manually connected. We need
20+
// to use "defineProperty" because the default "@bazel/concatjs" config overwrites the option.
21+
Object.defineProperty(overwrites, 'autoWatch', {
22+
get: () => true,
23+
set: () => {},
24+
enumerable: true,
25+
});
26+
27+
// When not running with ibazel, do not set up the `@bazel/concatjs` watcher. This one
28+
// relies on ibazel to write to the `stdin` interface. When running without ibazel, the
29+
// watcher will kill concatjs since there is no data written to the `stdin` interface.
30+
if (process.env['IBAZEL_NOTIFY_CHANGES'] !== 'y') {
31+
// We pre-define a plugins array that captures registration of Karma plugins
32+
// and unsets the watcher definitions so that no watcher can be configured.
33+
overwrites.plugins = new KarmaPluginArrayWithoutWatchers();
34+
}
35+
36+
config.set(overwrites);
37+
};
38+
39+
class KarmaPluginArrayWithoutWatchers extends Array {
40+
// The Bazel Karma rules only register new plugins using `.push`.
41+
push(...plugins) {
42+
plugins.filter(p => typeof p === 'object').forEach(p => delete p.watcher);
43+
44+
super.push(...plugins);
45+
}
46+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
load("@npm//@angular/build-toolinge/bazel/benchmark/component_benchmark:component_benchmark.bzl", "component_benchmark")
2+
load("//tools:defaults.bzl", "ng_test_library", "ng_web_test_suite", "ts_library")
3+
4+
package(default_visibility = ["//visibility:public"])
5+
6+
# These are two separate files that provide the same tidy interface for running a performance
7+
# benchmark.
8+
#
9+
# The testbed and protractor tests use different methods for measuring time. Since Karma does not
10+
# patch console.time, the results are not pushed back from the page to the Karma server. Instead
11+
# we use performance.now() to measure performance.
12+
13+
ts_library(
14+
name = "constants",
15+
srcs = [":constants.ts"],
16+
)
17+
18+
ts_library(
19+
name = "protractor-benchmark-utilities",
20+
srcs = [":protractor-benchmark-utilities.ts"],
21+
deps = [
22+
":constants",
23+
"@npm//@angular/build-tooling/bazel/benchmark/driver-utilities",
24+
"@npm//@types/node",
25+
],
26+
)
27+
28+
ts_library(
29+
name = "testbed-benchmark-utilities",
30+
srcs = [":testbed-benchmark-utilities.ts"],
31+
deps = [":constants"],
32+
)
33+
34+
# ProtractorHarnessEnvironment
35+
36+
component_benchmark(
37+
name = "e2e_benchmark",
38+
driver = ":protractor.perf-spec.ts",
39+
driver_deps = [
40+
":constants",
41+
":protractor-benchmark-utilities",
42+
"@npm//protractor",
43+
"@npm//@types/jasmine",
44+
"@npm//@types/node",
45+
"//src/cdk/testing",
46+
"//src/material/button/testing",
47+
"//src/cdk/testing/protractor",
48+
],
49+
ng_deps = [
50+
":constants",
51+
"@npm//@angular/core",
52+
"@npm//@angular/platform-browser",
53+
"//src/material/button",
54+
],
55+
ng_srcs = [":app.module.ts"],
56+
prefix = "",
57+
styles = ["//src/material/prebuilt-themes:indigo-pink"],
58+
)
59+
60+
# TestbedHarnessEnvironment
61+
62+
ng_test_library(
63+
name = "unit_tests_lib",
64+
srcs = ["testbed.perf-spec.ts"],
65+
deps = [
66+
":constants",
67+
":testbed-benchmark-utilities",
68+
"//src/cdk/testing",
69+
"//src/cdk/testing/testbed",
70+
"//src/material/button",
71+
"//src/material/button/testing",
72+
"@npm//@angular/build-tooling/bazel/benchmark/driver-utilities",
73+
],
74+
)
75+
76+
ng_web_test_suite(
77+
name = "unit_benchmark_tests",
78+
deps = [":unit_tests_lib"],
79+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {Component, NgModule, ViewEncapsulation} from '@angular/core';
10+
import {BrowserModule} from '@angular/platform-browser';
11+
import {MatButtonModule} from '@angular/material/button';
12+
import {NUM_BUTTONS} from './constants';
13+
14+
/** component: component-harness-test */
15+
16+
@Component({
17+
selector: 'app-root',
18+
template: `
19+
<button *ngFor="let val of vals" mat-button> {{ val }} </button>
20+
`,
21+
encapsulation: ViewEncapsulation.None,
22+
styleUrls: ['../../../../../src/material/core/theming/prebuilt/indigo-pink.css'],
23+
})
24+
export class ButtonHarnessTest {
25+
vals = Array.from({length: NUM_BUTTONS}, (_, i) => i);
26+
}
27+
28+
@NgModule({
29+
declarations: [ButtonHarnessTest],
30+
imports: [BrowserModule, MatButtonModule],
31+
bootstrap: [ButtonHarnessTest],
32+
})
33+
export class AppModule {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
/**
10+
* Benchpress gives us fine-grained metrics on browser interactions. In some cases, we want to
11+
* just get a whole measurement of how long the entire interaction took.
12+
*/
13+
export const USE_BENCHPRESS = false;
14+
15+
export const NUM_BUTTONS = 25;
16+
export const MIDDLE_BUTTON = `${Math.floor(NUM_BUTTONS / 2)}`;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {runBenchmark} from '@angular/build-tooling/bazel/benchmark/driver-utilities';
10+
import {USE_BENCHPRESS} from './constants';
11+
12+
/**
13+
* Records the performance of the given function.
14+
*
15+
* @param id A unique identifier.
16+
* @param callback A function whose performance will be recorded.
17+
*/
18+
export async function benchmark(id: string, callback: () => Promise<unknown>) {
19+
if (USE_BENCHPRESS) {
20+
await benchmarkWithBenchpress(id, callback);
21+
} else {
22+
await benchmarkWithConsoleAPI(id, callback);
23+
}
24+
}
25+
26+
/**
27+
* A simple wrapper for runBenchmark which is a wrapper for benchpress.
28+
*/
29+
async function benchmarkWithBenchpress(id: string, callback: () => Promise<unknown>) {
30+
await runBenchmark({
31+
id,
32+
url: '',
33+
ignoreBrowserSynchronization: true,
34+
work: async () => await callback(),
35+
});
36+
}
37+
38+
/**
39+
* Measures the time it takes to invoke the given function and prints the duration to the console.
40+
*/
41+
async function benchmarkWithConsoleAPI(id: string, callback: () => Promise<unknown>, runs = 5) {
42+
const t0 = Number(process.hrtime.bigint()) / 1000000;
43+
for (let i = 0; i < runs; i++) {
44+
await callback();
45+
}
46+
const t1 = Number(process.hrtime.bigint()) / 1000000;
47+
console.warn(`${id}: ${((t1 - t0) / runs).toFixed(2)}ms (avg over ${runs} runs)`);
48+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {HarnessLoader} from '@angular/cdk/testing';
10+
import {MatButtonHarness} from '@angular/material/button/testing/button-harness';
11+
import {ProtractorHarnessEnvironment} from '@angular/cdk/testing/protractor';
12+
import {$$, element, by, browser} from 'protractor';
13+
import {benchmark} from './protractor-benchmark-utilities';
14+
import {NUM_BUTTONS} from './constants';
15+
16+
const FIRST_BUTTON = '0';
17+
const MIDDLE_BUTTON = `${Math.floor(NUM_BUTTONS / 2)}`;
18+
const LAST_BUTTON = `${NUM_BUTTONS - 1}`;
19+
20+
describe('baseline tests for interacting with the page through Protractor directly', () => {
21+
beforeEach(async () => {
22+
await browser.get('');
23+
});
24+
25+
it('(baseline) should retrieve all of the buttons', async () => {
26+
await benchmark('(baseline) get every button', async () => {
27+
await $$('.mat-button');
28+
});
29+
});
30+
31+
it('(baseline) should click the first button', async () => {
32+
await benchmark('(baseline) click first button', async () => {
33+
await element(by.buttonText(FIRST_BUTTON)).click();
34+
});
35+
});
36+
37+
it('(baseline) should click the middle button', async () => {
38+
await benchmark('(baseline) click middle button', async () => {
39+
await element(by.buttonText(MIDDLE_BUTTON)).click();
40+
});
41+
});
42+
43+
it('(baseline) should click the last button', async () => {
44+
await benchmark('(baseline) click last button', async () => {
45+
await element(by.buttonText(LAST_BUTTON)).click();
46+
});
47+
});
48+
49+
it('(baseline) should click all of the buttons', async () => {
50+
await benchmark('(baseline) click every button', async () => {
51+
const buttons = $$('.mat-button');
52+
await buttons.each(async button => await button!.click());
53+
});
54+
});
55+
});
56+
57+
describe('performance tests for the protractor button harness', () => {
58+
let loader: HarnessLoader;
59+
60+
beforeEach(async () => {
61+
await browser.get('');
62+
loader = ProtractorHarnessEnvironment.loader();
63+
});
64+
65+
it('should retrieve all of the buttons', async () => {
66+
await benchmark('get every button', async () => {
67+
await loader.getAllHarnesses(MatButtonHarness);
68+
});
69+
});
70+
71+
it('should click the first button', async () => {
72+
await benchmark('click first button', async () => {
73+
const button = await loader.getHarness(MatButtonHarness.with({text: FIRST_BUTTON}));
74+
await button.click();
75+
});
76+
});
77+
78+
it('should click the middle button', async () => {
79+
await benchmark('click middle button', async () => {
80+
const button = await loader.getHarness(MatButtonHarness.with({text: MIDDLE_BUTTON}));
81+
await button.click();
82+
});
83+
});
84+
85+
it('should click the last button', async () => {
86+
await benchmark('click last button', async () => {
87+
const button = await loader.getHarness(MatButtonHarness.with({text: LAST_BUTTON}));
88+
await button.click();
89+
});
90+
});
91+
92+
it('should click all of the buttons', async () => {
93+
await benchmark('click every button', async () => {
94+
const buttons = await loader.getAllHarnesses(MatButtonHarness);
95+
for (let i = 0; i < buttons.length; i++) {
96+
const button = buttons[i];
97+
await button.click();
98+
}
99+
});
100+
});
101+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
/**
10+
* Measures the time it takes to invoke the given function and prints the duration to the console.
11+
* @param id A unique identifier for the callback being measured.
12+
* @param callback A function whose performance will be recorded.
13+
* @param runs The number of times to run the callback.
14+
*/
15+
export async function benchmark(id: string, callback: () => Promise<unknown>, runs = 5) {
16+
// Currently karma does not send the logs from console.time to the server so console.time will
17+
// not show. As an alternative, we use performance.now()
18+
const t0 = performance.now();
19+
for (let i = 0; i < runs; i++) {
20+
await callback();
21+
}
22+
const t1 = performance.now();
23+
console.warn(`${id}: ${((t1 - t0) / runs).toFixed(2)}ms (avg over ${runs} runs)`);
24+
}

0 commit comments

Comments
 (0)