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.openoffice.test.common.Testspace.*;
29 import static org.openoffice.test.vcl.Tester.*;
30 import static testlib.gui.UIMap.*;
31 
32 import java.io.File;
33 import java.io.FileOutputStream;
34 import java.io.PrintStream;
35 import java.util.HashMap;
36 
37 import org.junit.AfterClass;
38 import org.junit.BeforeClass;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.rules.TestName;
42 import org.openoffice.test.OpenOffice;
43 import org.openoffice.test.common.Condition;
44 import org.openoffice.test.common.Logger;
45 import org.openoffice.test.common.SystemUtil;
46 import org.openoffice.test.common.Testspace;
47 
48 
49 public class Benchmark {
50 	@Rule
51 	public Logger log = Logger.getLogger(this);
52 
53 	@Rule
54 	public TestName testname = new TestName();
55 
56 	private static PrintStream result;
57 
58 	private static final double INTERVAL = 0.01;
59 
60 	public Benchmark() {
61 
62 	}
63 
64 	@BeforeClass
65 	public static void beforeClass() throws Exception {
66 		File resultFile = Testspace.getFile("output/gui_benchmark.csv");
67 		resultFile.getParentFile().mkdirs();
68 		result = new PrintStream(new FileOutputStream(resultFile));
69 		OpenOffice.killAll();
70 
71 		result.println("Scenario,Consumed Time,Memory(VSZ),Memory(RSS),Handles(Windows Only)");
72 	}
73 
74 	@AfterClass
75 	public static void afterClass() throws Exception {
76 		result.close();
77 		app.close();
78 	}
79 
80 	private HashMap<String, Object> getPerfData() {
81 		HashMap<String, Object> proccessInfo = SystemUtil.findProcess(".*(soffice\\.bin|soffice\\.exe .*-env).*");
82 		String pid = (String) proccessInfo.get("pid");
83 		return SystemUtil.getProcessPerfData(pid);
84 	}
85 
86 	private void addRecord(int i, long start, long end) {
87 		HashMap<String, Object>  perf = getPerfData();
88 		result.println(testname.getMethodName() + i + "," + (end - start) + "," + perf.get("vsz") + "," + perf.get("rss") + "," + perf.get("handles"));
89 	}
90 
91 	@Test
92 	public void coolStartup() throws Exception {
93 		aoo.kill();
94 		aoo.cleanUserInstallation();
95 		aoo.start();
96 		long start = System.currentTimeMillis();
97 		startcenter.waitForExistence(120, INTERVAL);
98 		long end = System.currentTimeMillis();
99 		sleep(2);
100 		addRecord(0, start, end);
101 		app.close();
102 	}
103 
104 	@Test
105 	public void warmStartup() throws Exception {
106 		for (int i = 0; i < 8; i++) {
107 			aoo.kill();//make sure app is closed
108 			aoo.start();
109 			long start = System.currentTimeMillis();
110 			startcenter.waitForExistence(120, INTERVAL);
111 			long end = System.currentTimeMillis();
112 			sleep(2);
113 			addRecord(i, start, end);
114 			app.close();
115 		}
116 	}
117 
118 	@Test
119 	public void newTextDocument() {
120 		for (int i = 0; i < 8; i++) {
121 			aoo.kill();//make sure app is closed
122 			aoo.start();
123 			startCenterWriterButton.waitForExistence(120, 1);
124 			sleep(2);
125 			startCenterWriterButton.click(0.5, 0.5);
126 			long start = System.currentTimeMillis();
127 			writer.waitForExistence(60, INTERVAL);
128 			long end = System.currentTimeMillis();
129 			sleep(2);
130 			addRecord(i, start, end);
131 			app.close();
132 		}
133 	}
134 
135 	@Test
136 	public void newSpreadsheet() {
137 		for (int i = 0; i < 8; i++) {
138 			aoo.kill();//make sure app is closed
139 			aoo.start();
140 			startCenterCalcButton.waitForExistence(120, 1);
141 			sleep(2);
142 			startCenterCalcButton.click(0.5, 0.5);
143 			long start = System.currentTimeMillis();
144 			calc.waitForExistence(60, INTERVAL);
145 			long end = System.currentTimeMillis();
146 			sleep(2);
147 			addRecord(i, start, end);
148 			app.close();
149 		}
150 	}
151 
152 	@Test
153 	public void newPresentation() {
154 		for (int i = 0; i < 8; i++) {
155 			aoo.kill();//make sure app is closed
156 			aoo.start();
157 			startCenterImpressButton.waitForExistence(120, 1);
158 			sleep(2);
159 			startCenterImpressButton.click(0.5, 0.5);
160 			sleep(1);
161 			presentationWizard.click(0.9, 0.95);
162 			long start = System.currentTimeMillis();
163 			impress.waitForExistence(60, INTERVAL);
164 			long end = System.currentTimeMillis();
165 			sleep(2);
166 			addRecord(i, start, end);
167 			app.close();
168 		}
169 	}
170 
171 	@Test
172 	public void loadFinishPlainDoc() {
173 		loadFinishTextDocument("pvt_benchmark/sw_plain_120p.doc", "Page 1 / ");
174 	}
175 
176 	@Test
177 	public void loadFinishPlainOdt() {
178 		loadFinishTextDocument("pvt_benchmark/sw_plain_120p_odf1.2.odt", "Page 1 / ");
179 	}
180 
181 	@Test
182 	public void loadFinishComplexDoc() {
183 		loadFinishTextDocument("pvt_benchmark/sw_complex_100p.doc", "Page 1 / ");
184 	}
185 
186 	@Test
187 	public void loadFinishComplexOdt() {
188 		loadFinishTextDocument("pvt_benchmark/sw_complex_100p_odf1.2.odt", "Page 1 / ");
189 	}
190 
191 	public void loadFinishTextDocument(String file, final String indicator) {
192 		String path = prepareData(file);
193 		aoo.kill();
194 		aoo.start();
195 		startcenter.waitForExistence(120, 1);
196 		for (int i = 0; i < 8; i++) {
197 			app.dispatch(".uno:Open");
198 			filePickerPath.setText(path);
199 			filePickerOpen.click();
200 			long start = System.currentTimeMillis();
201 			new Condition() {
202 
203 				@Override
204 				public boolean value() {
205 					String text = statusBar.getItemText(0);
206 					if (text == null)
207 						return false;
208 					return text.startsWith(indicator);
209 				}
210 
211 			}.waitForTrue("", 120, INTERVAL);
212 			long end = System.currentTimeMillis();
213 			sleep(2);
214 			addRecord(i, start, end);
215 			app.dispatch(".uno:CloseDoc");
216 		}
217 
218 		app.close();
219 	}
220 
221 	@Test
222 	public void loadFinishPlainXLS() {
223 		loadFinishTextDocument("pvt_benchmark/sc_plain_4sh_5kcell.xls", "Sheet 2 / 4");
224 	}
225 
226 	@Test
227 	public void loadFinishPlainODS() {
228 		loadFinishTextDocument("pvt_benchmark/sc_plain_4sh_5kcell_new_odf1.2.ods", "Sheet 1 / 4");
229 	}
230 
231 	@Test
232 	public void loadFinishComplexXLS() {
233 		loadFinishTextDocument("pvt_benchmark/sc_complex_13sh_4kcell.xls", "Sheet 2 / 13");
234 	}
235 
236 	@Test
237 	public void loadFinishComplexODS() {
238 		loadFinishTextDocument("pvt_benchmark/sc_complex_13sh_4kcell_new_odf1.2.ods", "Sheet 6 / 13");
239 	}
240 
241 
242 	@Test
243 	public void loadFinishPlainODP() {
244 		loadFinishTextDocument("pvt_benchmark/sc_complex_13sh_4kcell_new_odf1.2.ods", "Sheet 6 / 13");
245 	}
246 
247 	@Test
248 	public void loadFinishPlainPPT() {
249 		loadFinishTextDocument("pvt_benchmark/sc_complex_13sh_4kcell_new_odf1.2.ods", "Sheet 6 / 13");
250 	}
251 
252 	@Test
253 	public void loadFinishComplexODP() {
254 		loadFinishTextDocument("pvt_benchmark/sc_complex_13sh_4kcell_new_odf1.2.ods", "Sheet 6 / 13");
255 	}
256 
257 	@Test
258 	public void loadFinishComplexPPT() {
259 		loadFinishTextDocument("pvt_benchmark/sc_complex_13sh_4kcell_new_odf1.2.ods", "Sheet 6 / 13");
260 	}
261 
262 	public void loadFinish(String file, final String indicator) {
263 		String path = prepareData(file);
264 		aoo.kill();
265 		aoo.start();
266 		startcenter.waitForExistence(120, 1);
267 		for (int i = 0; i < 8; i++) {
268 			app.dispatch(".uno:Open");
269 			filePickerPath.setText(path);
270 			filePickerOpen.click();
271 			long start = System.currentTimeMillis();
272 			new Condition() {
273 
274 				@Override
275 				public boolean value() {
276 					String text = statusBar.getItemText(0);
277 					if (text == null)
278 						return false;
279 					return text.startsWith(indicator);
280 				}
281 
282 			}.waitForTrue("", 120, INTERVAL);
283 			long end = System.currentTimeMillis();
284 			sleep(2);
285 			addRecord(i, start, end);
286 			app.dispatch(".uno:CloseDoc");
287 		}
288 
289 		app.close();
290 	}
291 
292 
293 }
294