Skip to content
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

8252935: Add treeShowing listener only when needed #185

Closed
@@ -127,14 +127,13 @@ public ProgressIndicatorSkin(ProgressIndicator control) {
super(control);

this.control = control;
this.treeShowingExpression = new TreeShowingExpression(control);

// register listeners
registerChangeListener(control.indeterminateProperty(), e -> initialize());
registerChangeListener(control.progressProperty(), e -> updateProgress());
registerChangeListener(control.sceneProperty(), e->updateAnimation());

treeShowingExpression = new TreeShowingExpression(control);
treeShowingExpression.addListener((obs, old, current) -> updateAnimation());
registerChangeListener(treeShowingExpression, e -> updateAnimation());

initialize();
updateAnimation();
@@ -0,0 +1,166 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
hjohn marked this conversation as resolved.
Show resolved Hide resolved
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package test.javafx.scene;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import test.util.Util;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import static org.junit.Assert.assertTrue;

/**
* TODO fix
* This test is based on the test case reported in JDK-8209830
hjohn marked this conversation as resolved.
Show resolved Hide resolved
*
* Redundant CSS Re-application was avoided in JDK-8193445.
* It results in faster application of CSS on Controls (Nodes). In turn,
* resulting in improved Node creation/addition time to a Scene.
*
* The goal of this test is *NOT* to measure absolute performance, but to show
* creating and adding 500 Nodes to a scene does not take more than a
* particular threshold of time.
*
* The selected thresold is larger than actual observed time.
* It is not a benchmark value. It is good enough to catch the regression
* in performance, if any.
*/

public class NodeTreeShowingTest {

private static CountDownLatch startupLatch;
private static Stage stage;
private static BorderPane rootPane;

public static class TestApp extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
stage = primaryStage;
rootPane = new BorderPane();
stage.setScene(new Scene(rootPane));
stage.addEventHandler(WindowEvent.WINDOW_SHOWN, e -> {
Platform.runLater(() -> startupLatch.countDown());
});
stage.show();
}
}

@BeforeClass
public static void initFX() throws Exception {
startupLatch = new CountDownLatch(1);
new Thread(() -> Application.launch(NodeTreeShowingTest.TestApp.class, (String[]) null)).start();

assertTrue("Timeout waiting for FX runtime to start", startupLatch.await(15, TimeUnit.SECONDS));
}

private StackPane createNodesRecursively(int count, int level) {
StackPane pane = new StackPane();

for (int i = 0; i < count; i++) {
pane.getChildren().add(level == 1 ? new StackPane() : createNodesRecursively(count, level - 1));
}

return pane;
}

/**
* This tests how quickly Nodes can be added and removed from a very large Scene. Specifically,
* this test was created to ensure that not every Node in the Scene creates a listener on its
* Scene (and/or its associated Window). With a large amount of Nodes in a Scene the listener
* lists of the associated Scene and/or Window can become very large and adding or removing
* a Node takes a performance hit.
*/
@Test
public void testAddRemovalSpeedInHugeScene() throws Exception {
Random rnd = new Random(0); // seed random to keep it predictable
int loopCount = 10000;
int levels = 13;
int nodesPerLevel = 2; // total nodes is (nodesPerLevel ^ levels) * 2 - 1
int leafCount = (int)Math.pow(nodesPerLevel, levels);
int total = leafCount * 2 - 1;
StackPane testNode = new StackPane();
StackPane root = createNodesRecursively(nodesPerLevel, levels);
AtomicLong bestMillis = new AtomicLong(Long.MAX_VALUE);

Util.runAndWait(() -> {
rootPane.setCenter(root);
});

for(int j = 0; j < 5; j++) {
hjohn marked this conversation as resolved.
Show resolved Hide resolved
int loopNumber = j + 1;

Util.runAndWait(() -> {
// Compute time it takes to add/remove Nodes at random spots
long startTime = System.currentTimeMillis();

for (int i = 0; i < loopCount; i++) {
// Find a random leaf to remove/re-add a child:
int index = rnd.nextInt(leafCount);
StackPane current = root;

while (index >= nodesPerLevel) {
current = (StackPane) current.getChildren().get(index % nodesPerLevel);
index /= nodesPerLevel;
}

current.getChildren().add(current.getChildren().remove(index));
}

long endTime = System.currentTimeMillis();

bestMillis.set(Math.min(endTime - startTime, bestMillis.get()));

System.out.println("Loop " + loopNumber + ": Time to add/remove " + loopCount + " nodes in "
+ "a Scene consisting of " + total + " nodes = " + (endTime - startTime) + " mSec");
});
}

// NOTE : 800 mSec is not a benchmark value
// It is good enough to catch the regression in performance, if any
assertTrue("Time to add/remove " + loopCount + " nodes in a large Scene is more than 800 mSec", bestMillis.get() <= 800);
}

@AfterClass
public static void teardownOnce() {
Platform.runLater(() -> {
stage.hide();
Platform.exit();
});
}
}