Skip to content

test(mdc-menu): add performance tests for mdc-menu #20494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions test/benchmarks/material/menu/menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@
<button mat-button [matMenuTriggerFor]="nested">Nested Menu</button>

<mat-menu #basic="matMenu">
<button mat-menu-item>Item 1</button>
<button mat-menu-item>Item 2</button>
<button mat-menu-item>Item 3</button>
<button mat-menu-item>Item 4</button>
<button mat-menu-item>Item 5</button>
<button mat-menu-item>Item 6</button>
<button mat-menu-item>Item 7</button>
<button mat-menu-item>Item 8</button>
<button mat-menu-item>Item 9</button>
<button mat-menu-item>Item 10</button>
<button mat-menu-item>Item 1</button>
<button mat-menu-item>Item 2</button>
<button mat-menu-item>Item 3</button>
<button mat-menu-item>Item 4</button>
<button mat-menu-item>Item 5</button>
<button mat-menu-item>Item 6</button>
<button mat-menu-item>Item 7</button>
<button mat-menu-item>Item 8</button>
<button mat-menu-item>Item 9</button>
<button mat-menu-item>Item 10</button>
</mat-menu>

<!-- Nested Menu with 3 Levels -->

<mat-menu #nested="matMenu">
<button mat-menu-item [matMenuTriggerFor]="sub1">Sub Menu 1</button>
<button mat-menu-item [matMenuTriggerFor]="sub1">Sub Menu 1</button>
</mat-menu>

<mat-menu #sub1="matMenu">
<button mat-menu-item [matMenuTriggerFor]="sub2">Sub Menu 2</button>
<button mat-menu-item [matMenuTriggerFor]="sub2">Sub Menu 2</button>
</mat-menu>

<mat-menu #sub2="matMenu">
<button mat-menu-item>Item 1</button>
<button mat-menu-item>Item 2</button>
<button mat-menu-item>Item 3</button>
<button mat-menu-item>Item 4</button>
<button mat-menu-item>Item 5</button>
<button mat-menu-item>Item 1</button>
<button mat-menu-item>Item 2</button>
<button mat-menu-item>Item 3</button>
<button mat-menu-item>Item 4</button>
<button mat-menu-item>Item 5</button>
</mat-menu>
24 changes: 15 additions & 9 deletions test/benchmarks/material/menu/menu.perf-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,50 @@ async function closeMenu(trigger: ElementFinder) {
}

describe('menu performance benchmarks', () => {
it('opens a basic menu', async () => {
it('opens and closes a basic menu', async () => {
let trigger: ElementFinder;
await runBenchmark({
id: 'basic-menu-open',
id: 'open-and-close-basic-menu',
url: '',
ignoreBrowserSynchronization: true,
params: [],
setup: async () => trigger = element(by.buttonText('Basic Menu')),
setup: async () => {
trigger = element(by.buttonText('Basic Menu'));
},
work: async () => {
await trigger.click();
await closeMenu(trigger);
}
});
});

it('opens the root menu of a set of nested menus', async () => {
it('opens and closes the root menu of a set of nested menus', async () => {
let trigger: ElementFinder;
await runBenchmark({
id: 'nested-menu-open-shallow',
id: 'shallow-open-and-close-nested-menu',
url: '',
ignoreBrowserSynchronization: true,
params: [],
setup: async () => trigger = element(by.buttonText('Nested Menu')),
setup: async () => {
trigger = element(by.buttonText('Nested Menu'));
},
work: async () => {
await trigger.click();
await closeMenu(trigger);
},
});
});

it('fully opens a menu with nested menus', async () => {
it('fully opens and closes a menu with nested menus', async () => {
let trigger: ElementFinder;
await runBenchmark({
id: 'menu-open-deep',
id: 'deep-open-and-close-nested-menus',
url: '',
ignoreBrowserSynchronization: true,
params: [],
setup: async () => trigger = element(by.buttonText('Nested Menu')),
setup: () => {
trigger = element(by.buttonText('Nested Menu'));
},
work: async () => {
await trigger.click();
await element(by.buttonText('Sub Menu 1')).click();
Expand Down
23 changes: 23 additions & 0 deletions test/benchmarks/mdc/menu/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@npm_angular_dev_infra_private//benchmark/component_benchmark:component_benchmark.bzl", "component_benchmark")

# TODO(wagnermaciel): Update this target to provide indigo-pink in a way that doesn't require having to import it with
# stylesUrls inside the components once `component_benchmark` supports asset injection.

component_benchmark(
name = "benchmark",
driver = ":menu.perf-spec.ts",
driver_deps = [
"@npm//@angular/dev-infra-private",
"@npm//protractor",
"@npm//@types/jasmine",
],
ng_assets = [":menu.html"],
ng_deps = [
"@npm//@angular/core",
"@npm//@angular/platform-browser",
"//src/material-experimental/mdc-menu",
],
ng_srcs = [":app.module.ts"],
prefix = "",
styles = ["//src/material-experimental/mdc-theming:indigo_pink_prebuilt"],
)
33 changes: 33 additions & 0 deletions test/benchmarks/mdc/menu/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @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, ViewEncapsulation} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {MatMenuModule} from '@angular/material-experimental/mdc-menu';

/** component: mdc-menu */

@Component({
selector: 'app-root',
templateUrl: './menu.html',
encapsulation: ViewEncapsulation.None,
styleUrls: ['//src/material-experimental/mdc-theming/prebuilt/indigo-pink.css'],
})
export class MenuBenchmarkApp {
}


@NgModule({
declarations: [MenuBenchmarkApp],
imports: [
BrowserModule,
MatMenuModule,
],
bootstrap: [MenuBenchmarkApp]
})
export class AppModule {}
33 changes: 33 additions & 0 deletions test/benchmarks/mdc/menu/menu.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<button mat-button [matMenuTriggerFor]="basic">Basic Menu</button>
<button mat-button [matMenuTriggerFor]="nested">Nested Menu</button>

<mat-menu #basic="matMenu">
<button mat-menu-item>Item 1</button>
<button mat-menu-item>Item 2</button>
<button mat-menu-item>Item 3</button>
<button mat-menu-item>Item 4</button>
<button mat-menu-item>Item 5</button>
<button mat-menu-item>Item 6</button>
<button mat-menu-item>Item 7</button>
<button mat-menu-item>Item 8</button>
<button mat-menu-item>Item 9</button>
<button mat-menu-item>Item 10</button>
</mat-menu>

<!-- Nested Menu with 3 Levels -->

<mat-menu #nested="matMenu">
<button mat-menu-item [matMenuTriggerFor]="sub1">Sub Menu 1</button>
</mat-menu>

<mat-menu #sub1="matMenu">
<button mat-menu-item [matMenuTriggerFor]="sub2">Sub Menu 2</button>
</mat-menu>

<mat-menu #sub2="matMenu">
<button mat-menu-item>Item 1</button>
<button mat-menu-item>Item 2</button>
<button mat-menu-item>Item 3</button>
<button mat-menu-item>Item 4</button>
<button mat-menu-item>Item 5</button>
</mat-menu>
75 changes: 75 additions & 0 deletions test/benchmarks/mdc/menu/menu.perf-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @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 {$, by, element, ElementFinder, Key} from 'protractor';
import {runBenchmark} from '@angular/dev-infra-private/benchmark/driver-utilities';

// Clicking to close a menu is problematic. This is a solution that uses `.sendKeys()` avoids
// issues with `.click()`.

async function closeMenu(trigger: ElementFinder) {
const backdropId = await trigger.getAttribute('aria-controls');
if (await $(`#${backdropId}`).isPresent()) {
await $(`#${backdropId}`).sendKeys(Key.ESCAPE);
}
}

describe('menu performance benchmarks', () => {
it('opens and closes a basic menu', async () => {
let trigger: ElementFinder;
await runBenchmark({
id: 'open-and-close-basic-menu',
url: '',
ignoreBrowserSynchronization: true,
params: [],
setup: async () => {
trigger = element(by.buttonText('Basic Menu'));
},
Copy link
Member

@devversion devversion Sep 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a note for the dev-infra helper: We should remove the setup property from the runBenchmark function and just do the setup before. e.g. you could just directly assign the trigger variable based on what I can tell.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean inside the work callback?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I just meant before calling runBenchmark.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The browser is not open until runBenchmark is called.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC the browser is always open with protractor, it just is on an empty page on initialization. So ideally IMO, the test would manually call await browser.open(<..>) before and then do all preparation work before starting with the samples.

The reason why I'm proposing this is that setup and prepare are very unclear and ambiguous IMO, and as it looks, it's also complicating the logic for preparations (as seen with above for finding the trigger). I don't feel very strongly about it. Changing the name of these properties might help too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing runBenchmark to not call openBrowser would require fixing all the old tests in angular/angular, too. Same with renaming setup, prepare, and work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's clear to me. Though I think that these usages should ideally not prevent us from maintaining good health and readable code. I mentioned this here as a side note, but I think we should do something in the future.

For the record: I'd be happy to go through the instances in the future if needed. We could also add a separate helper that has the less ambiguous API. All of that is obviously not a priority right now but something to keep in mind since we keep adding new benchmarks and spread this across repositories.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

work: async () => {
await trigger.click();
await closeMenu(trigger);
}
});
});

it('opens and closes the root menu of a set of nested menus', async () => {
let trigger: ElementFinder;
await runBenchmark({
id: 'shallow-open-and-close-nested-menu',
url: '',
ignoreBrowserSynchronization: true,
params: [],
setup: async () => {
trigger = element(by.buttonText('Nested Menu'));
},
work: async () => {
await trigger.click();
await closeMenu(trigger);
},
});
});

it('fully opens and closes a menu with nested menus', async () => {
let trigger: ElementFinder;
await runBenchmark({
id: 'deep-open-and-close-nested-menus',
url: '',
ignoreBrowserSynchronization: true,
params: [],
setup: async () => {
trigger = element(by.buttonText('Nested Menu'));
},
work: async () => {
await trigger.click();
await element(by.buttonText('Sub Menu 1')).click();
await element(by.buttonText('Sub Menu 2')).click();
await closeMenu(trigger);
},
});
});
});