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