1/*
2 * Copyright (c) 2003, 2007, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @summary Tests just a benchmark of introspector performance
27 * @author Mark Davidson
28 * @run main/manual TestIntrospector
29 */
30
31import java.beans.BeanInfo;
32import java.beans.IntrospectionException;
33import java.beans.Introspector;
34
35/**
36 * This test is just a benchmark of introspector performance.
37 */
38public class TestIntrospector {
39    private static final Class[] TYPES = {
40            javax.swing.Box.class,
41            javax.swing.DefaultComboBoxModel.class,
42            javax.swing.DefaultCellEditor.class,
43            javax.swing.DefaultComboBoxModel.class,
44            javax.swing.DefaultListModel.class,
45            javax.swing.ImageIcon.class,
46            javax.swing.JApplet.class,
47            javax.swing.JButton.class,
48            javax.swing.JCheckBox.class,
49            javax.swing.JColorChooser.class,
50            javax.swing.JComboBox.class,
51            javax.swing.JDesktopPane.class,
52            javax.swing.JDialog.class,
53            javax.swing.JEditorPane.class,
54            javax.swing.JFileChooser.class,
55            javax.swing.JFrame.class,
56            javax.swing.JInternalFrame.class,
57            javax.swing.JLabel.class,
58            javax.swing.JList.class,
59            javax.swing.JMenu.class,
60            javax.swing.JMenuBar.class,
61            javax.swing.JMenuItem.class,
62            javax.swing.JOptionPane.class,
63            javax.swing.JPanel.class,
64            javax.swing.JPasswordField.class,
65            javax.swing.JPopupMenu.class,
66            javax.swing.JProgressBar.class,
67            javax.swing.JRadioButton.class,
68            javax.swing.JRadioButtonMenuItem.class,
69            javax.swing.JRootPane.class,
70            javax.swing.JScrollPane.class,
71            javax.swing.JSeparator.class,
72            javax.swing.JSlider.class,
73            javax.swing.JSplitPane.class,
74            javax.swing.JTabbedPane.class,
75            javax.swing.JTable.class,
76            javax.swing.JTextField.class,
77            javax.swing.JTextArea.class,
78            javax.swing.JTextPane.class,
79            javax.swing.JToggleButton.class,
80            javax.swing.JToolBar.class,
81            javax.swing.JToolTip.class,
82            javax.swing.JTree.class,
83            javax.swing.JWindow.class,
84            java.awt.Button.class,
85            java.awt.Canvas.class,
86            java.awt.Checkbox.class,
87            java.awt.Choice.class,
88            java.awt.Dialog.class,
89            java.awt.FileDialog.class,
90            java.awt.Frame.class,
91            java.awt.Image.class,
92            java.awt.List.class,
93            java.awt.Menu.class,
94            java.awt.MenuBar.class,
95            java.awt.MenuItem.class,
96            java.awt.Panel.class,
97            java.awt.Point.class,
98            java.awt.Rectangle.class,
99            java.awt.Scrollbar.class,
100            java.awt.TextArea.class,
101            java.awt.TextField.class,
102            java.awt.Window.class,
103    };
104
105    public static void main(String[] args) throws IntrospectionException {
106        StringBuilder sb = null;
107        if (args.length > 0) {
108            if (args[0].equals("show")) {
109                sb = new StringBuilder(65536);
110            }
111        }
112        Introspector.flushCaches();
113        int count = (sb != null) ? 10 : 100;
114        long time = -System.currentTimeMillis();
115        for (int i = 0; i < count; i++) {
116            test(sb);
117            test(sb);
118            Introspector.flushCaches();
119        }
120        time += System.currentTimeMillis();
121        System.out.println("Time (average): " + time / count);
122    }
123
124    private static void test(StringBuilder sb) throws IntrospectionException {
125        long time = 0L;
126        if (sb != null) {
127            sb.append("Time\t#Props\t#Events\t#Methods\tClass\n");
128            sb.append("----------------------------------------");
129            time = -System.currentTimeMillis();
130        }
131        for (Class type : TYPES) {
132            test(sb, type);
133        }
134        if (sb != null) {
135            time += System.currentTimeMillis();
136            sb.append("\nTime: ").append(time).append(" ms\n");
137            System.out.println(sb);
138            sb.setLength(0);
139        }
140    }
141
142    private static void test(StringBuilder sb, Class type) throws IntrospectionException {
143        long time = 0L;
144        if (sb != null) {
145            time = -System.currentTimeMillis();
146        }
147        BeanInfo info = Introspector.getBeanInfo(type);
148        if (sb != null) {
149            time += System.currentTimeMillis();
150            sb.append('\n').append(time);
151            sb.append('\t').append(info.getPropertyDescriptors().length);
152            sb.append('\t').append(info.getEventSetDescriptors().length);
153            sb.append('\t').append(info.getMethodDescriptors().length);
154            sb.append('\t').append(type.getName());
155        }
156    }
157}
158