Commit | Line | Data |
---|---|---|
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 | ||
13 | package org.eclipse.tracecompass.analysis.os.linux.ui.swtbot.tests.latency; | |
14 | ||
37b7faba | 15 | import static org.junit.Assert.*; |
b70c55af | 16 | |
37b7faba | 17 | import java.io.ByteArrayOutputStream; |
973f89f2 | 18 | import java.io.IOException; |
37b7faba MK |
19 | import java.lang.reflect.InvocationTargetException; |
20 | import java.lang.reflect.Method; | |
94d1ce7d | 21 | import java.util.ArrayList; |
94d1ce7d MK |
22 | import java.util.List; |
23 | import java.util.Random; | |
24 | ||
25 | import org.apache.log4j.ConsoleAppender; | |
26 | import org.apache.log4j.Logger; | |
27 | import org.apache.log4j.SimpleLayout; | |
973f89f2 | 28 | import org.eclipse.core.runtime.FileLocator; |
94d1ce7d MK |
29 | import org.eclipse.jdt.annotation.NonNull; |
30 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; | |
31 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; | |
e92fbda6 | 32 | import org.eclipse.swtbot.swt.finder.SWTBot; |
94d1ce7d | 33 | import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; |
b70c55af | 34 | import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner; |
94d1ce7d MK |
35 | import org.eclipse.swtbot.swt.finder.results.BoolResult; |
36 | import org.eclipse.swtbot.swt.finder.results.Result; | |
37 | import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; | |
37b7faba | 38 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu; |
94d1ce7d | 39 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable; |
37b7faba | 40 | import org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.table.AbstractSegmentStoreTableView; |
edded5c1 | 41 | import org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.table.AbstractSegmentStoreTableViewer; |
94d1ce7d | 42 | import org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.latency.SystemCallLatencyView; |
660d4ed9 | 43 | import org.eclipse.tracecompass.segmentstore.core.BasicSegment; |
973f89f2 | 44 | import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace; |
94d1ce7d MK |
45 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers; |
46 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils; | |
47 | import org.eclipse.ui.IViewPart; | |
48 | import org.eclipse.ui.IViewReference; | |
49 | import org.eclipse.ui.PlatformUI; | |
50 | import org.eclipse.ui.WorkbenchException; | |
51 | import org.junit.After; | |
52 | import org.junit.Before; | |
53 | import org.junit.BeforeClass; | |
b70c55af MK |
54 | import org.junit.Test; |
55 | import org.junit.runner.RunWith; | |
56 | ||
57 | /** | |
58 | * Tests of the latency table | |
94d1ce7d | 59 | * |
b70c55af MK |
60 | * @author Matthew Khouzam |
61 | */ | |
62 | @RunWith(SWTBotJunit4ClassRunner.class) | |
63 | public class SystemCallLatencyTableAnalysisTest { | |
64 | ||
973f89f2 MK |
65 | private static final String TRACE_TYPE = "org.eclipse.linuxtools.lttng2.kernel.tracetype"; |
66 | private static final String PROJECT_NAME = "test"; | |
94d1ce7d MK |
67 | private static final String VIEW_ID = SystemCallLatencyView.ID; |
68 | private static final String TRACING_PERSPECTIVE_ID = "org.eclipse.linuxtools.tmf.ui.perspective"; | |
69 | ||
70 | /** The Log4j logger instance. */ | |
71 | private static final Logger fLogger = Logger.getRootLogger(); | |
72 | private SystemCallLatencyView fLatencyView; | |
73 | private AbstractSegmentStoreTableViewer fTable; | |
74 | ||
75 | /** | |
76 | * Things to setup | |
77 | */ | |
78 | @BeforeClass | |
79 | public static void beforeClass() { | |
80 | ||
81 | SWTBotUtils.initialize(); | |
82 | Thread.currentThread().setName("SWTBotTest"); | |
83 | /* set up for swtbot */ | |
84 | SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */ | |
85 | SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US"; | |
86 | fLogger.removeAllAppenders(); | |
87 | fLogger.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT)); | |
88 | SWTWorkbenchBot bot = new SWTWorkbenchBot(); | |
89 | final List<SWTBotView> openViews = bot.views(); | |
90 | for (SWTBotView view : openViews) { | |
91 | if (view.getTitle().equals("Welcome")) { | |
92 | view.close(); | |
93 | bot.waitUntil(ConditionHelpers.ViewIsClosed(view)); | |
94 | } | |
95 | } | |
96 | /* Switch perspectives */ | |
97 | switchTracingPerspective(); | |
98 | /* Finish waiting for eclipse to load */ | |
99 | SWTBotUtils.waitForJobs(); | |
100 | ||
101 | } | |
102 | ||
103 | /** | |
104 | * Opens a latency table | |
105 | */ | |
106 | @Before | |
107 | public void createTable() { | |
108 | /* | |
109 | * Open latency view | |
110 | */ | |
111 | SWTBotUtils.openView(VIEW_ID); | |
112 | SWTWorkbenchBot bot = new SWTWorkbenchBot(); | |
113 | SWTBotView viewBot = bot.viewById(VIEW_ID); | |
114 | final IViewReference viewReference = viewBot.getViewReference(); | |
115 | IViewPart viewPart = UIThreadRunnable.syncExec(new Result<IViewPart>() { | |
116 | @Override | |
117 | public IViewPart run() { | |
118 | return viewReference.getView(true); | |
119 | } | |
120 | }); | |
121 | assertNotNull(viewPart); | |
122 | if (!(viewPart instanceof SystemCallLatencyView)) { | |
123 | fail("Could not instanciate view"); | |
124 | } | |
125 | fLatencyView = (SystemCallLatencyView) viewPart; | |
126 | fTable = fLatencyView.getSegmentStoreViewer(); | |
127 | assertNotNull(fTable); | |
128 | } | |
129 | ||
130 | /** | |
131 | * Closes the view | |
132 | */ | |
133 | @After | |
134 | public void closeTable() { | |
135 | final SWTWorkbenchBot swtWorkbenchBot = new SWTWorkbenchBot(); | |
136 | SWTBotView viewBot = swtWorkbenchBot.viewById(VIEW_ID); | |
137 | viewBot.close(); | |
138 | } | |
139 | ||
140 | private static void switchTracingPerspective() { | |
141 | final Exception retE[] = new Exception[1]; | |
142 | if (!UIThreadRunnable.syncExec(new BoolResult() { | |
143 | @Override | |
144 | public Boolean run() { | |
145 | try { | |
146 | PlatformUI.getWorkbench().showPerspective(TRACING_PERSPECTIVE_ID, | |
147 | PlatformUI.getWorkbench().getActiveWorkbenchWindow()); | |
148 | } catch (WorkbenchException e) { | |
149 | retE[0] = e; | |
150 | return false; | |
151 | } | |
152 | return true; | |
153 | } | |
154 | })) { | |
155 | fail(retE[0].getMessage()); | |
156 | } | |
157 | ||
158 | } | |
159 | ||
160 | /** | |
161 | * Test incrementing | |
162 | */ | |
163 | @Test | |
164 | public void climbTest() { | |
660d4ed9 | 165 | List<@NonNull BasicSegment> fixture = new ArrayList<>(); |
94d1ce7d | 166 | for (int i = 0; i < 100; i++) { |
660d4ed9 | 167 | fixture.add(new BasicSegment(i, 2 * i)); |
94d1ce7d MK |
168 | } |
169 | ||
170 | assertNotNull(fTable); | |
171 | fTable.updateModel(fixture); | |
172 | SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable()); | |
e92fbda6 MAL |
173 | SWTBot bot = new SWTBot(); |
174 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2)); | |
94d1ce7d | 175 | tableBot.header("Duration").click(); |
e92fbda6 | 176 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2)); |
94d1ce7d | 177 | tableBot.header("Duration").click(); |
e92fbda6 | 178 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "99", 0, 2)); |
94d1ce7d MK |
179 | } |
180 | ||
181 | /** | |
182 | * Test decrementing | |
183 | */ | |
184 | @Test | |
185 | public void decrementingTest() { | |
660d4ed9 | 186 | List<@NonNull BasicSegment> fixture = new ArrayList<>(); |
94d1ce7d | 187 | for (int i = 100; i >= 0; i--) { |
660d4ed9 | 188 | fixture.add(new BasicSegment(i, 2 * i)); |
94d1ce7d MK |
189 | } |
190 | assertNotNull(fTable); | |
191 | fTable.updateModel(fixture); | |
192 | SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable()); | |
e92fbda6 MAL |
193 | SWTBot bot = new SWTBot(); |
194 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "100", 0, 2)); | |
94d1ce7d | 195 | tableBot.header("Duration").click(); |
e92fbda6 | 196 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2)); |
94d1ce7d | 197 | tableBot.header("Duration").click(); |
e92fbda6 | 198 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "100", 0, 2)); |
94d1ce7d MK |
199 | } |
200 | ||
201 | /** | |
202 | * Test small table | |
203 | */ | |
204 | @Test | |
205 | public void smallTest() { | |
660d4ed9 | 206 | List<@NonNull BasicSegment> fixture = new ArrayList<>(); |
94d1ce7d | 207 | for (int i = 1; i >= 0; i--) { |
660d4ed9 | 208 | fixture.add(new BasicSegment(i, 2 * i)); |
94d1ce7d MK |
209 | } |
210 | assertNotNull(fTable); | |
211 | fTable.updateModel(fixture); | |
212 | SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable()); | |
e92fbda6 MAL |
213 | SWTBot bot = new SWTBot(); |
214 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "1", 0, 2)); | |
94d1ce7d | 215 | tableBot.header("Duration").click(); |
e92fbda6 | 216 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2)); |
94d1ce7d | 217 | tableBot.header("Duration").click(); |
e92fbda6 | 218 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "1", 0, 2)); |
94d1ce7d MK |
219 | } |
220 | ||
221 | /** | |
222 | * Test large | |
223 | */ | |
224 | @Test | |
225 | public void largeTest() { | |
226 | final int size = 1000000; | |
660d4ed9 | 227 | BasicSegment[] fixture = new BasicSegment[size]; |
94d1ce7d | 228 | for (int i = 0; i < size; i++) { |
660d4ed9 | 229 | fixture[i] = (new BasicSegment(i, 2 * i)); |
94d1ce7d MK |
230 | } |
231 | assertNotNull(fTable); | |
232 | fTable.updateModel(fixture); | |
233 | SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable()); | |
e92fbda6 MAL |
234 | SWTBot bot = new SWTBot(); |
235 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2)); | |
94d1ce7d | 236 | tableBot.header("Duration").click(); |
e92fbda6 | 237 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2)); |
94d1ce7d | 238 | tableBot.header("Duration").click(); |
ef47abba | 239 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "999,999", 0, 2)); |
94d1ce7d MK |
240 | } |
241 | ||
242 | /** | |
243 | * Test noise | |
244 | */ | |
245 | @Test | |
246 | public void noiseTest() { | |
247 | Random rnd = new Random(); | |
248 | rnd.setSeed(1234); | |
249 | final int size = 1000000; | |
660d4ed9 | 250 | BasicSegment[] fixture = new BasicSegment[size]; |
94d1ce7d MK |
251 | for (int i = 0; i < size; i++) { |
252 | int start = Math.abs(rnd.nextInt(100000000)); | |
253 | int end = start + Math.abs(rnd.nextInt(1000000)); | |
660d4ed9 | 254 | fixture[i] = (new BasicSegment(start, end)); |
94d1ce7d MK |
255 | } |
256 | assertNotNull(fTable); | |
257 | fTable.updateModel(fixture); | |
258 | SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable()); | |
e92fbda6 | 259 | SWTBot bot = new SWTBot(); |
ef47abba | 260 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "894,633", 0, 2)); |
94d1ce7d | 261 | tableBot.header("Duration").click(); |
e92fbda6 | 262 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2)); |
94d1ce7d | 263 | tableBot.header("Duration").click(); |
ef47abba | 264 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "999,999", 0, 2)); |
94d1ce7d MK |
265 | } |
266 | ||
b70c55af | 267 | /** |
94d1ce7d | 268 | * Test gaussian noise |
b70c55af MK |
269 | */ |
270 | @Test | |
94d1ce7d MK |
271 | public void gaussianNoiseTest() { |
272 | Random rnd = new Random(); | |
273 | rnd.setSeed(1234); | |
660d4ed9 | 274 | List<@NonNull BasicSegment> fixture = new ArrayList<>(); |
94d1ce7d MK |
275 | for (int i = 1; i <= 1000000; i++) { |
276 | int start = Math.abs(rnd.nextInt(100000000)); | |
277 | final int delta = Math.abs(rnd.nextInt(1000)); | |
278 | int end = start + delta * delta; | |
660d4ed9 | 279 | fixture.add(new BasicSegment(start, end)); |
94d1ce7d MK |
280 | } |
281 | assertNotNull(fTable); | |
282 | fTable.updateModel(fixture); | |
283 | SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable()); | |
e92fbda6 | 284 | SWTBot bot = new SWTBot(); |
ef47abba | 285 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "400,689", 0, 2)); |
94d1ce7d | 286 | tableBot.header("Duration").click(); |
e92fbda6 | 287 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "0", 0, 2)); |
94d1ce7d | 288 | tableBot.header("Duration").click(); |
ef47abba | 289 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "998,001", 0, 2)); |
b70c55af MK |
290 | } |
291 | ||
37b7faba MK |
292 | /** |
293 | * Test creating a tsv | |
294 | * | |
295 | * @throws NoSuchMethodException | |
296 | * Error creating the tsv | |
297 | */ | |
298 | @Test | |
299 | public void testWriteToTsv() throws NoSuchMethodException { | |
300 | List<@NonNull BasicSegment> fixture = new ArrayList<>(); | |
301 | for (int i = 1; i <= 20; i++) { | |
302 | int start = i; | |
303 | final int delta = i; | |
304 | int end = start + delta * delta; | |
305 | fixture.add(new BasicSegment(start, end)); | |
306 | } | |
307 | assertNotNull(fTable); | |
308 | fTable.updateModel(fixture); | |
309 | SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable()); | |
310 | SWTBot bot = new SWTBot(); | |
311 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "1", 0, 2)); | |
312 | SWTWorkbenchBot swtWorkbenchBot = new SWTWorkbenchBot(); | |
313 | SWTBotView viewBot = swtWorkbenchBot.viewById(VIEW_ID); | |
314 | testToTsv(viewBot); | |
315 | SWTBotMenu menuBot = viewBot.viewMenu().menu("Export to TSV"); | |
316 | assertTrue(menuBot.isEnabled()); | |
317 | assertTrue(menuBot.isVisible()); | |
318 | } | |
319 | ||
320 | private void testToTsv(SWTBotView view) throws NoSuchMethodException { | |
321 | ByteArrayOutputStream os = new ByteArrayOutputStream(); | |
322 | assertNotNull(os); | |
323 | Class<@NonNull AbstractSegmentStoreTableView> clazz = AbstractSegmentStoreTableView.class; | |
324 | Method method = clazz.getDeclaredMethod("exportToTsv", java.io.OutputStream.class); | |
325 | method.setAccessible(true); | |
326 | final Exception[] except = new Exception[1]; | |
327 | UIThreadRunnable.syncExec(() -> { | |
328 | try { | |
329 | method.invoke(fLatencyView, os); | |
330 | } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { | |
331 | except[0] = e; | |
332 | } | |
333 | }); | |
334 | assertNull(except[0]); | |
335 | @SuppressWarnings("null") | |
336 | String[] lines = String.valueOf(os).split(System.getProperty("line.separator")); | |
337 | assertNotNull(lines); | |
338 | assertEquals("number of lines", 21, lines.length); | |
339 | assertEquals("header", "Start Time\tEnd Time\tDuration", lines[0]); | |
340 | // not a straight up string compare due to time zones. Kathmandu and Eucla have 15 minute time zones. | |
341 | assertTrue("line 1", lines[1].matches("\\d\\d:\\d\\d:00\\.000\\s000\\s001\\t\\d\\d:\\d\\d:00.000 000 002\\t1")); | |
342 | } | |
343 | ||
973f89f2 MK |
344 | /** |
345 | * Test with an actual trace, this is more of an integration test than a | |
346 | * unit test. This test is a slow one too. If some analyses are not well | |
347 | * configured, this test will also generates null pointer exceptions. These | |
348 | * are will be logged. | |
349 | * | |
350 | * @throws IOException | |
351 | * trace not found? | |
352 | */ | |
353 | @Test | |
354 | public void testWithTrace() throws IOException { | |
355 | String tracePath; | |
356 | tracePath = FileLocator.toFileURL(CtfTestTrace.ARM_64_BIT_HEADER.getTraceURL()).getPath(); | |
357 | SWTWorkbenchBot bot = new SWTWorkbenchBot(); | |
358 | SWTBotView view = bot.viewById(VIEW_ID); | |
359 | view.close(); | |
360 | bot.waitUntil(ConditionHelpers.ViewIsClosed(view)); | |
361 | SWTBotUtils.createProject(PROJECT_NAME); | |
362 | SWTBotUtils.openTrace(PROJECT_NAME, tracePath, TRACE_TYPE); | |
363 | SWTBotUtils.waitForJobs(); | |
364 | createTable(); | |
365 | SWTBotUtils.waitForJobs(); | |
366 | SWTBotTable tableBot = new SWTBotTable(fTable.getTableViewer().getTable()); | |
ef47abba | 367 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "24,100", 0, 2)); |
973f89f2 | 368 | tableBot.header("Duration").click(); |
ef47abba | 369 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "1,000", 0, 2)); |
973f89f2 | 370 | tableBot.header("Duration").click(); |
ef47abba | 371 | bot.waitUntil(ConditionHelpers.isTableCellFilled(tableBot, "5,904,091,700", 0, 2)); |
973f89f2 MK |
372 | bot.closeAllEditors(); |
373 | SWTBotUtils.deleteProject(PROJECT_NAME, bot); | |
374 | } | |
b70c55af | 375 | } |