Skip to content

Commit eb8d037

Browse files
committed
change some things
1 parent 1fa517f commit eb8d037

File tree

5 files changed

+74
-113
lines changed

5 files changed

+74
-113
lines changed

src/cdk/testing/BUILD.bazel

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

src/cdk/testing/harness-environment.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ 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';
2120

2221
/** Parsed form of the queries passed to the `locatorFor*` methods. */
2322
type ParsedQueries<T extends ComponentHarness> = {
@@ -59,21 +58,9 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
5958
// Implemented as part of the `LocatorFactory` interface.
6059
locatorFor<T extends (HarnessQuery<any> | string)[]>(...queries: T):
6160
AsyncFactoryFn<LocatorFnResult<T>> {
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-
}
61+
return () => _assertResultFound(
62+
this._getAllHarnessesAndTestElements(queries),
63+
_getDescriptionForLocatorForQueries(queries));
7764
}
7865

7966
// Implemented as part of the `LocatorFactory` interface.

test/benchmarks/material/button-harness/constants.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,5 @@
77
*/
88

99
export const USE_BENCHPRESS = false;
10-
export const NUM_BUTTONS = 5;
11-
export const FIRST_BUTTON = '0';
10+
export const NUM_BUTTONS = 1000;
1211
export const MIDDLE_BUTTON = `${Math.floor(NUM_BUTTONS / 2)}`;
13-
export const LAST_BUTTON = `${NUM_BUTTONS - 1}`;

test/benchmarks/material/button-harness/testbed-benchmark-utilities.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,16 @@
77
*/
88

99
/**
10-
* Records the performance of the given function.
11-
*
12-
* @param id A unique identifier.
13-
* @param callback A function whose performance will be recorded.
14-
*/
15-
export async function benchmark(id: string, callback: Function) {
16-
const t0 = performance.now();
17-
await callback();
10+
* Records the performance of the given function.
11+
* @param id A unique identifier.
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+
const t0 = performance.now();
17+
for (let i = 0; i < runs; i++) {
18+
await callback();
19+
}
1820
const t1 = performance.now();
19-
console.warn(`${id}: ${t1 - t0}`);
20-
}
21-
22-
export function getButtonWithText(text: string): HTMLButtonElement {
23-
const xpathExpression = `//button[//span[text()=' ${text} ']]`;
24-
const xPathResult = document.evaluate(
25-
xpathExpression,
26-
document,
27-
null,
28-
XPathResult.FIRST_ORDERED_NODE_TYPE,
29-
null
30-
);
31-
return xPathResult.singleNodeValue as HTMLButtonElement;
21+
console.warn(`${id}: ${((t1 - t0) / runs).toFixed(2)}ms (avg over ${runs} runs)`);
3222
}

test/benchmarks/material/button-harness/testbed.perf-spec.ts

Lines changed: 59 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ import {Component} from '@angular/core';
1212
import {ComponentFixture, TestBed} from '@angular/core/testing';
1313
import {MatButtonModule} from '@angular/material/button';
1414
import {MatButtonHarness} from '@angular/material/button/testing/button-harness';
15-
import {benchmark, getButtonWithText} from './testbed-benchmark-utilities';
16-
import {FIRST_BUTTON, MIDDLE_BUTTON, NUM_BUTTONS, LAST_BUTTON} from './constants';
15+
import {MIDDLE_BUTTON, NUM_BUTTONS} from './constants';
16+
import {benchmark} from './testbed-benchmark-utilities';
1717

18-
describe('performance baseline for the testbed harness', () => {
18+
describe('performance for the testbed harness environment', () => {
1919
let fixture: ComponentFixture<ButtonHarnessTest>;
20+
let loader: HarnessLoader;
21+
22+
beforeAll(() => {
23+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 36000000;
24+
});
2025

2126
beforeEach(async () => {
2227
await TestBed.configureTestingModule({
@@ -26,99 +31,81 @@ describe('performance baseline for the testbed harness', () => {
2631

2732
fixture = TestBed.createComponent(ButtonHarnessTest);
2833
fixture.detectChanges();
34+
loader = TestbedHarnessEnvironment.loader(fixture);
2935
});
3036

31-
it('(baseline) should retrieve all of the buttons', async () => {
32-
await benchmark('(baseline) get every button', async () => {
33-
document.querySelectorAll('button');
37+
describe('(baseline)', () => {
38+
it('should find a button', async () => {
39+
await benchmark('(baseline) find a button', async () => {
40+
document.querySelector('button');
41+
});
3442
});
35-
});
3643

37-
it('(baseline) should click the first button', async () => {
38-
await benchmark('(baseline) click first button', async () => {
39-
const button = getButtonWithText(FIRST_BUTTON);
40-
button.click();
44+
it('should find all buttons', async () => {
45+
await benchmark('(baseline) find all buttons', async () => {
46+
document.querySelectorAll('button');
47+
});
4148
});
42-
});
4349

44-
it('(baseline) should click the middle button', async () => {
45-
await benchmark('(baseline) click middle button', async () => {
46-
const button = getButtonWithText(MIDDLE_BUTTON);
47-
await button.click();
50+
it('should find a button via text filter', async () => {
51+
await benchmark('(baseline) find a button via text filter', async () => {
52+
return Array.from(document.querySelectorAll('button'))
53+
.filter(b => b.innerText === MIDDLE_BUTTON);
54+
});
4855
});
49-
});
5056

51-
it('(baseline) should click the last button', async () => {
52-
await benchmark('(baseline) click last button', async () => {
53-
const button = getButtonWithText(LAST_BUTTON);
54-
await button.click();
57+
it('should click a button', async () => {
58+
const button = document.querySelector('button')!;
59+
await benchmark('(baseline) click a button', async () => {
60+
button.click();
61+
fixture.detectChanges();
62+
});
5563
});
56-
});
5764

58-
it('(baseline) should click all of the buttons', async () => {
59-
await benchmark('(baseline) click every button', async () => {
60-
const buttons = document.getElementsByTagName('button');
61-
for (let i = 0; i < buttons.length; i++) {
62-
const button = buttons[i];
63-
await button.click();
64-
}
65+
it('should click all buttons', async () => {
66+
const buttons = Array.prototype.slice.call(document.querySelectorAll('button'));
67+
await benchmark('(baseline) click all buttons', async () => {
68+
buttons.forEach(button => button.click());
69+
fixture.detectChanges();
70+
});
6571
});
6672
});
67-
});
68-
69-
describe('performance tests for the testbed harness', () => {
70-
let fixture: ComponentFixture<ButtonHarnessTest>;
71-
let loader: HarnessLoader;
72-
73-
beforeEach(async () => {
74-
await TestBed.configureTestingModule({
75-
imports: [MatButtonModule],
76-
declarations: [ButtonHarnessTest],
77-
}).compileComponents();
78-
79-
fixture = TestBed.createComponent(ButtonHarnessTest);
80-
fixture.detectChanges();
81-
loader = TestbedHarnessEnvironment.loader(fixture);
82-
});
8373

84-
it('should retrieve all of the buttons', async () => {
85-
await benchmark('get every button', async () => {
86-
await loader.getAllHarnesses(MatButtonHarness);
74+
describe('(with harness)', () => {
75+
it('should find a button', async () => {
76+
await benchmark('(with harness) find a button', async () => {
77+
await loader.getHarness(MatButtonHarness);
78+
});
8779
});
88-
});
8980

90-
it('should click the first button', async () => {
91-
await benchmark('click first button', async () => {
92-
const button = await loader.getHarness(MatButtonHarness.with({text: FIRST_BUTTON}));
93-
await button.click();
81+
it('should find all buttons', async () => {
82+
await benchmark('(with harness) find all buttons', async () => {
83+
await loader.getAllHarnesses(MatButtonHarness);
84+
});
9485
});
95-
});
9686

97-
it('should click the middle button', async () => {
98-
await benchmark('click middle button', async () => {
99-
const button = await loader.getHarness(MatButtonHarness.with({text: MIDDLE_BUTTON}));
100-
await button.click();
87+
it('should find a button via text filter', async () => {
88+
await benchmark('(with harness) find a button via text filter', async () => {
89+
await loader.getAllHarnesses(MatButtonHarness.with({text: MIDDLE_BUTTON}));
90+
});
10191
});
102-
});
10392

104-
it('should click the last button', async () => {
105-
await benchmark('click last button', async () => {
106-
const button = await loader.getHarness(MatButtonHarness.with({text: LAST_BUTTON}));
107-
await button.click();
93+
it('should click a button', async () => {
94+
const button = await loader.getHarness(MatButtonHarness);
95+
await benchmark('(with harness) click a button', async () => {
96+
await button.click();
97+
});
10898
});
109-
});
11099

111-
it('should click all of the buttons', async () => {
112-
await benchmark('click every button', async () => {
100+
it('should click all buttons', async () => {
113101
const buttons = await loader.getAllHarnesses(MatButtonHarness);
114-
for (let i = 0; i < buttons.length; i++) {
115-
const button = buttons[i];
116-
await button.click();
117-
}
102+
await benchmark('(with harness) click all buttons', async () => {
103+
await Promise.all(buttons.map(button => button.click()));
104+
});
118105
});
119106
});
120107

121-
it('should fail intentionally', () => expect(1).toBe(2));
108+
it('should fail intentionally so performance numbers are logged', fail);
122109
});
123110

124111
@Component({
@@ -127,5 +114,5 @@ describe('performance tests for the testbed harness', () => {
127114
`,
128115
})
129116
export class ButtonHarnessTest {
130-
vals = Array.from({ length: NUM_BUTTONS }, (_, i) => i);
117+
vals = Array.from({ length: NUM_BUTTONS }, (_, i) => i);
131118
}

0 commit comments

Comments
 (0)