Skip to content

Commit

Permalink
Fix performance testing methods (#1012)
Browse files Browse the repository at this point in the history
Update Flow beta2 which contains poll() implementation
  • Loading branch information
Artur- committed Mar 5, 2018
1 parent 0ab2921 commit fed87a2
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ private static Logger getLogger() {
private boolean autoScrollIntoView = true;
// @formatter:off
String WAIT_FOR_VAADIN_SCRIPT =
"var vaadin = (window.Vaadin && window.Vaadin.Flow) ? window.Vaadin.Flow : window.vaadin;"
+ "if (vaadin == null) {"
"if (!window.Vaadin || !window.Vaadin.Flow) {"
+ " return true;"
+ "}"
+ "var clients = vaadin.clients;"
+ "var clients = window.Vaadin.Flow.clients;"
+ "if (clients) {"
+ " for (var client in clients) {"
+ " if (clients[client].isActive()) {"
Expand Down Expand Up @@ -197,22 +196,33 @@ public long totalTimeSpentServicingRequests() {
}

@SuppressWarnings("unchecked")
private List<Long> getTimingValues(boolean forceSync) {
// @formatter:off
String getProfilingData = "var pd = [0,0,0,0];\n"
+ "var clients = (window.Vaadin && window.Vaadin.Flow) ? window.Vaadin.Flow.clients : window.vaadin.clients;\n"
+ "for (client in clients) {\n"
+ " var p = clients[client].getProfilingData();\n"
+ " pd[0] += p[0];\n" + " pd[1] += p[1];\n"
+ " pd[2] += p[2];\n" + " pd[3] += p[3];\n" + "}\n"
+ "return pd;\n";
// @formatter:on
if (forceSync) {
// Force sync to get the latest server-side timing data. The
// server-side timing data is always one request behind.
executeScript("(window.Vaadin && window.Vaadin.Flow) ? window.Vaadin.Flow.forceSync() : window.vaadin.forceSync()");
private List<Long> getTimingValues(boolean poll) {
if (poll) {
// Get the latest server-side timing data.
// The server-side timing data is always one request behind.
executeScript("" //
+ "if (!window.Vaadin || !window.Vaadin.Flow || !window.Vaadin.Flow.clients) {"
+ " throw 'Performance data is only available when using Vaadin Flow';"
+ "}" //
+ "for (client in window.Vaadin.Flow.clients) {\n" //
+ " window.Vaadin.Flow.clients[client].poll();\n" + "}");
}
return (List<Long>) executeScript(getProfilingData);

return (List<Long>) executeScript("" //
+ "if (!window.Vaadin || !window.Vaadin.Flow || !window.Vaadin.Flow.clients) {"
+ " throw 'Performance data is only available when using Vaadin Flow';"
+ "}" //
+ "var pd = [0,0,0,0];\n" //
+ "for (client in window.Vaadin.Flow.clients) {\n"
+ " if (!window.Vaadin.Flow.clients[client].getProfilingData) {"
+ " throw 'Performance data is not available in production mode';"
+ " }" //
+ " var p = window.Vaadin.Flow.clients[client].getProfilingData();\n"
+ " pd[0] += p[0];\n" //
+ " pd[1] += p[1];\n"//
+ " pd[2] += p[2];\n" //
+ " pd[3] += p[3];\n" //
+ "}\n" + "return pd;\n");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ private WebDriver mockScreenshotDriver(int nrScreenshotsGrabbed,
"cursor-bottom-edge-off.png");
expect(driver.getScreenshotAs(OutputType.BYTES))
.andReturn(screenshotBytes).times(nrScreenshotsGrabbed);
expect(driver.executeScript(contains("(window.Vaadin && window.Vaadin.Flow) ? window.Vaadin.Flow : window.vaadin")))
expect(driver.executeScript(contains("window.Vaadin.Flow")))
.andReturn(Boolean.TRUE).anyTimes();
if (expectGetCapabilities) {
expect(driver.getCapabilities())
Expand Down Expand Up @@ -309,14 +309,10 @@ public void testTotalTimeSpentServicingRequests() {
}

private FirefoxDriver mockJSExecutor(boolean forcesSync) {
FirefoxDriver jse = createMock(FirefoxDriver.class);
if (forcesSync) {
expect(jse.executeScript("(window.Vaadin && window.Vaadin.Flow) ? window.Vaadin.Flow.forceSync() : window.vaadin.forceSync()"))
.andReturn(null);
}
FirefoxDriver jse = createNiceMock(FirefoxDriver.class);
expect(jse.executeScript(contains("getProfilingData()")))
.andReturn(Arrays.asList(1000L, 2000L, 3000L));
expect(jse.executeScript(contains("(window.Vaadin && window.Vaadin.Flow) ? window.Vaadin.Flow : window.vaadin")))
expect(jse.executeScript(contains("window.Vaadin.Flow.client")))
.andReturn(Boolean.TRUE).anyTimes();
return jse;
}
Expand Down
2 changes: 1 addition & 1 deletion vaadin-testbench-integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<url>http://maven.apache.org</url>
<properties>
<jetty.version>9.3.12.v20160915</jetty.version>
<flow.version>1.0.0.beta1</flow.version>
<flow.version>1.0.0.beta2</flow.version>

<!-- Frontend -->
<node.version>v8.1.2</node.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.vaadin.testUI;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.router.Route;

@Route("PerformanceView")
public class PerformanceView extends Div {

public PerformanceView() {
NativeButton button = new NativeButton("1s delay", e -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
add(new Span("Done sleeping"));
});
add(button);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.vaadin.tests;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import com.vaadin.flow.component.Component;
import com.vaadin.testUI.PerformanceView;
import com.vaadin.tests.elements.NativeButtonElement;

public class PerfomanceIT extends AbstractTB6Test {

@Override
protected Class<? extends Component> getTestView() {
return PerformanceView.class;
}

@Test
@Ignore("Profiling info not available in production mode until beta3")
public void serverTime() {
openTestURL();
$(NativeButtonElement.class).first().click();

Assert.assertEquals(1000.0, testBench().timeSpentServicingLastRequest(),
100.0);
$(NativeButtonElement.class).first().click();
Assert.assertEquals(2000.0,
testBench().totalTimeSpentServicingRequests(), 200.0);
}

@Test
@Ignore("Profiling info not available in production mode until beta3")
public void renderingTime() {
openTestURL();
long initialRendering = testBench().timeSpentRenderingLastRequest();
// Assuming initial rendering is done in 5-195ms
Assert.assertEquals(150, initialRendering, 145);
Assert.assertEquals(initialRendering,
testBench().totalTimeSpentRendering());
$(NativeButtonElement.class).first().click();
$(NativeButtonElement.class).first().click();
$(NativeButtonElement.class).first().click();

// Assuming rendering three poll responses is done in 50ms
Assert.assertTrue(
testBench().totalTimeSpentRendering() > initialRendering);
Assert.assertEquals(initialRendering,
testBench().totalTimeSpentRendering(), 50);
}

}

0 comments on commit fed87a2

Please sign in to comment.