2 * (C) Copyright IBM Corp. 1998-2007. All Rights Reserved.
\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
13 package com.ibm.richtext.test;
\r
15 import java.awt.Button;
\r
16 import java.awt.GridLayout;
\r
17 import java.awt.Frame;
\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
25 import java.io.File;
\r
26 import java.io.PrintWriter;
\r
28 import java.text.DateFormat;
\r
29 import java.util.Date;
\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
36 import com.ibm.richtext.demo.EditDemo;
\r
37 import com.ibm.richtext.demo.TextDocument;
\r
39 public class TypingPerfTest implements ActionListener {
\r
40 private TextFrame fTextFrame;
\r
41 private KeyEventForwarder fKeyEventForwarder;
\r
42 private PrintWriter fOut;
\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
51 private static final String USAGE = "Usage: java com.ibm.richtext.test.TypingPerfTest [file] [-insertionText text]";
\r
52 private char[] fInsText;
\r
54 public static void main(String[] args) {
\r
56 // not used OutputStream outStream = null;
\r
57 PrintWriter writer = new PrintWriter(System.out);
\r
59 MConstText text = Declaration.fgDeclaration;
\r
60 char[] insText = "The quick brown fox jumps over the lazy dog. The end. ".toCharArray();
\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
68 insText = args[index++].toCharArray();
\r
71 // This will try MConstText first, then plain text.
\r
72 TextDocument doc = EditDemo.getDocumentFromFile(new File(args[index++]));
\r
74 throw new Error("Couldn't open file "+args[index-1]);
\r
76 text = doc.getText();
\r
80 if (index != args.length) {
\r
81 throw new Error(USAGE);
\r
84 new TypingPerfTest(writer, text, insText);
\r
87 public TypingPerfTest(PrintWriter out, MConstText text, char[] insText) {
\r
90 fTextFrame = new TextFrame(text, "", null);
\r
91 TextPanel textPanel = (TextPanel) fTextFrame.getTextPanel();
\r
92 fKeyEventForwarder = new KeyEventForwarder(textPanel);
\r
95 DateFormat df = DateFormat.getDateTimeInstance();
\r
96 out.println("Test date: " + df.format(new Date()));
\r
98 fTextFrame.setSize(500, 700);
\r
101 Frame f = new Frame("Typing Perf Test");
\r
102 f.setLayout(new GridLayout(0, 1));
\r
105 b = new Button(fgAtStartCmd);
\r
106 b.addActionListener(this);
\r
109 b = new Button(fgAtEndCmd);
\r
110 b.addActionListener(this);
\r
113 b = new Button(fgAtCurrentPosCommand);
\r
114 b.addActionListener(this);
\r
117 b = new Button(fgLotsOfTextCommand);
\r
118 b.addActionListener(this);
\r
121 b = new Button(fgFwdDelete);
\r
122 b.addActionListener(this);
\r
125 b = new Button(fgBackspace);
\r
126 b.addActionListener(this);
\r
130 WindowAdapter closer = new WindowAdapter() {
\r
131 public void windowClosing(WindowEvent e) {
\r
137 f.addWindowListener(closer);
\r
138 fTextFrame.addWindowListener(closer);
\r
140 f.setSize(200, 80);
\r
144 public void actionPerformed(ActionEvent evt) {
\r
145 if (evt.getActionCommand().equals(fgAtCurrentPosCommand)) {
\r
146 insertAtCurrentPos(1);
\r
148 else if (evt.getActionCommand().equals(fgLotsOfTextCommand)) {
\r
149 insertAtCurrentPos(8);
\r
151 else if (evt.getActionCommand().equals(fgFwdDelete)) {
\r
154 else if (evt.getActionCommand().equals(fgBackspace)) {
\r
159 private void insertAtCurrentPos(final int times) {
\r
161 fTextFrame.toFront();
\r
165 long startTime = System.currentTimeMillis();
\r
167 for (int t=0; t < times; t++) {
\r
168 for (int i=0; i < fInsText.length; i++) {
\r
170 KeyEvent event = new KeyEvent(fTextFrame, KeyEvent.KEY_TYPED, 0, 0, 0, fInsText[i]);
\r
171 fKeyEventForwarder.handleKeyEvent(event);
\r
175 long time = System.currentTimeMillis() - startTime;
\r
177 fOut.println("Total time: " + time);
\r
178 fOut.println("Millis per character: " + (time / (fInsText.length*times)));
\r
182 private void forwardDelete(final int times) {
\r
186 long startTime = System.currentTimeMillis();
\r
188 for (int t=0; t < times; t++) {
\r
189 for (int i=0; i < fInsText.length; i++) {
\r
191 KeyEvent event = new KeyEvent(fTextFrame, 0, 0, 0, KeyEvent.VK_DELETE, '\u00FF');
\r
192 fKeyEventForwarder.handleKeyEvent(event);
\r
196 long time = System.currentTimeMillis() - startTime;
\r
198 fOut.println("Total time: " + time);
\r
199 fOut.println("Millis per character: " + (time / (fInsText.length*times)));
\r
203 private void backspace(final int times) {
\r
207 long startTime = System.currentTimeMillis();
\r
209 for (int t=0; t < times; t++) {
\r
210 for (int i=0; i < fInsText.length; i++) {
\r
212 KeyEvent event = new KeyEvent(fTextFrame, 0, 0, 0, KeyEvent.VK_BACK_SPACE, '\u0010');
\r
213 fKeyEventForwarder.handleKeyEvent(event);
\r
217 long time = System.currentTimeMillis() - startTime;
\r
219 fOut.println("Total time: " + time);
\r
220 fOut.println("Millis per character: " + (time / (fInsText.length*times)));
\r