Path: blob/master/test/jdk/java/awt/Multiscreen/WPanelPeerPerf/WPanelPeerPerf.java
41153 views
/*1* Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24@test25@key headful26@bug 508562627@summary Exponential performance regression in AWT components (multiple mon)28@run main WPanelPeerPerf29*/3031import java.awt.*;32import java.awt.event.*;33import javax.swing.*;3435/**36* This test must be run on a multi-screen system.37* This test works by moving a Frame back and forth between the screens a few38* times. When the bug is active, the first move will overwhelm the EDT with39* recursive display change calls. The test fails if it takes too long to40* service the setLocation() calls and send componentMoved() events.41*/42public class WPanelPeerPerf {4344private static final int NESTED_PANELS = 25;45private static final int ITERATIONS_PER_SCREEN = 3;46private static final int MAX_WAIT_PER_SCREEN = 2500;47private static final int PAUSE_BETWEEN_MOVES = 500;484950private static Object showLock = new Object();5152private static Counter instance = null;53public static Counter getCounter() {54if (instance == null) {55instance = new Counter();56}57return instance;58}5960private static class Counter {61int counter;62Counter() { counter = 0; }63}6465// This one is very slow!66public static void testAWT() {67// fail if only on one screen68int numScreens = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices().length;69if (numScreens < 2) {70System.err.println("Test must be run on a multiscreen system");71return;72}73final Frame frame = new Frame("AWT WPanelPeerPerf");74frame.addWindowListener(new WindowAdapter() {75public void windowClosing(WindowEvent ev) {76System.exit(0);77}78public void windowOpened(WindowEvent e) {79synchronized(showLock) {80showLock.notify();81}82}83});84frame.setLayout(new BorderLayout());85Label label = new Label("Hello world");86frame.add(label, BorderLayout.NORTH);87Panel panel = new Panel(new BorderLayout());88Panel currentPanel = panel;89for (int i = 0; i < NESTED_PANELS; i++) {90Panel newPanel = new Panel(new BorderLayout());91currentPanel.add(newPanel, BorderLayout.CENTER);92currentPanel = newPanel;93}94currentPanel.add(new Label("WPanelPeerPerf"));95frame.add(panel, BorderLayout.CENTER);96Button btn = new Button("OK");97frame.add(btn, BorderLayout.SOUTH);98frame.pack();99100frame.addComponentListener(new ComponentAdapter() {101public void componentMoved(ComponentEvent e) {102System.out.println("Frame moved: ");103Counter ctr = getCounter();104synchronized(ctr) {105ctr.counter++;106ctr.notify();107}108}109});110synchronized(showLock) {111try {112frame.setVisible(true);113showLock.wait();114}115catch (InterruptedException e) {116e.printStackTrace();117throw new RuntimeException("Problem with showLock");118}119}120runTest(frame);121}122123public static void runTest(Frame theFrame) {124System.out.println("Running test");125GraphicsDevice[] devs = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();126Point[] points = new Point[devs.length];127128for (int i = 0; i < points.length; i++) {129Rectangle bounds = devs[i].getDefaultConfiguration().getBounds();130points[i] = new Point(bounds.x + (bounds.width / 2),131bounds.y + (bounds.height / 2));132System.out.println("Added point:" + points[i]);133}134135final Frame localFrame = theFrame;136137for (int n = 0; n < ITERATIONS_PER_SCREEN; n++) {138for (int i = 0; i < points.length; i++) {139final Point contextPoint = points[i];140SwingUtilities.invokeLater(new Runnable() {141public void run() {142localFrame.setLocation(contextPoint);143}144});145try {146Thread.sleep(PAUSE_BETWEEN_MOVES);147}148catch (InterruptedException e) {149System.out.println("Interrupted during iteration");150}151}152}153Counter ctr = getCounter();154synchronized(ctr) {155try {156if (ctr.counter < ITERATIONS_PER_SCREEN * devs.length) {157// If test hasn't finished, wait for maximum time158// If we get interrupted, test fails159ctr.wait((long)(ITERATIONS_PER_SCREEN * MAX_WAIT_PER_SCREEN * devs.length));160System.out.println("after wait");161if (ctr.counter < ITERATIONS_PER_SCREEN * devs.length) {162throw new RuntimeException("Waited too long for all the componentMoved()s");163}164}165}166catch(InterruptedException e) {167e.printStackTrace();168throw new RuntimeException("Wait interrupted - ???");169}170System.out.println("Counter reads: " + ctr.counter);171}172173}174public static void main(String[] args) {175testAWT();176}177}178179180