Skip to content

test(menu): add performance tests for mat-menu #20151

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 4 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 26 additions & 0 deletions test/benchmarks/material/menu/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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",
"//src/cdk/testing",
"//src/cdk/testing/protractor",
"//src/material/menu/testing",
],
ng_deps = [
"@npm//@angular/core",
"@npm//@angular/platform-browser",
"//src/material/menu",
"//src/cdk/a11y",
],
ng_srcs = [":app.module.ts"],
prefix = "",
styles = ["//src/material/prebuilt-themes:indigo-pink"],
)
50 changes: 50 additions & 0 deletions test/benchmarks/material/menu/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @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 {A11yModule} from '@angular/cdk/a11y';
import {Component, NgModule, ViewEncapsulation} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {MatMenuModule} from '@angular/material/menu';

/** component: mat-menu */

@Component({
selector: 'app-root',
template: `
<button mat-button [matMenuTriggerFor]="menu">Menu</button>
<mat-menu #menu="matMenu">
Copy link
Member

Choose a reason for hiding this comment

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

We support multi-level menus too. Should we have a test that captures it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm I ran into a bit of an issue with this. The main things I wanted to test out for nested menus is 1. is there any slowdown with opening the root menu if there are a couple of nested menus, and 2. how long it takes to open a menu fully.

The problem (which I left a comment for) is that calling getHarness seems to be an expensive operation which makes figuring out how expensive opening up submenus are.

I am wondering if I should change the test from opening a nested menu fully to just opening a single nested menu like

setup: () => open root menu and call getHarness for the sub menu
prepare: () => close sub menu
work: () => open on sub menu

@crisbeto Lmk what are your thoughts

Copy link
Member

Choose a reason for hiding this comment

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

Harnesses trigger change detection and wait for it to finish before every action so I'm not sure whether we should be basing our performance tests on them. @mmalerba do you think that we should use Protractor directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using protractor has it's own issues with cdk-overlay-container

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the harnesses explicitly run change detection in e2e tests (though I'm sure they cause it by clicking). I think using the harness to open/close pretty much boils down to the same thing you would be doing via protractor directly anyways. The part where the harnesses might add some overhead is in searching for submenus, e.g. await loader.getHarness(MatMenuHarness.with({ triggerText: 'Sub Menu 1' })) - it would be ideal if we could pause measuring while doing this and then resume again when we go to actually click it, kind of like a "setup" step but it runs in the middle of the test. Not sure if that's possible though

Copy link
Member

Choose a reason for hiding this comment

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

Don't they also wait for animations for finish? @wagnermaciel it might be worth trying to import the NoopAnimationsModule.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was under the impression that we actually wanted to wait for animations to finish

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I think I figured out the correct solution here. The MatMenuHarness uses .sendKeys(Key.ESCAPE) to close it's menu. It does not call click on the .cdk-overlay-backdrop like I was trying to do. This avoids the issue I was running into while trying to use protractor for these tests, so no need to use harnesses here anymore.

<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>
`,
encapsulation: ViewEncapsulation.None,
styleUrls: ['//src/material/core/theming/prebuilt/indigo-pink.css'],
})
export class MenuBenchmarkApp {
}


@NgModule({
declarations: [MenuBenchmarkApp],
imports: [
A11yModule,
BrowserModule,
MatMenuModule,
],
providers: [],
bootstrap: [MenuBenchmarkApp]
})
export class AppModule {}
34 changes: 34 additions & 0 deletions test/benchmarks/material/menu/menu.perf-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @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 {runBenchmark} from '@angular/dev-infra-private/benchmark/driver-utilities';

import {HarnessLoader} from '@angular/cdk/testing';
import {ProtractorHarnessEnvironment} from '@angular/cdk/testing/protractor';
import {MatMenuHarness} from '@angular/material/menu/testing/menu-harness';

let loader: HarnessLoader;

describe('menu performance benchmarks', () => {
beforeEach(() => {
loader = ProtractorHarnessEnvironment.loader();
});

it('opens a menu with 10 items', async () => {
let menu: MatMenuHarness;
await runBenchmark({
id: 'menu-open',
url: '',
ignoreBrowserSynchronization: true,
params: [],
setup: async () => menu = await loader.getHarness(MatMenuHarness),
prepare: async () => await menu.close(),
work: async () => await menu.open(),
});
});
});