001: /*
002: * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
003: *
004: * The program is provided "as is" without any warranty express or
005: * implied, including the warranty of non-infringement and the implied
006: * warranties of merchantibility and fitness for a particular purpose.
007: * IBM will not be liable for any damages suffered by you as a result
008: * of using the Program. In no event will IBM be liable for any
009: * special, indirect or consequential damages or lost profits even if
010: * IBM has been advised of the possibility of their occurrence. IBM
011: * will not be liable for any third party claims against you.
012: */
013: package com.ibm.richtext.test;
014:
015: import java.awt.Button;
016: import java.awt.GridLayout;
017: import java.awt.Frame;
018:
019: import java.awt.event.ActionEvent;
020: import java.awt.event.ActionListener;
021: import java.awt.event.KeyEvent;
022: import java.awt.event.WindowAdapter;
023: import java.awt.event.WindowEvent;
024:
025: import java.io.File;
026: import java.io.PrintWriter;
027: import java.io.IOException;
028:
029: import java.text.DateFormat;
030: import java.util.Date;
031:
032: import com.ibm.richtext.textpanel.KeyEventForwarder;
033: import com.ibm.richtext.textpanel.TextPanel;
034: import com.ibm.richtext.awtui.TextFrame;
035: import com.ibm.richtext.styledtext.MConstText;
036:
037: import com.ibm.richtext.demo.EditDemo;
038: import com.ibm.richtext.demo.TextDocument;
039:
040: public class TypingPerfTest implements ActionListener {
041:
042: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
043: private TextFrame fTextFrame;
044: private KeyEventForwarder fKeyEventForwarder;
045: private PrintWriter fOut;
046:
047: private static final String fgAtStartCommand = "Insert at start";
048: private static final String fgAtEndCommand = "Insert at end";
049: private static final String fgFwdDelete = "Forward delete";
050: private static final String fgBackspace = "Backspace";
051: private static final String fgAtCurrentPosCommand = "Insert at current position";
052: private static final String fgLotsOfTextCommand = "Insert a lot of text";
053:
054: private static final String USAGE = "Usage: java com.ibm.richtext.test.TypingPerfTest [file] [-insertionText text]";
055: private char[] fInsText;
056:
057: public static void main(String[] args) throws IOException {
058:
059: // not used OutputStream outStream = null;
060: PrintWriter writer = new PrintWriter(System.out);
061:
062: MConstText text = Declaration.fgDeclaration;
063: char[] insText = "The quick brown fox jumps over the lazy dog. The end. "
064: .toCharArray();
065:
066: int index = 0;
067: while (index < args.length) {
068: if (args[index].equals("-insertionText")) {
069: if (args.length == ++index) {
070: throw new Error(USAGE);
071: }
072: insText = args[index++].toCharArray();
073: } else {
074: // This will try MConstText first, then plain text.
075: TextDocument doc = EditDemo
076: .getDocumentFromFile(new File(args[index++]));
077: if (doc == null) {
078: throw new Error("Couldn't open file "
079: + args[index - 1]);
080: }
081: text = doc.getText();
082: }
083: }
084:
085: if (index != args.length) {
086: throw new Error(USAGE);
087: }
088:
089: new TypingPerfTest(writer, text, insText);
090: }
091:
092: public TypingPerfTest(PrintWriter out, MConstText text,
093: char[] insText) throws IOException {
094:
095: fInsText = insText;
096: fTextFrame = new TextFrame(text, "", null);
097: TextPanel textPanel = (TextPanel) fTextFrame.getTextPanel();
098: fKeyEventForwarder = new KeyEventForwarder(textPanel);
099: fOut = out;
100:
101: DateFormat df = DateFormat.getDateTimeInstance();
102: out.println("Test date: " + df.format(new Date()));
103:
104: fTextFrame.setSize(500, 700);
105: fTextFrame.show();
106:
107: Frame f = new Frame("Typing Perf Test");
108: f.setLayout(new GridLayout(0, 1));
109: Button b;
110: /*
111: b = new Button(fgAtStartCmd);
112: b.addActionListener(this);
113: f.add(b);
114:
115: b = new Button(fgAtEndCmd);
116: b.addActionListener(this);
117: f.add(b);
118: */
119: b = new Button(fgAtCurrentPosCommand);
120: b.addActionListener(this );
121: f.add(b);
122:
123: b = new Button(fgLotsOfTextCommand);
124: b.addActionListener(this );
125: f.add(b);
126:
127: b = new Button(fgFwdDelete);
128: b.addActionListener(this );
129: f.add(b);
130:
131: b = new Button(fgBackspace);
132: b.addActionListener(this );
133: f.add(b);
134:
135: f.doLayout();
136: WindowAdapter closer = new WindowAdapter() {
137: public void windowClosing(WindowEvent e) {
138: fOut.close();
139: System.exit(0);
140: }
141: };
142:
143: f.addWindowListener(closer);
144: fTextFrame.addWindowListener(closer);
145:
146: f.setSize(200, 80);
147: f.show();
148: }
149:
150: public void actionPerformed(ActionEvent evt) {
151:
152: try {
153: if (evt.getActionCommand().equals(fgAtCurrentPosCommand)) {
154:
155: insertAtCurrentPos(1);
156: } else if (evt.getActionCommand().equals(
157: fgLotsOfTextCommand)) {
158:
159: insertAtCurrentPos(8);
160: } else if (evt.getActionCommand().equals(fgFwdDelete)) {
161:
162: forwardDelete(1);
163: } else if (evt.getActionCommand().equals(fgBackspace)) {
164:
165: backspace(1);
166: }
167: } catch (IOException e) {
168: System.out.println("Caught exception: " + e);
169: }
170: }
171:
172: private void insertAtCurrentPos(final int times) throws IOException {
173:
174: fTextFrame.toFront();
175:
176: System.gc();
177:
178: long startTime = System.currentTimeMillis();
179:
180: for (int t = 0; t < times; t++) {
181: for (int i = 0; i < fInsText.length; i++) {
182:
183: KeyEvent event = new KeyEvent(fTextFrame,
184: KeyEvent.KEY_TYPED, 0, 0, 0, fInsText[i]);
185: fKeyEventForwarder.handleKeyEvent(event);
186: }
187: }
188:
189: long time = System.currentTimeMillis() - startTime;
190:
191: fOut.println("Total time: " + time);
192: fOut.println("Millis per character: "
193: + (time / (fInsText.length * times)));
194: fOut.flush();
195: }
196:
197: private void forwardDelete(final int times) throws IOException {
198:
199: System.gc();
200:
201: long startTime = System.currentTimeMillis();
202:
203: for (int t = 0; t < times; t++) {
204: for (int i = 0; i < fInsText.length; i++) {
205:
206: KeyEvent event = new KeyEvent(fTextFrame, 0, 0, 0,
207: KeyEvent.VK_DELETE, '\u00FF');
208: fKeyEventForwarder.handleKeyEvent(event);
209: }
210: }
211:
212: long time = System.currentTimeMillis() - startTime;
213:
214: fOut.println("Total time: " + time);
215: fOut.println("Millis per character: "
216: + (time / (fInsText.length * times)));
217: fOut.flush();
218: }
219:
220: private void backspace(final int times) throws IOException {
221:
222: System.gc();
223:
224: long startTime = System.currentTimeMillis();
225:
226: for (int t = 0; t < times; t++) {
227: for (int i = 0; i < fInsText.length; i++) {
228:
229: KeyEvent event = new KeyEvent(fTextFrame, 0, 0, 0,
230: KeyEvent.VK_BACK_SPACE, '\u0010');
231: fKeyEventForwarder.handleKeyEvent(event);
232: }
233: }
234:
235: long time = System.currentTimeMillis() - startTime;
236:
237: fOut.println("Total time: " + time);
238: fOut.println("Millis per character: "
239: + (time / (fInsText.length * times)));
240: fOut.flush();
241: }
242: }
|