1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 /**
23  *
24  */
25 
26 package pvt.gui;
27 
28 import static org.junit.Assert.*;
29 import static org.openoffice.test.common.Testspace.*;
30 import static org.openoffice.test.vcl.Tester.*;
31 import static testlib.gui.AppTool.*;
32 import static testlib.gui.UIMap.*;
33 
34 import java.awt.Rectangle;
35 import java.io.File;
36 import java.io.FileOutputStream;
37 import java.io.PrintStream;
38 import java.util.HashMap;
39 
40 import org.junit.AfterClass;
41 import org.junit.BeforeClass;
42 import org.junit.Ignore;
43 import org.junit.Rule;
44 import org.junit.Test;
45 import org.junit.rules.TestName;
46 import org.openoffice.test.OpenOffice;
47 import org.openoffice.test.common.Condition;
48 import org.openoffice.test.common.GraphicsUtil;
49 import org.openoffice.test.common.Logger;
50 import org.openoffice.test.common.SystemUtil;
51 import org.openoffice.test.common.Testspace;
52 
53 
54 public class Benchmark {
55 	@Rule
56 	public Logger log = Logger.getLogger(this);
57 
58 	@Rule
59 	public TestName testname = new TestName();
60 
61 	private static PrintStream result;
62 
63 	private static final double INTERVAL = 0.01;
64 
65 	public Benchmark() {
66 
67 	}
68 
69 	@BeforeClass
70 	public static void beforeClass() throws Exception {
71 		File resultFile = Testspace.getFile("output/pvt_gui_benchmark.csv");
72 		resultFile.getParentFile().mkdirs();
73 		result = new PrintStream(new FileOutputStream(resultFile));
74 		OpenOffice.killAll();
75 
76 		result.println("Scenario,No,Consumed Time,Memory(VSZ),Memory(RSS),Handles(Windows Only)");
77 	}
78 
79 	@AfterClass
80 	public static void afterClass() throws Exception {
81 		result.close();
82 		app.close();
83 	}
84 
85 	private HashMap<String, Object> getPerfData() {
86 		HashMap<String, Object> proccessInfo = SystemUtil.findProcess(".*(soffice\\.bin|soffice\\.exe .*-env).*");
87 		String pid = (String) proccessInfo.get("pid");
88 		return SystemUtil.getProcessPerfData(pid);
89 	}
90 
91 	private void addRecord(int i, long start, long end) {
92 		HashMap<String, Object>  perf = getPerfData();
93 		result.println(testname.getMethodName() + "," + i + "," + (end - start) + "," + perf.get("vsz") + "," + perf.get("rss") + "," + perf.get("handles"));
94 	}
95 
96 	@Test
97 	public void coolStartup() throws Exception {
98 		aoo.kill();
99 		aoo.cleanUserInstallation();
100 		aoo.start();
101 		long start = System.currentTimeMillis();
102 		startcenter.waitForExistence(120, INTERVAL);
103 		long end = System.currentTimeMillis();
104 		sleep(2);
105 		addRecord(0, start, end);
106 		app.close();
107 	}
108 
109 	@Test
110 	public void warmStartup() throws Exception {
111 		for (int i = 0; i < 8; i++) {
112 			aoo.kill();//make sure app is closed
113 			aoo.start();
114 			long start = System.currentTimeMillis();
115 			startcenter.waitForExistence(120, INTERVAL);
116 			long end = System.currentTimeMillis();
117 			sleep(2);
118 			addRecord(i, start, end);
119 			app.close();
120 		}
121 	}
122 
123 	@Test
124 	public void newTextDocument() {
125 		for (int i = 0; i < 8; i++) {
126 			aoo.kill();//make sure app is closed
127 			aoo.start();
128 			startCenterWriterButton.waitForExistence(120, 1);
129 			sleep(2);
130 			startCenterWriterButton.click(0.5, 0.5);
131 			long start = System.currentTimeMillis();
132 			writer.waitForExistence(60, INTERVAL);
133 			long end = System.currentTimeMillis();
134 			sleep(2);
135 			addRecord(i, start, end);
136 			app.close();
137 		}
138 	}
139 
140 	@Test
141 	public void newSpreadsheet() {
142 		for (int i = 0; i < 8; i++) {
143 			aoo.kill();//make sure app is closed
144 			aoo.start();
145 			startCenterCalcButton.waitForExistence(120, 1);
146 			sleep(2);
147 			startCenterCalcButton.click(0.5, 0.5);
148 			long start = System.currentTimeMillis();
149 			calc.waitForExistence(60, INTERVAL);
150 			long end = System.currentTimeMillis();
151 			sleep(2);
152 			addRecord(i, start, end);
153 			app.close();
154 		}
155 	}
156 
157 	@Test
158 	public void newPresentation() {
159 		for (int i = 0; i < 8; i++) {
160 			aoo.kill();//make sure app is closed
161 			aoo.start();
162 			startCenterImpressButton.waitForExistence(120, 1);
163 			sleep(2);
164 			startCenterImpressButton.click(0.5, 0.5);
165 			sleep(1);
166 			presentationWizard.click(0.9, 0.95);
167 			long start = System.currentTimeMillis();
168 			impress.waitForExistence(60, INTERVAL);
169 			long end = System.currentTimeMillis();
170 			sleep(2);
171 			addRecord(i, start, end);
172 			app.close();
173 		}
174 	}
175 
176 	@Test
177 	public void loadFinishPlainODT() {
178 		loadFinish("pvt/plain_200p.odt", "Page 1 / 23[0-9]{1}");
179 	}
180 
181 	@Test
182 	public void loadFinishPlainDOC() {
183 		loadFinish("pvt/plain_200p.doc", "Page 1 / 23[0-9]{1}");
184 	}
185 
186 	@Test
187 	public void loadFinishPlainDOCX() {
188 		loadFinish("pvt/plain_200p.docx", "Page 1 / 19[0-9]{1}");
189 	}
190 
191 	@Test
192 	public void loadFinishPlainODS() {
193 		loadFinish("pvt/plain_11s.ods", "Sheet 1 / 11");
194 	}
195 
196 	@Test
197 	public void loadFinishPlainXLS() {
198 		loadFinish("pvt/plain_11s.xls", "Sheet 1 / 11");
199 	}
200 
201 	@Test
202 	public void loadFinishPlainXLSX() {
203 		loadFinish("pvt/plain_11s.xlsx", "Sheet 1 / 11");
204 	}
205 
206 	@Test
207 	public void loadFinishPlainODP() {
208 		loadFinish("pvt/plain_200p.odp", "Slide 1 / 200");
209 	}
210 
211 	@Test
212 	public void loadFinishPlainPPT() {
213 		loadFinish("pvt/plain_200p.ppt", "Slide 1 / 200");
214 	}
215 
216 	@Test
217 	public void loadFinishPlainPPTX() {
218 		loadFinish("pvt/plain_200p.pptx", "Slide 1 / 200");
219 	}
220 
221 	@Test
222 	@Ignore
223 	public void loadFinishComplexDOC() {
224 		loadFinish("pvt/sw_complex_100p.doc", "Page 1 / ");
225 	}
226 
227 	@Test
228 	@Ignore
229 	public void loadFinishComplexODT() {
230 		loadFinish("pvt/sw_complex_100p_odf1.2.odt", "Page 1 / ");
231 	}
232 
233 	@Test
234 	public void loadFinishComplexXLS() {
235 		loadFinish("pvt/sc_complex_13sh_4kcell.xls", "Sheet 2 / 13");
236 	}
237 
238 	@Test
239 	public void loadFinishComplexODS() {
240 		loadFinish("pvt/sc_complex_13sh_4kcell_new_odf1.2.ods", "Sheet 6 / 13");
241 	}
242 
243 
244 	@Test
245 	public void loadFinishComplexODP() {
246 		loadFinish("pvt/sd_complex_51p_odf1.2.odp", "Slide 1 / 51");
247 	}
248 
249 	@Test
250 	public void loadFinishComplexPPT() {
251 		loadFinish("pvt/sd_complex_51p.ppt", "Slide 1 / 51");
252 	}
253 
254 	public void loadFinish(String file, final String indicator) {
255 		final int openIndicatorIndex = file.matches(".*\\.(odp|ppt|pptx)$") ? 4 : 0;
256 		String path = prepareData(file);
257 		aoo.kill();
258 		app.start();
259 		startcenter.waitForExistence(10, 1);
260 		sleep(2); // Give some seconds to AOO relax. We want data more stable.
261 		for (int i = 0; i < 8; i++) {
262 			app.dispatch(".uno:Open");
263 			filePickerPath.setText(path);
264 			sleep(2);
265 			filePickerOpen.click(0.5, 0.5);
266 			long start = System.currentTimeMillis();
267 			new Condition() {
268 				@Override
269 				public boolean value() {
270 					try {
271 						String text = statusBar.getItemText(openIndicatorIndex);
272 						return text.matches(indicator);
273 					} catch (Exception e) {
274 						return false;
275 					}
276 				}
277 
278 			}.waitForTrue("", 120, INTERVAL);
279 			long end = System.currentTimeMillis();
280 			sleep(2);
281 			addRecord(i, start, end);
282 			discard();
283 		}
284 
285 		app.close();
286 	}
287 
288 	@Test
289 	public void savePlainDOC() {
290 		save("pvt/sw_plain_120p.doc", "Page 1 / ");
291 	}
292 
293 	@Test
294 	public void savePlainODT() {
295 		save("pvt/plain_200p.odt", "Page 1 / 23[0-9]{1}");
296 	}
297 
298 	@Test
299 	public void saveComplexDOC() {
300 		save("pvt/plain_200p.doc", "Page 1 / 23[0-9]{1}");
301 	}
302 
303 	@Test
304 	public void saveComplexODT() {
305 		save("pvt/sw_complex_100p_odf1.2.odt", "Page 1 / ");
306 	}
307 
308 	@Test
309 	public void savePlainXLS() {
310 		save("pvt/plain_11s.xls", "Sheet 1 / 11");
311 	}
312 
313 	@Test
314 	public void savePlainODS() {
315 		save("pvt/plain_11s.ods", "Sheet 1 / 11");
316 	}
317 
318 	@Test
319 	public void saveComplexXLS() {
320 		save("pvt/sc_complex_13sh_4kcell.xls", "Sheet 2 / 13");
321 	}
322 
323 	@Test
324 	public void saveComplexODS() {
325 		save("pvt/sc_complex_13sh_4kcell_new_odf1.2.ods", "Sheet 6 / 13");
326 	}
327 
328 	@Test
329 	public void savePlainODP() {
330 		save("pvt/plain_200p.odp", "Slide 1 / 200");
331 	}
332 
333 	@Test
334 	public void savePlainPPT() {
335 		save("pvt/plain_200p.ppt", "Slide 1 / 200");
336 	}
337 
338 	@Test
339 	public void saveComplexODP() {
340 		save("pvt/sd_complex_51p_odf1.2.odp", "Slide 1 / 51");
341 	}
342 
343 	@Test
344 	public void saveComplexPPT() {
345 		save("pvt/sd_complex_51p.ppt", "Slide 1 / 51");
346 	}
347 
348 	public void save(String file, final String openIndicator) {
349 		boolean alienFormat = file.matches(".*\\.(doc|xls|ppt|docx|xlsx|pptx)$");
350 		final int openIndicatorIndex = file.matches(".*\\.(odp|ppt|pptx)$") ? 4 : 0;
351 		final int saveIndicatorIndex = file.matches(".*\\.(odt|doc|docx)$") ? 5 : file.matches(".*\\.(ods|xls|xlsx)$") ? 4 : 2;
352 		aoo.kill();
353 		app.start();
354 		String picture = prepareData("image/red_64x64.bmp");
355 		for (int i = 0; i < 8; i++) {
356 			String dir = "temp/file" + i;
357 			getFile(dir).mkdirs();
358 			open(prepareData(file, dir));
359 			new Condition() {
360 				@Override
361 				public boolean value() {
362 					try {
363 						String text = statusBar.getItemText(openIndicatorIndex);
364 						return text.matches(openIndicator);
365 					} catch (Exception e) {
366 						return false;
367 					}
368 				}
369 
370 			}.waitForTrue("", 120, 1);
371 			sleep(2);
372 			insertPicture(picture);
373 			sleep(3);
374 			assertEquals("File is modified", "*", statusBar.getItemText(saveIndicatorIndex));
375 			app.dispatch(".uno:Save");
376 			if (alienFormat) {
377 				alienFormatDlg.waitForExistence(3, 1);
378 				sleep(1);
379 				typeKeys("<enter>");
380 			}
381 
382 			long start = System.currentTimeMillis();
383 			new Condition() {
384 				@Override
385 				public boolean value() {
386 					try {
387 						String text = statusBar.getItemText(saveIndicatorIndex);
388 						return " ".equals(text);
389 					} catch (Exception e) {
390 						return false;
391 					}
392 				}
393 
394 			}.waitForTrue("", 120, INTERVAL);
395 			long end = System.currentTimeMillis();
396 			sleep(2);
397 			addRecord(i, start, end);
398 			close();
399 		}
400 
401 		app.close();
402 	}
403 
404 	@Test
405 	public void slideShow() {
406 		aoo.kill();
407 		app.start();
408 		String path = prepareData("pvt/sd_slideshow.odp");
409 		final Rectangle rect = GraphicsUtil.getScreenRectangle();
410 		// when slide show is running, top-center area will be filled with green
411 		rect.setRect(rect.getCenterX(), 2, 2, 2);
412 		for (int i = 0; i < 8; i++) {
413 			open(path);
414 			impress.waitForExistence(60, 1);
415 			sleep(2);
416 			assertFalse("Slideshow control exists", slideShow.exists());
417 			assertFalse("Slideshow is not started", GraphicsUtil.isFilledWith(0xFF00FF00, rect));
418 			typeKeys("<F5>");
419 			long start = System.currentTimeMillis();
420 			new Condition() {
421 				@Override
422 				public boolean value() {
423 					return GraphicsUtil.isFilledWith(0xFF00FF00, rect);
424 				}
425 
426 			}.waitForTrue("", 120, INTERVAL);
427 			long end = System.currentTimeMillis();
428 			sleep(2);
429 			addRecord(i, start, end);
430 			slideShow.typeKeys("<esc>");
431 			sleep(2);
432 			close();
433 		}
434 		app.close();
435 	}
436 }
437