From d38c768e91928e40a7eef074f505770f5abd8760 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pekka=20Hyv=C3=B6nen?= Date: Tue, 26 Oct 2021 12:32:09 +0300 Subject: [PATCH] test: adjust test and add measurements The change improves the server side roundtrip processing time for 2000 buttons with click listeners from roughtly 210ms to 130ms (-40%) by not eagerly creating the id using MessageDigest after evaluating each event data expression. Difference to buttons without any click listeners is still about x2 (65ms->130ms). --- .../vaadin/flow/internal/ConstantPoolKey.java | 6 ++--- .../flow/component/ComponentEventBusTest.java | 7 +++-- .../flow/internal/ConstantPoolTest.java | 9 +++++++ .../ui/ConstantPoolPerformanceView.java | 27 ++++++++++++------- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/internal/ConstantPoolKey.java b/flow-server/src/main/java/com/vaadin/flow/internal/ConstantPoolKey.java index c9c3dd46ec0..5b04b396fd8 100644 --- a/flow-server/src/main/java/com/vaadin/flow/internal/ConstantPoolKey.java +++ b/flow-server/src/main/java/com/vaadin/flow/internal/ConstantPoolKey.java @@ -68,9 +68,9 @@ public String getId() { } /** - * Exports this key into a JSON object to send to the client. This - * method should be called only by the {@link ConstantPool} instance that - * manages this value. It may be called multiple times. + * Exports this key into a JSON object to send to the client. This method + * should be called only by the {@link ConstantPool} instance that manages + * this value. It may be called multiple times. * * @param clientConstantPoolUpdate * the constant pool update that is to be sent to the client, not diff --git a/flow-server/src/test/java/com/vaadin/flow/component/ComponentEventBusTest.java b/flow-server/src/test/java/com/vaadin/flow/component/ComponentEventBusTest.java index 240086352a9..8134616b699 100644 --- a/flow-server/src/test/java/com/vaadin/flow/component/ComponentEventBusTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/component/ComponentEventBusTest.java @@ -494,8 +494,11 @@ public void eventUnregisterListener_outsideListenerTwiceThrows() { @Test // #7826 public void addListener_eventDataExpressionsPresent_constantPoolKeyNotCreatedAfterEachExpression() { final TestButton button = new TestButton(); - try (MockedStatic util = Mockito.mockStatic(MessageDigestUtil.class)){ - util.when(() -> MessageDigestUtil.sha256(Mockito.anyString())).thenReturn(new byte[]{1,1,1,1,1,1,1,1,1,1,1,}); + try (MockedStatic util = Mockito + .mockStatic(MessageDigestUtil.class)) { + util.when(() -> MessageDigestUtil.sha256(Mockito.anyString())) + .thenReturn( + new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, }); button.addClickListener(event -> { }); util.verifyNoInteractions(); diff --git a/flow-server/src/test/java/com/vaadin/flow/internal/ConstantPoolTest.java b/flow-server/src/test/java/com/vaadin/flow/internal/ConstantPoolTest.java index 043271ac9d6..2ad1756c389 100644 --- a/flow-server/src/test/java/com/vaadin/flow/internal/ConstantPoolTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/internal/ConstantPoolTest.java @@ -74,4 +74,13 @@ public void differentValue_differentId() { Assert.assertNotEquals(constantId, otherId); Assert.assertTrue(constantPool.hasNewConstants()); } + + @Test + public void constantPoolKey_exportedDirectly_idCreated() { + final ConstantPoolKey constantPoolKey = new ConstantPoolKey( + Json.createObject()); + final JsonObject message = Json.createObject(); + constantPoolKey.export(message); + Assert.assertTrue(message.hasKey(constantPoolKey.getId())); + } } diff --git a/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/ConstantPoolPerformanceView.java b/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/ConstantPoolPerformanceView.java index 1d8afbcd0ad..00102a46bea 100644 --- a/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/ConstantPoolPerformanceView.java +++ b/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/ConstantPoolPerformanceView.java @@ -16,28 +16,37 @@ package com.vaadin.flow.uitest.ui; -import com.vaadin.flow.component.UI; import com.vaadin.flow.component.html.Div; -import com.vaadin.flow.component.html.H1; import com.vaadin.flow.component.html.NativeButton; import com.vaadin.flow.router.Route; +/* Manual test case for #7826 */ @Route("performance/constant-pool") public class ConstantPoolPerformanceView extends AbstractDivView { Div container = new Div(); Div notification = new Div(); + public ConstantPoolPerformanceView() { - NativeButton btn = new NativeButton("refresh"); - btn.addClickListener(e->{ + NativeButton btn = new NativeButton( + "add 2k divs without click listener"); + btn.addClickListener(e -> { + container.removeAll(); + for (int i = 0; i < 2000; i++) { + container.add(new NativeButton("No click listener ")); + } + }); + NativeButton btn2 = new NativeButton("add 2k divs with click listener"); + btn2.addClickListener(e -> { container.removeAll(); - for (int i = 0; i <1000; i++) { - container.add(new H1("Headline " + i)); - container.add(new NativeButton("BTN " + i, e2 -> notification.setText("clicked"))); + for (int i = 0; i < 2000; i++) { + container.add(new NativeButton("With click listener " + i, + e2 -> notification.setText("clicked"))); } - UI.getCurrent().getPage().executeJs("setTimeout(function(){$0.click()},2000)", btn); }); - add(btn,notification,container); + final NativeButton clearButtons = new NativeButton("clear buttons", + e -> container.removeAll()); + add(btn, btn2, clearButtons, notification, container); } }