|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package test.javafx.scene; |
| 27 | + |
| 28 | +import javafx.application.Application; |
| 29 | +import javafx.application.Platform; |
| 30 | +import javafx.geometry.Insets; |
| 31 | +import javafx.scene.Scene; |
| 32 | +import javafx.scene.layout.HBox; |
| 33 | +import javafx.scene.layout.BorderPane; |
| 34 | +import javafx.scene.text.Text; |
| 35 | +import javafx.stage.Stage; |
| 36 | +import javafx.stage.WindowEvent; |
| 37 | + |
| 38 | +import java.util.concurrent.CountDownLatch; |
| 39 | +import java.util.concurrent.TimeUnit; |
| 40 | + |
| 41 | +import junit.framework.Assert; |
| 42 | +import org.junit.Test; |
| 43 | +import org.junit.AfterClass; |
| 44 | +import org.junit.BeforeClass; |
| 45 | +import static org.junit.Assert.assertTrue; |
| 46 | + |
| 47 | +/** |
| 48 | + * This test is based on the test case reported in JDK-8209830 |
| 49 | + * |
| 50 | + * Redundant CSS Re-application was avoided in JDK-8193445. |
| 51 | + * It results in faster application of CSS on Controls (Nodes). In turn, |
| 52 | + * resulting in improved Node creation/addition time to a Scene. |
| 53 | + * |
| 54 | + * The goal of this test is *NOT* to measure absolute performance, but to show |
| 55 | + * creating and adding 500 Nodes to a scene does not take more than a |
| 56 | + * particular threshold of time. |
| 57 | + * |
| 58 | + * The selected thresold is larger than actual observed time. |
| 59 | + * It is not a benchmark value. It is good enough to catch the regression |
| 60 | + * in performance, if any. |
| 61 | + */ |
| 62 | + |
| 63 | +public class QuadraticCssTimeTest extends Application { |
| 64 | + |
| 65 | + static private CountDownLatch startupLatch; |
| 66 | + static private Stage stage; |
| 67 | + private BorderPane rootPane = new BorderPane(); |
| 68 | + |
| 69 | + @Override |
| 70 | + public void start(Stage primaryStage) throws Exception { |
| 71 | + stage = primaryStage; |
| 72 | + rootPane = new BorderPane(); |
| 73 | + stage.setScene(new Scene(rootPane)); |
| 74 | + stage.addEventHandler(WindowEvent.WINDOW_SHOWN, e -> { |
| 75 | + Platform.runLater(() -> startupLatch.countDown()); |
| 76 | + }); |
| 77 | + stage.show(); |
| 78 | + } |
| 79 | + |
| 80 | + @BeforeClass |
| 81 | + public static void initFX() { |
| 82 | + startupLatch = new CountDownLatch(1); |
| 83 | + new Thread(() -> Application.launch(QuadraticCssTimeTest.class, (String[]) null)).start(); |
| 84 | + |
| 85 | + try { |
| 86 | + if (!startupLatch.await(15, TimeUnit.SECONDS)) { |
| 87 | + Assert.fail("Timeout waiting for FX runtime to start"); |
| 88 | + } |
| 89 | + } catch (InterruptedException ex) { |
| 90 | + Assert.fail("Unexpected exception: " + ex); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testTimeForAdding500NodesToScene() throws Exception { |
| 96 | + |
| 97 | + // Compute time for adding 500 Nodes |
| 98 | + long startTime = System.currentTimeMillis(); |
| 99 | + |
| 100 | + HBox hbox = new HBox(); |
| 101 | + for (int i = 0; i < 500; i++) { |
| 102 | + hbox = new HBox(new Text("y"), hbox); |
| 103 | + final HBox h = hbox; |
| 104 | + h.setPadding(new Insets(1)); |
| 105 | + } |
| 106 | + rootPane.setCenter(hbox); |
| 107 | + |
| 108 | + long endTime = System.currentTimeMillis(); |
| 109 | + |
| 110 | + System.out.println("Time to create and add 500 nodes to a Scene = " + |
| 111 | + (endTime - startTime) + " mSec"); |
| 112 | + |
| 113 | + // NOTE : 800 mSec is not a benchmark value |
| 114 | + // It is good enough to catch the regression in performance, if any |
| 115 | + assertTrue("Time to add 500 Nodes is more than 800 mSec", (endTime - startTime) < 800); |
| 116 | + } |
| 117 | + |
| 118 | + @AfterClass |
| 119 | + public static void teardownOnce() { |
| 120 | + Platform.runLater(() -> { |
| 121 | + stage.hide(); |
| 122 | + Platform.exit(); |
| 123 | + }); |
| 124 | + } |
| 125 | +} |
0 commit comments