Skip to content

Commit 1fa517f

Browse files
committed
fixup! test(button-harness): add performance tests for buttons using the protractor harness env
1 parent 21b528c commit 1fa517f

File tree

9 files changed

+365
-109
lines changed

9 files changed

+365
-109
lines changed

src/cdk/testing/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ts_library(
1111
"**/*.spec.ts",
1212
],
1313
),
14+
deps = ["//test/benchmarks/material/button-harness:protractor-benchmark-utilities"],
1415
module_name = "@angular/cdk/testing",
1516
)
1617

src/cdk/testing/harness-environment.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
LocatorFnResult,
1818
} from './component-harness';
1919
import {TestElement} from './test-element';
20+
import {benchmark} from '../../../test/benchmarks/material/button-harness/protractor-benchmark-utilities';
2021

2122
/** Parsed form of the queries passed to the `locatorFor*` methods. */
2223
type ParsedQueries<T extends ComponentHarness> = {
@@ -58,9 +59,21 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
5859
// Implemented as part of the `LocatorFactory` interface.
5960
locatorFor<T extends (HarnessQuery<any> | string)[]>(...queries: T):
6061
AsyncFactoryFn<LocatorFnResult<T>> {
61-
return () => _assertResultFound(
62-
this._getAllHarnessesAndTestElements(queries),
63-
_getDescriptionForLocatorForQueries(queries));
62+
return async () => {
63+
let allHarnessesAndTestElements: any;
64+
let descriptionForLocatorForQueries: any;
65+
let result: any;
66+
await benchmark('_getAllHarnessesAndTestElements', async () => {
67+
allHarnessesAndTestElements = await this._getAllHarnessesAndTestElements(queries);
68+
});
69+
await benchmark('_getDescriptionForLocatorForQueries', async () => {
70+
descriptionForLocatorForQueries = _getDescriptionForLocatorForQueries(queries);
71+
});
72+
await benchmark('_assertResultFound', async () => {
73+
result = await _assertResultFound(allHarnessesAndTestElements, descriptionForLocatorForQueries);
74+
});
75+
return result;
76+
}
6477
}
6578

6679
// Implemented as part of the `LocatorFactory` interface.
Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,56 @@
11
load("@npm_angular_dev_infra_private//benchmark/component_benchmark:component_benchmark.bzl", "component_benchmark")
2+
load("//tools:defaults.bzl", "ng_test_library", "ng_web_test_suite", "ts_library")
23

3-
# TODO(wagnermaciel): Update this target to provide indigo-pink in a way that doesn't require having to import it with
4-
# stylesUrls inside the components once `component_benchmark` supports asset injection.
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+
# I am not sure why the testbed version of the test does not log console.time to the terminal,
10+
# although I assume it has something to do with the default karma config that is generated by
11+
# karma_web_test_suite (which is wrapped by ng_web_test_suite).
12+
#
13+
# I am equally lost as to why the performance api exists for the testbed tests but not for the
14+
# protractor tests.
15+
#
16+
# If you have any idea why these things happen, please let me know - @wagnermaciel.
17+
18+
ts_library(
19+
name = "constants",
20+
srcs = [":constants.ts"],
21+
)
22+
23+
ts_library(
24+
name = "protractor-benchmark-utilities",
25+
srcs = [":protractor-benchmark-utilities.ts"],
26+
deps = [
27+
":constants",
28+
"@npm//@angular/dev-infra-private",
29+
],
30+
)
31+
32+
ts_library(
33+
name = "testbed-benchmark-utilities",
34+
srcs = [":testbed-benchmark-utilities.ts"],
35+
deps = [":constants"],
36+
)
37+
38+
# ProtractorHarnessEnvironment
539

640
component_benchmark(
741
name = "benchmark",
842
driver = ":protractor.perf-spec.ts",
943
driver_deps = [
10-
"@npm//@angular/dev-infra-private",
44+
":constants",
45+
":protractor-benchmark-utilities",
1146
"@npm//protractor",
1247
"@npm//@types/jasmine",
1348
"//src/cdk/testing",
1449
"//src/material/button/testing",
1550
"//src/cdk/testing/protractor",
1651
],
1752
ng_deps = [
53+
":constants",
1854
"@npm//@angular/core",
1955
"@npm//@angular/platform-browser",
2056
"//src/material/button",
@@ -23,3 +59,24 @@ component_benchmark(
2359
prefix = "",
2460
styles = ["//src/material/prebuilt-themes:indigo-pink"],
2561
)
62+
63+
# TestbedHarnessEnvironment
64+
65+
ng_test_library(
66+
name = "unit-test-lib",
67+
srcs = ["testbed.perf-spec.ts"],
68+
deps = [
69+
":constants",
70+
":testbed-benchmark-utilities",
71+
"//src/material/button/testing",
72+
"//src/cdk/testing",
73+
"//src/cdk/testing/testbed",
74+
"//src/material/button",
75+
"@npm//@angular/dev-infra-private",
76+
],
77+
)
78+
79+
ng_web_test_suite(
80+
name = "harness-unit-tests",
81+
deps = [":unit-test-lib"],
82+
)

test/benchmarks/material/button-harness/app.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import {Component, NgModule, ViewEncapsulation} from '@angular/core';
1010
import {BrowserModule} from '@angular/platform-browser';
1111
import {MatButtonModule} from '@angular/material/button';
12+
import {NUM_BUTTONS} from './constants';
1213

1314
/** component: mat-raised-button-harness-test */
1415

@@ -21,7 +22,7 @@ import {MatButtonModule} from '@angular/material/button';
2122
styleUrls: ['//src/material/core/theming/prebuilt/indigo-pink.css'],
2223
})
2324
export class ButtonHarnessTest {
24-
vals = Array.from({ length: 25 }, (_, i) => i);
25+
vals = Array.from({ length: NUM_BUTTONS }, (_, i) => i);
2526
}
2627

2728
@NgModule({
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
export const USE_BENCHPRESS = false;
10+
export const NUM_BUTTONS = 5;
11+
export const FIRST_BUTTON = '0';
12+
export const MIDDLE_BUTTON = `${Math.floor(NUM_BUTTONS / 2)}`;
13+
export const LAST_BUTTON = `${NUM_BUTTONS - 1}`;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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/dev-infra-private/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: Function) {
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+
* @param id
30+
* @param callback
31+
*/
32+
async function benchmarkWithBenchpress(id: string, callback: Function) {
33+
await runBenchmark({
34+
id,
35+
url: '',
36+
ignoreBrowserSynchronization: true,
37+
params: [],
38+
work: async () => await callback(),
39+
});
40+
}
41+
42+
/**
43+
* Uses console.time with the given id to record the time it takes for the given callback to run.
44+
*
45+
* @param id
46+
* @param callback
47+
*/
48+
async function benchmarkWithConsoleAPI(id: string, callback: Function) {
49+
console.time(id);
50+
await callback();
51+
console.timeEnd(id);
52+
}

0 commit comments

Comments
 (0)