linux.swtbot: Add more tests to SystemCallLatencyTableAnalysisTest
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.ui.swtbot.tests / src / org / eclipse / tracecompass / analysis / os / linux / ui / swtbot / tests / latency / SystemCallLatencyTableAnalysisTest.java
1 /*******************************************************************************
2 * Copyright (c) 2015, 2016 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.analysis.os.linux.ui.swtbot.tests.latency;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertNull;
18 import static org.junit.Assert.assertTrue;
19 import static org.junit.Assert.fail;
20
21 import java.io.BufferedReader;
22 import java.io.ByteArrayOutputStream;
23 import java.io.File;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.Random;
32 import java.util.stream.Collectors;
33
34 import org.apache.log4j.ConsoleAppender;
35 import org.apache.log4j.Logger;
36 import org.apache.log4j.SimpleLayout;
37 import org.eclipse.core.runtime.FileLocator;
38 import org.eclipse.jdt.annotation.NonNull;
39 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
40 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
41 import org.eclipse.swtbot.swt.finder.SWTBot;
42 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
43 import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
44 import org.eclipse.swtbot.swt.finder.results.BoolResult;
45 import org.eclipse.swtbot.swt.finder.results.Result;
46 import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
47 import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
48 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
49 import org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.table.AbstractSegmentStoreTableView;
50 import org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.table.AbstractSegmentStoreTableViewer;
51 import org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.latency.SystemCallLatencyView;
52 import org.eclipse.tracecompass.segmentstore.core.BasicSegment;
53 import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
54 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
55 import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
56 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
57 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
58 import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils;
59 import org.eclipse.ui.IViewPart;
60 import org.eclipse.ui.IViewReference;
61 import org.eclipse.ui.PlatformUI;
62 import org.eclipse.ui.WorkbenchException;
63 import org.junit.After;
64 import org.junit.Before;
65 import org.junit.BeforeClass;
66 import org.junit.Test;
67 import org.junit.runner.RunWith;
68
69 /**
70 * Tests of the latency table
71 *
72 * @author Matthew Khouzam
73 */
74 @RunWith(SWTBotJunit4ClassRunner.class)
75 public class SystemCallLatencyTableAnalysisTest {
76
77 private static final String TRACE_TYPE = "org.eclipse.linuxtools.lttng2.kernel.tracetype";
78 private static final String PROJECT_NAME = "test";
79 private static final String VIEW_ID = SystemCallLatencyView.ID;
80 private static final String TRACING_PERSPECTIVE_ID = "org.eclipse.linuxtools.tmf.ui.perspective";
81
82 /** The Log4j logger instance. */
83 private static final Logger fLogger = Logger.getRootLogger();
84 private SystemCallLatencyView fLatencyView;
85 private AbstractSegmentStoreTableViewer fTable;
86
87 /**
88 * Things to setup
89 */
90 @BeforeClass
91 public static void beforeClass() {
92
93 SWTBotUtils.initialize();
94 Thread.currentThread().setName("SWTBotTest");
95 /* set up for swtbot */
96 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
97 SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
98 fLogger.removeAllAppenders();
99 fLogger.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT));
100 SWTWorkbenchBot bot = new SWTWorkbenchBot();
101 final List<SWTBotView> openViews = bot.views();
102 for (SWTBotView view : openViews) {
103 if (view.getTitle().equals("Welcome")) {
104 view.close();
105 bot.waitUntil(ConditionHelpers.ViewIsClosed(view));
106 }
107 }
108 /* Switch perspectives */
109 switchTracingPerspective();
110 /* Finish waiting for eclipse to load */
111 WaitUtils.waitForJobs();
112
113 }
114
115 /**
116 * Opens a latency table
117 */
118 @Before
119 public void createTable() {
120 /*
121 * Open latency view
122 */
123 SWTBotUtils.openView(VIEW_ID);
124 SWTWorkbenchBot bot = new SWTWorkbenchBot();
125 SWTBotView viewBot = bot.viewById(VIEW_ID);
126 final IViewReference viewReference = viewBot.getViewReference();
127 IViewPart viewPart = UIThreadRunnable.syncExec(new Result<IViewPart>() {
128 @Override
129 public IViewPart run() {
130 return viewReference.getView(true);
131 }
132 });
133 assertNotNull(viewPart);
134 if (!(viewPart instanceof SystemCallLatencyView)) {
135 fail("Could not instanciate view");
136 }
137 fLatencyView = (SystemCallLatencyView) viewPart;
138 fTable = fLatencyView.getSegmentStoreViewer();
139 assertNotNull(fTable);
140 }
141
142 /**
143 * Closes the view
144 */
145 @After
146 public void closeTable() {
147 final SWTWorkbenchBot swtWorkbenchBot = new SWTWorkbenchBot();
148 SWTBotView viewBot = swtWorkbenchBot.viewById(VIEW_ID);
149 viewBot.close();
150 }
151
152 private static void switchTracingPerspective() {
153 final Exception retE[] = new Exception[1];
154 if (!UIThreadRunnable.syncExec(new BoolResult() {
155 @Override
156 public Boolean run() {
157 try {
158 PlatformUI.getWorkbench().showPerspective(TRACING_PERSPECTIVE_ID,
159 PlatformUI.getWorkbench().getActiveWorkbenchWindow());
160 } catch (WorkbenchException e) {
161 retE[0] = e;
162 return false;
163 }
164 return true;
165 }
166 })) {
167 fail(retE[0].getMessage());
168 }
169
170 }
171
172 /**
173 * Test incrementing
174 */
175 @Test
176 public void climbTest() {
177 List<@NonNull BasicSegment> fixture = new ArrayList<>();
178 for (int i = 0; i < 100; i++) {
179 fixture.add(new BasicSegment(i, 2 * i));
180 }
181
182 assertNotNull(fTable);
183 fTable.updateModel(fixture);
184 SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable());
185 SWTBot bot = new SWTBot();
186 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2));
187 tableBot.header("Duration").click();
188 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2));
189 tableBot.header("Duration").click();
190 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "99", 0, 2));
191 }
192
193 /**
194 * Test decrementing
195 */
196 @Test
197 public void decrementingTest() {
198 List<@NonNull BasicSegment> fixture = new ArrayList<>();
199 for (int i = 100; i >= 0; i--) {
200 fixture.add(new BasicSegment(i, 2 * i));
201 }
202 assertNotNull(fTable);
203 fTable.updateModel(fixture);
204 SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable());
205 SWTBot bot = new SWTBot();
206 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "100", 0, 2));
207 tableBot.header("Duration").click();
208 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2));
209 tableBot.header("Duration").click();
210 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "100", 0, 2));
211 }
212
213 /**
214 * Test small table
215 */
216 @Test
217 public void smallTest() {
218 List<@NonNull BasicSegment> fixture = new ArrayList<>();
219 for (int i = 1; i >= 0; i--) {
220 fixture.add(new BasicSegment(i, 2 * i));
221 }
222 assertNotNull(fTable);
223 fTable.updateModel(fixture);
224 SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable());
225 SWTBot bot = new SWTBot();
226 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "1", 0, 2));
227 tableBot.header("Duration").click();
228 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2));
229 tableBot.header("Duration").click();
230 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "1", 0, 2));
231 }
232
233 /**
234 * Test large
235 */
236 @Test
237 public void largeTest() {
238 final int size = 1000000;
239 BasicSegment[] fixture = new BasicSegment[size];
240 for (int i = 0; i < size; i++) {
241 fixture[i] = (new BasicSegment(i, 2 * i));
242 }
243 assertNotNull(fTable);
244 fTable.updateModel(fixture);
245 SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable());
246 SWTBot bot = new SWTBot();
247 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2));
248 tableBot.header("Duration").click();
249 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2));
250 tableBot.header("Duration").click();
251 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "999,999", 0, 2));
252 }
253
254 /**
255 * Test noise
256 */
257 @Test
258 public void noiseTest() {
259 Random rnd = new Random();
260 rnd.setSeed(1234);
261 final int size = 1000000;
262 BasicSegment[] fixture = new BasicSegment[size];
263 for (int i = 0; i < size; i++) {
264 int start = Math.abs(rnd.nextInt(100000000));
265 int end = start + Math.abs(rnd.nextInt(1000000));
266 fixture[i] = (new BasicSegment(start, end));
267 }
268 assertNotNull(fTable);
269 fTable.updateModel(fixture);
270 SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable());
271 SWTBot bot = new SWTBot();
272 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "894,633", 0, 2));
273 tableBot.header("Duration").click();
274 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2));
275 tableBot.header("Duration").click();
276 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "999,999", 0, 2));
277 }
278
279 /**
280 * Test gaussian noise
281 */
282 @Test
283 public void gaussianNoiseTest() {
284 Random rnd = new Random();
285 rnd.setSeed(1234);
286 List<@NonNull BasicSegment> fixture = new ArrayList<>();
287 for (int i = 1; i <= 1000000; i++) {
288 int start = Math.abs(rnd.nextInt(100000000));
289 final int delta = Math.abs(rnd.nextInt(1000));
290 int end = start + delta * delta;
291 fixture.add(new BasicSegment(start, end));
292 }
293 assertNotNull(fTable);
294 fTable.updateModel(fixture);
295 SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable());
296 SWTBot bot = new SWTBot();
297 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "400,689", 0, 2));
298 tableBot.header("Duration").click();
299 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2));
300 tableBot.header("Duration").click();
301 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "998,001", 0, 2));
302 }
303
304 /**
305 * Test creating a tsv
306 *
307 * @throws NoSuchMethodException
308 * Error creating the tsv
309 * @throws IOException
310 * no such file or the file is locked.
311 */
312 @Test
313 public void testWriteToTsv() throws NoSuchMethodException, IOException {
314
315 List<@NonNull BasicSegment> fixture = new ArrayList<>();
316 for (int i = 1; i <= 20; i++) {
317 int start = i;
318 final int delta = i;
319 int end = start + delta * delta;
320 fixture.add(new BasicSegment(start, end));
321 }
322 assertNotNull(fTable);
323 fTable.updateModel(fixture);
324 SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable());
325 SWTBot bot = new SWTBot();
326 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "1", 0, 2));
327 SWTWorkbenchBot swtWorkbenchBot = new SWTWorkbenchBot();
328 SWTBotView viewBot = swtWorkbenchBot.viewById(VIEW_ID);
329 List<String> actionResult = Arrays.asList(testToTsv(viewBot));
330 String absolutePath = TmfTraceManager.getTemporaryDirPath() + File.separator + "syscallLatencyTest.testWriteToTsv.tsv";
331 TmfFileDialogFactory.setOverrideFiles(absolutePath);
332 SWTBotMenu menuBot = viewBot.viewMenu().menu("Export to TSV");
333 try {
334 assertTrue(menuBot.isEnabled());
335 assertTrue(menuBot.isVisible());
336 menuBot.click();
337
338 try (BufferedReader br = new BufferedReader(new FileReader(absolutePath))) {
339 List<String> lines = br.lines().collect(Collectors.toList());
340 assertEquals("Both reads", actionResult, lines);
341 }
342 } finally {
343 new File(absolutePath).delete();
344 }
345
346 }
347
348 private String[] testToTsv(SWTBotView view) throws NoSuchMethodException {
349 ByteArrayOutputStream os = new ByteArrayOutputStream();
350 assertNotNull(os);
351 Class<@NonNull AbstractSegmentStoreTableView> clazz = AbstractSegmentStoreTableView.class;
352 Method method = clazz.getDeclaredMethod("exportToTsv", java.io.OutputStream.class);
353 method.setAccessible(true);
354 final Exception[] except = new Exception[1];
355 UIThreadRunnable.syncExec(() -> {
356 try {
357 method.invoke(fLatencyView, os);
358 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
359 except[0] = e;
360 }
361 });
362 assertNull(except[0]);
363 @SuppressWarnings("null")
364 String[] lines = String.valueOf(os).split(System.getProperty("line.separator"));
365 assertNotNull(lines);
366 assertEquals("number of lines", 21, lines.length);
367 assertEquals("header", "Start Time\tEnd Time\tDuration", lines[0]);
368 // not a straight up string compare due to time zones. Kathmandu and
369 // Eucla have 15 minute time zones.
370 assertTrue("line 1", lines[1].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s001\\t\\d\\d:\\d\\d:00.000 000 002\\t1"));
371 assertTrue("line 2", lines[2].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s002\\t\\d\\d:\\d\\d:00.000 000 006\\t4"));
372 assertTrue("line 3", lines[3].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s003\\t\\d\\d:\\d\\d:00.000 000 012\\t9"));
373 assertTrue("line 4", lines[4].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s004\\t\\d\\d:\\d\\d:00.000 000 020\\t16"));
374 assertTrue("line 5", lines[5].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s005\\t\\d\\d:\\d\\d:00.000 000 030\\t25"));
375 assertTrue("line 6", lines[6].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s006\\t\\d\\d:\\d\\d:00.000 000 042\\t36"));
376 assertTrue("line 7", lines[7].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s007\\t\\d\\d:\\d\\d:00.000 000 056\\t49"));
377 assertTrue("line 8", lines[8].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s008\\t\\d\\d:\\d\\d:00.000 000 072\\t64"));
378 assertTrue("line 9", lines[9].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s009\\t\\d\\d:\\d\\d:00.000 000 090\\t81"));
379 assertTrue("line 10", lines[10].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s010\\t\\d\\d:\\d\\d:00.000 000 110\\t100"));
380 assertTrue("line 11", lines[11].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s011\\t\\d\\d:\\d\\d:00.000 000 132\\t121"));
381 assertTrue("line 12", lines[12].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s012\\t\\d\\d:\\d\\d:00.000 000 156\\t144"));
382 assertTrue("line 13", lines[13].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s013\\t\\d\\d:\\d\\d:00.000 000 182\\t169"));
383 assertTrue("line 14", lines[14].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s014\\t\\d\\d:\\d\\d:00.000 000 210\\t196"));
384 assertTrue("line 15", lines[15].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s015\\t\\d\\d:\\d\\d:00.000 000 240\\t225"));
385 assertTrue("line 16", lines[16].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s016\\t\\d\\d:\\d\\d:00.000 000 272\\t256"));
386 assertTrue("line 17", lines[17].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s017\\t\\d\\d:\\d\\d:00.000 000 306\\t289"));
387 assertTrue("line 18", lines[18].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s018\\t\\d\\d:\\d\\d:00.000 000 342\\t324"));
388 assertTrue("line 19", lines[19].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s019\\t\\d\\d:\\d\\d:00.000 000 380\\t361"));
389 assertTrue("line 20", lines[20].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s020\\t\\d\\d:\\d\\d:00.000 000 420\\t400"));
390 return lines;
391 }
392
393 /**
394 * Test with an actual trace, this is more of an integration test than a
395 * unit test. This test is a slow one too. If some analyses are not well
396 * configured, this test will also generates null pointer exceptions. These
397 * are will be logged.
398 *
399 * @throws IOException
400 * trace not found?
401 */
402 @Test
403 public void testWithTrace() throws IOException {
404 String tracePath;
405 tracePath = FileLocator.toFileURL(CtfTestTrace.ARM_64_BIT_HEADER.getTraceURL()).getPath();
406 SWTWorkbenchBot bot = new SWTWorkbenchBot();
407 SWTBotView view = bot.viewById(VIEW_ID);
408 view.close();
409 bot.waitUntil(ConditionHelpers.ViewIsClosed(view));
410 SWTBotUtils.createProject(PROJECT_NAME);
411 SWTBotUtils.openTrace(PROJECT_NAME, tracePath, TRACE_TYPE);
412 WaitUtils.waitForJobs();
413 createTable();
414 WaitUtils.waitForJobs();
415 SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable());
416 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "24,100", 0, 2));
417 tableBot.header("Duration").click();
418 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "1,000", 0, 2));
419 tableBot.header("Duration").click();
420 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "5,904,091,700", 0, 2));
421 bot.closeAllEditors();
422 SWTBotUtils.deleteProject(PROJECT_NAME, bot);
423 }
424 }
This page took 0.04045 seconds and 5 git commands to generate.