]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/test/TypingPerfTest.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / test / TypingPerfTest.java
1 /*\r
2  * (C) Copyright IBM Corp. 1998-2007.  All Rights Reserved.\r
3  *\r
4  * The program is provided "as is" without any warranty express or\r
5  * implied, including the warranty of non-infringement and the implied\r
6  * warranties of merchantibility and fitness for a particular purpose.\r
7  * IBM will not be liable for any damages suffered by you as a result\r
8  * of using the Program. In no event will IBM be liable for any\r
9  * special, indirect or consequential damages or lost profits even if\r
10  * IBM has been advised of the possibility of their occurrence. IBM\r
11  * will not be liable for any third party claims against you.\r
12  */\r
13 package com.ibm.richtext.test;\r
14 \r
15 import java.awt.Button;\r
16 import java.awt.GridLayout;\r
17 import java.awt.Frame;\r
18 \r
19 import java.awt.event.ActionEvent;\r
20 import java.awt.event.ActionListener;\r
21 import java.awt.event.KeyEvent;\r
22 import java.awt.event.WindowAdapter;\r
23 import java.awt.event.WindowEvent;\r
24 \r
25 import java.io.File;\r
26 import java.io.PrintWriter;\r
27 \r
28 import java.text.DateFormat;\r
29 import java.util.Date;\r
30 \r
31 import com.ibm.richtext.textpanel.KeyEventForwarder;\r
32 import com.ibm.richtext.textpanel.TextPanel;\r
33 import com.ibm.richtext.awtui.TextFrame;\r
34 import com.ibm.richtext.styledtext.MConstText;\r
35 \r
36 import com.ibm.richtext.demo.EditDemo;\r
37 import com.ibm.richtext.demo.TextDocument;\r
38 \r
39 public class TypingPerfTest implements ActionListener {\r
40     private TextFrame fTextFrame;\r
41     private KeyEventForwarder fKeyEventForwarder;\r
42     private PrintWriter fOut;\r
43 \r
44     //private static final String fgAtStartCommand = "Insert at start";\r
45     //private static final String fgAtEndCommand = "Insert at end";\r
46     private static final String fgFwdDelete = "Forward delete";\r
47     private static final String fgBackspace = "Backspace";\r
48     private static final String fgAtCurrentPosCommand = "Insert at current position";\r
49     private static final String fgLotsOfTextCommand = "Insert a lot of text";\r
50 \r
51     private static final String USAGE = "Usage: java com.ibm.richtext.test.TypingPerfTest [file] [-insertionText text]";\r
52     private char[] fInsText;\r
53 \r
54     public static void main(String[] args) {\r
55 \r
56         // not used OutputStream outStream = null;\r
57         PrintWriter writer = new PrintWriter(System.out);\r
58 \r
59         MConstText text = Declaration.fgDeclaration;\r
60         char[] insText = "The quick brown fox jumps over the lazy dog. The end. ".toCharArray();\r
61         \r
62         int index = 0;\r
63         while (index < args.length) {\r
64             if (args[index].equals("-insertionText")) {\r
65                 if (args.length == ++index) {\r
66                     throw new Error(USAGE);\r
67                 }\r
68                 insText = args[index++].toCharArray();\r
69             }\r
70             else {\r
71                 // This will try MConstText first, then plain text.\r
72                 TextDocument doc = EditDemo.getDocumentFromFile(new File(args[index++]));\r
73                 if (doc == null) {\r
74                     throw new Error("Couldn't open file "+args[index-1]);\r
75                 }\r
76                 text = doc.getText();\r
77             }\r
78         }\r
79         \r
80         if (index != args.length) {\r
81             throw new Error(USAGE);\r
82         }\r
83         \r
84         new TypingPerfTest(writer, text, insText);\r
85     }\r
86 \r
87     public TypingPerfTest(PrintWriter out, MConstText text, char[] insText) {\r
88 \r
89         fInsText = insText;\r
90         fTextFrame = new TextFrame(text, "", null);\r
91         TextPanel textPanel = (TextPanel) fTextFrame.getTextPanel();\r
92         fKeyEventForwarder = new KeyEventForwarder(textPanel);\r
93         fOut = out;\r
94 \r
95         DateFormat df = DateFormat.getDateTimeInstance();\r
96         out.println("Test date: " + df.format(new Date()));\r
97 \r
98         fTextFrame.setSize(500, 700);\r
99         fTextFrame.show();\r
100 \r
101         Frame f = new Frame("Typing Perf Test");\r
102         f.setLayout(new GridLayout(0, 1));\r
103         Button b;\r
104 /*\r
105         b = new Button(fgAtStartCmd);\r
106         b.addActionListener(this);\r
107         f.add(b);\r
108 \r
109         b = new Button(fgAtEndCmd);\r
110         b.addActionListener(this);\r
111         f.add(b);\r
112 */\r
113         b = new Button(fgAtCurrentPosCommand);\r
114         b.addActionListener(this);\r
115         f.add(b);\r
116 \r
117         b = new Button(fgLotsOfTextCommand);\r
118         b.addActionListener(this);\r
119         f.add(b);\r
120 \r
121         b = new Button(fgFwdDelete);\r
122         b.addActionListener(this);\r
123         f.add(b);\r
124 \r
125         b = new Button(fgBackspace);\r
126         b.addActionListener(this);\r
127         f.add(b);\r
128 \r
129         f.doLayout();\r
130         WindowAdapter closer = new WindowAdapter() {\r
131             public void windowClosing(WindowEvent e) {\r
132                 fOut.close();\r
133                 System.exit(0);\r
134             }\r
135         };\r
136         \r
137         f.addWindowListener(closer);\r
138         fTextFrame.addWindowListener(closer);\r
139         \r
140         f.setSize(200, 80);\r
141         f.show();\r
142     }\r
143 \r
144     public void actionPerformed(ActionEvent evt) {\r
145         if (evt.getActionCommand().equals(fgAtCurrentPosCommand)) {\r
146             insertAtCurrentPos(1);\r
147         }\r
148         else if (evt.getActionCommand().equals(fgLotsOfTextCommand)) {\r
149             insertAtCurrentPos(8);\r
150         }\r
151         else if (evt.getActionCommand().equals(fgFwdDelete)) {\r
152             forwardDelete(1);\r
153         }\r
154         else if (evt.getActionCommand().equals(fgBackspace)) {\r
155             backspace(1);\r
156         }\r
157     }\r
158 \r
159     private void insertAtCurrentPos(final int times) {\r
160 \r
161         fTextFrame.toFront();\r
162 \r
163         System.gc();\r
164 \r
165         long startTime = System.currentTimeMillis();\r
166 \r
167         for (int t=0; t < times; t++) {\r
168             for (int i=0; i < fInsText.length; i++) {\r
169 \r
170                 KeyEvent event = new KeyEvent(fTextFrame, KeyEvent.KEY_TYPED, 0, 0, 0, fInsText[i]);\r
171                 fKeyEventForwarder.handleKeyEvent(event);\r
172             }\r
173         }\r
174 \r
175         long time = System.currentTimeMillis() - startTime;\r
176 \r
177         fOut.println("Total time: " + time);\r
178         fOut.println("Millis per character: " + (time / (fInsText.length*times)));\r
179         fOut.flush();\r
180     }\r
181 \r
182     private void forwardDelete(final int times) {\r
183 \r
184         System.gc();\r
185 \r
186         long startTime = System.currentTimeMillis();\r
187 \r
188         for (int t=0; t < times; t++) {\r
189             for (int i=0; i < fInsText.length; i++) {\r
190 \r
191                 KeyEvent event = new KeyEvent(fTextFrame, 0, 0, 0, KeyEvent.VK_DELETE, '\u00FF');\r
192                 fKeyEventForwarder.handleKeyEvent(event);\r
193             }\r
194         }\r
195 \r
196         long time = System.currentTimeMillis() - startTime;\r
197 \r
198         fOut.println("Total time: " + time);\r
199         fOut.println("Millis per character: " + (time / (fInsText.length*times)));\r
200         fOut.flush();\r
201     }\r
202 \r
203     private void backspace(final int times) {\r
204 \r
205         System.gc();\r
206 \r
207         long startTime = System.currentTimeMillis();\r
208 \r
209         for (int t=0; t < times; t++) {\r
210             for (int i=0; i < fInsText.length; i++) {\r
211 \r
212                 KeyEvent event = new KeyEvent(fTextFrame, 0, 0, 0, KeyEvent.VK_BACK_SPACE, '\u0010');\r
213                 fKeyEventForwarder.handleKeyEvent(event);\r
214             }\r
215         }\r
216 \r
217         long time = System.currentTimeMillis() - startTime;\r
218 \r
219         fOut.println("Total time: " + time);\r
220         fOut.println("Millis per character: " + (time / (fInsText.length*times)));\r
221         fOut.flush();\r
222     }\r
223 }\r