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
CommitLineData
b70c55af 1/*******************************************************************************
edded5c1 2 * Copyright (c) 2015, 2016 Ericsson
b70c55af
MK
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
13package org.eclipse.tracecompass.analysis.os.linux.ui.swtbot.tests.latency;
14
b6fddb83
MK
15import static org.junit.Assert.assertEquals;
16import static org.junit.Assert.assertNotNull;
17import static org.junit.Assert.assertNull;
18import static org.junit.Assert.assertTrue;
19import static org.junit.Assert.fail;
b70c55af 20
b6fddb83 21import java.io.BufferedReader;
37b7faba 22import java.io.ByteArrayOutputStream;
b6fddb83
MK
23import java.io.File;
24import java.io.FileReader;
973f89f2 25import java.io.IOException;
37b7faba
MK
26import java.lang.reflect.InvocationTargetException;
27import java.lang.reflect.Method;
94d1ce7d 28import java.util.ArrayList;
b6fddb83 29import java.util.Arrays;
94d1ce7d
MK
30import java.util.List;
31import java.util.Random;
b6fddb83 32import java.util.stream.Collectors;
94d1ce7d
MK
33
34import org.apache.log4j.ConsoleAppender;
35import org.apache.log4j.Logger;
36import org.apache.log4j.SimpleLayout;
973f89f2 37import org.eclipse.core.runtime.FileLocator;
94d1ce7d
MK
38import org.eclipse.jdt.annotation.NonNull;
39import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
40import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
e92fbda6 41import org.eclipse.swtbot.swt.finder.SWTBot;
94d1ce7d 42import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
b70c55af 43import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
94d1ce7d
MK
44import org.eclipse.swtbot.swt.finder.results.BoolResult;
45import org.eclipse.swtbot.swt.finder.results.Result;
46import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
37b7faba 47import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
94d1ce7d 48import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
37b7faba 49import org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.table.AbstractSegmentStoreTableView;
edded5c1 50import org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.table.AbstractSegmentStoreTableViewer;
94d1ce7d 51import org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.latency.SystemCallLatencyView;
660d4ed9 52import org.eclipse.tracecompass.segmentstore.core.BasicSegment;
973f89f2 53import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
b6fddb83
MK
54import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
55import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
94d1ce7d
MK
56import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
57import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
f0beeb4a 58import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils;
94d1ce7d
MK
59import org.eclipse.ui.IViewPart;
60import org.eclipse.ui.IViewReference;
61import org.eclipse.ui.PlatformUI;
62import org.eclipse.ui.WorkbenchException;
63import org.junit.After;
64import org.junit.Before;
65import org.junit.BeforeClass;
b70c55af
MK
66import org.junit.Test;
67import org.junit.runner.RunWith;
68
69/**
70 * Tests of the latency table
94d1ce7d 71 *
b70c55af
MK
72 * @author Matthew Khouzam
73 */
74@RunWith(SWTBotJunit4ClassRunner.class)
75public class SystemCallLatencyTableAnalysisTest {
76
973f89f2
MK
77 private static final String TRACE_TYPE = "org.eclipse.linuxtools.lttng2.kernel.tracetype";
78 private static final String PROJECT_NAME = "test";
94d1ce7d
MK
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 */
f0beeb4a 111 WaitUtils.waitForJobs();
94d1ce7d
MK
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() {
660d4ed9 177 List<@NonNull BasicSegment> fixture = new ArrayList<>();
94d1ce7d 178 for (int i = 0; i < 100; i++) {
660d4ed9 179 fixture.add(new BasicSegment(i, 2 * i));
94d1ce7d
MK
180 }
181
182 assertNotNull(fTable);
183 fTable.updateModel(fixture);
184 SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable());
e92fbda6
MAL
185 SWTBot bot = new SWTBot();
186 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2));
94d1ce7d 187 tableBot.header("Duration").click();
e92fbda6 188 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2));
94d1ce7d 189 tableBot.header("Duration").click();
e92fbda6 190 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "99", 0, 2));
94d1ce7d
MK
191 }
192
193 /**
194 * Test decrementing
195 */
196 @Test
197 public void decrementingTest() {
660d4ed9 198 List<@NonNull BasicSegment> fixture = new ArrayList<>();
94d1ce7d 199 for (int i = 100; i >= 0; i--) {
660d4ed9 200 fixture.add(new BasicSegment(i, 2 * i));
94d1ce7d
MK
201 }
202 assertNotNull(fTable);
203 fTable.updateModel(fixture);
204 SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable());
e92fbda6
MAL
205 SWTBot bot = new SWTBot();
206 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "100", 0, 2));
94d1ce7d 207 tableBot.header("Duration").click();
e92fbda6 208 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2));
94d1ce7d 209 tableBot.header("Duration").click();
e92fbda6 210 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "100", 0, 2));
94d1ce7d
MK
211 }
212
213 /**
214 * Test small table
215 */
216 @Test
217 public void smallTest() {
660d4ed9 218 List<@NonNull BasicSegment> fixture = new ArrayList<>();
94d1ce7d 219 for (int i = 1; i >= 0; i--) {
660d4ed9 220 fixture.add(new BasicSegment(i, 2 * i));
94d1ce7d
MK
221 }
222 assertNotNull(fTable);
223 fTable.updateModel(fixture);
224 SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable());
e92fbda6
MAL
225 SWTBot bot = new SWTBot();
226 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "1", 0, 2));
94d1ce7d 227 tableBot.header("Duration").click();
e92fbda6 228 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2));
94d1ce7d 229 tableBot.header("Duration").click();
e92fbda6 230 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "1", 0, 2));
94d1ce7d
MK
231 }
232
233 /**
234 * Test large
235 */
236 @Test
237 public void largeTest() {
238 final int size = 1000000;
660d4ed9 239 BasicSegment[] fixture = new BasicSegment[size];
94d1ce7d 240 for (int i = 0; i < size; i++) {
660d4ed9 241 fixture[i] = (new BasicSegment(i, 2 * i));
94d1ce7d
MK
242 }
243 assertNotNull(fTable);
244 fTable.updateModel(fixture);
245 SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable());
e92fbda6
MAL
246 SWTBot bot = new SWTBot();
247 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2));
94d1ce7d 248 tableBot.header("Duration").click();
e92fbda6 249 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2));
94d1ce7d 250 tableBot.header("Duration").click();
ef47abba 251 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "999,999", 0, 2));
94d1ce7d
MK
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;
660d4ed9 262 BasicSegment[] fixture = new BasicSegment[size];
94d1ce7d
MK
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));
660d4ed9 266 fixture[i] = (new BasicSegment(start, end));
94d1ce7d
MK
267 }
268 assertNotNull(fTable);
269 fTable.updateModel(fixture);
270 SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable());
e92fbda6 271 SWTBot bot = new SWTBot();
ef47abba 272 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "894,633", 0, 2));
94d1ce7d 273 tableBot.header("Duration").click();
e92fbda6 274 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2));
94d1ce7d 275 tableBot.header("Duration").click();
ef47abba 276 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "999,999", 0, 2));
94d1ce7d
MK
277 }
278
b70c55af 279 /**
94d1ce7d 280 * Test gaussian noise
b70c55af
MK
281 */
282 @Test
94d1ce7d
MK
283 public void gaussianNoiseTest() {
284 Random rnd = new Random();
285 rnd.setSeed(1234);
660d4ed9 286 List<@NonNull BasicSegment> fixture = new ArrayList<>();
94d1ce7d
MK
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;
660d4ed9 291 fixture.add(new BasicSegment(start, end));
94d1ce7d
MK
292 }
293 assertNotNull(fTable);
294 fTable.updateModel(fixture);
295 SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable());
e92fbda6 296 SWTBot bot = new SWTBot();
ef47abba 297 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "400,689", 0, 2));
94d1ce7d 298 tableBot.header("Duration").click();
e92fbda6 299 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2));
94d1ce7d 300 tableBot.header("Duration").click();
ef47abba 301 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "998,001", 0, 2));
b70c55af
MK
302 }
303
37b7faba
MK
304 /**
305 * Test creating a tsv
306 *
307 * @throws NoSuchMethodException
308 * Error creating the tsv
b6fddb83
MK
309 * @throws IOException
310 * no such file or the file is locked.
37b7faba
MK
311 */
312 @Test
b6fddb83
MK
313 public void testWriteToTsv() throws NoSuchMethodException, IOException {
314
37b7faba
MK
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);
b6fddb83
MK
329 List<String> actionResult = Arrays.asList(testToTsv(viewBot));
330 String absolutePath = TmfTraceManager.getTemporaryDirPath() + File.separator + "syscallLatencyTest.testWriteToTsv.tsv";
331 TmfFileDialogFactory.setOverrideFiles(absolutePath);
37b7faba 332 SWTBotMenu menuBot = viewBot.viewMenu().menu("Export to TSV");
b6fddb83
MK
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
37b7faba
MK
346 }
347
b6fddb83 348 private String[] testToTsv(SWTBotView view) throws NoSuchMethodException {
37b7faba
MK
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]);
b6fddb83
MK
368 // not a straight up string compare due to time zones. Kathmandu and
369 // Eucla have 15 minute time zones.
37b7faba 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"));
5e5db534
MK
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"));
b6fddb83 390 return lines;
37b7faba
MK
391 }
392
973f89f2
MK
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);
f0beeb4a 412 WaitUtils.waitForJobs();
973f89f2 413 createTable();
f0beeb4a 414 WaitUtils.waitForJobs();
973f89f2 415 SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable());
ef47abba 416 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "24,100", 0, 2));
973f89f2 417 tableBot.header("Duration").click();
ef47abba 418 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "1,000", 0, 2));
973f89f2 419 tableBot.header("Duration").click();
ef47abba 420 bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "5,904,091,700", 0, 2));
973f89f2
MK
421 bot.closeAllEditors();
422 SWTBotUtils.deleteProject(PROJECT_NAME, bot);
423 }
b70c55af 424}
This page took 0.06428 seconds and 5 git commands to generate.