1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.analysis
.os
.linux
.ui
.swtbot
.tests
.latency
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertNotNull
;
17 import static org
.junit
.Assert
.fail
;
19 import java
.io
.IOException
;
20 import java
.util
.ArrayList
;
21 import java
.util
.Collections
;
22 import java
.util
.List
;
23 import java
.util
.Random
;
25 import org
.apache
.log4j
.ConsoleAppender
;
26 import org
.apache
.log4j
.Logger
;
27 import org
.apache
.log4j
.SimpleLayout
;
28 import org
.eclipse
.core
.runtime
.FileLocator
;
29 import org
.eclipse
.jdt
.annotation
.NonNull
;
30 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
31 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
32 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
33 import org
.eclipse
.swtbot
.swt
.finder
.junit
.SWTBotJunit4ClassRunner
;
34 import org
.eclipse
.swtbot
.swt
.finder
.results
.BoolResult
;
35 import org
.eclipse
.swtbot
.swt
.finder
.results
.Result
;
36 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTBotPreferences
;
37 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
38 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.latency
.SystemCall
;
39 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.latency
.SystemCall
.InitialInfo
;
40 import org
.eclipse
.tracecompass
.analysis
.timing
.ui
.views
.segmentstore
.AbstractSegmentStoreTableViewer
;
41 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.ui
.views
.latency
.SystemCallLatencyView
;
42 import org
.eclipse
.tracecompass
.testtraces
.ctf
.CtfTestTrace
;
43 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ConditionHelpers
;
44 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotUtils
;
45 import org
.eclipse
.ui
.IViewPart
;
46 import org
.eclipse
.ui
.IViewReference
;
47 import org
.eclipse
.ui
.PlatformUI
;
48 import org
.eclipse
.ui
.WorkbenchException
;
49 import org
.junit
.After
;
50 import org
.junit
.Before
;
51 import org
.junit
.BeforeClass
;
52 import org
.junit
.Test
;
53 import org
.junit
.runner
.RunWith
;
56 * Tests of the latency table
58 * @author Matthew Khouzam
60 @RunWith(SWTBotJunit4ClassRunner
.class)
61 public class SystemCallLatencyTableAnalysisTest
{
63 private static final String TRACE_TYPE
= "org.eclipse.linuxtools.lttng2.kernel.tracetype";
64 private static final String PROJECT_NAME
= "test";
65 private static final String VIEW_ID
= SystemCallLatencyView
.ID
;
66 private static final String TRACING_PERSPECTIVE_ID
= "org.eclipse.linuxtools.tmf.ui.perspective";
68 /** The Log4j logger instance. */
69 private static final Logger fLogger
= Logger
.getRootLogger();
70 private SystemCallLatencyView fLatencyView
;
71 private AbstractSegmentStoreTableViewer fTable
;
77 public static void beforeClass() {
79 SWTBotUtils
.initialize();
80 Thread
.currentThread().setName("SWTBotTest");
81 /* set up for swtbot */
82 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
83 SWTBotPreferences
.KEYBOARD_LAYOUT
= "EN_US";
84 fLogger
.removeAllAppenders();
85 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender
.SYSTEM_OUT
));
86 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
87 final List
<SWTBotView
> openViews
= bot
.views();
88 for (SWTBotView view
: openViews
) {
89 if (view
.getTitle().equals("Welcome")) {
91 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
94 /* Switch perspectives */
95 switchTracingPerspective();
96 /* Finish waiting for eclipse to load */
97 SWTBotUtils
.waitForJobs();
102 * Opens a latency table
105 public void createTable() {
109 SWTBotUtils
.openView(VIEW_ID
);
110 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
111 SWTBotView viewBot
= bot
.viewById(VIEW_ID
);
112 final IViewReference viewReference
= viewBot
.getViewReference();
113 IViewPart viewPart
= UIThreadRunnable
.syncExec(new Result
<IViewPart
>() {
115 public IViewPart
run() {
116 return viewReference
.getView(true);
119 assertNotNull(viewPart
);
120 if (!(viewPart
instanceof SystemCallLatencyView
)) {
121 fail("Could not instanciate view");
123 fLatencyView
= (SystemCallLatencyView
) viewPart
;
124 fTable
= fLatencyView
.getSegmentStoreViewer();
125 assertNotNull(fTable
);
132 public void closeTable() {
133 final SWTWorkbenchBot swtWorkbenchBot
= new SWTWorkbenchBot();
134 SWTBotView viewBot
= swtWorkbenchBot
.viewById(VIEW_ID
);
138 private static void switchTracingPerspective() {
139 final Exception retE
[] = new Exception
[1];
140 if (!UIThreadRunnable
.syncExec(new BoolResult() {
142 public Boolean
run() {
144 PlatformUI
.getWorkbench().showPerspective(TRACING_PERSPECTIVE_ID
,
145 PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
146 } catch (WorkbenchException e
) {
153 fail(retE
[0].getMessage());
162 public void climbTest() {
163 List
<@NonNull SystemCall
> fixture
= new ArrayList
<>();
164 for (int i
= 0; i
< 100; i
++) {
165 fixture
.add(new SystemCall(new InitialInfo(i
, "", Collections
.EMPTY_MAP
), 2 * i
, 0));
168 assertNotNull(fTable
);
169 fTable
.updateModel(fixture
);
170 SWTBotTable tableBot
= new SWTBotTable(fTable
.getTableViewer().getTable());
171 assertEquals("0", tableBot
.cell(0, 2));
172 tableBot
.header("Duration").click();
173 assertEquals("0", tableBot
.cell(0, 2));
174 tableBot
.header("Duration").click();
175 assertEquals("99", tableBot
.cell(0, 2));
182 public void decrementingTest() {
183 List
<@NonNull SystemCall
> fixture
= new ArrayList
<>();
184 for (int i
= 100; i
>= 0; i
--) {
185 fixture
.add(new SystemCall(new InitialInfo(i
, "", Collections
.EMPTY_MAP
), 2 * i
, 0));
187 assertNotNull(fTable
);
188 fTable
.updateModel(fixture
);
189 SWTBotTable tableBot
= new SWTBotTable(fTable
.getTableViewer().getTable());
190 assertEquals("100", tableBot
.cell(0, 2));
191 tableBot
.header("Duration").click();
192 assertEquals("0", tableBot
.cell(0, 2));
193 tableBot
.header("Duration").click();
194 assertEquals("100", tableBot
.cell(0, 2));
201 public void smallTest() {
202 List
<@NonNull SystemCall
> fixture
= new ArrayList
<>();
203 for (int i
= 1; i
>= 0; i
--) {
204 fixture
.add(new SystemCall(new InitialInfo(i
, "", Collections
.EMPTY_MAP
), 2 * i
, 0));
206 assertNotNull(fTable
);
207 fTable
.updateModel(fixture
);
208 SWTBotTable tableBot
= new SWTBotTable(fTable
.getTableViewer().getTable());
209 assertEquals("1", tableBot
.cell(0, 2));
210 tableBot
.header("Duration").click();
211 assertEquals("0", tableBot
.cell(0, 2));
212 tableBot
.header("Duration").click();
213 assertEquals("1", tableBot
.cell(0, 2));
220 public void largeTest() {
221 final int size
= 1000000;
222 SystemCall
[] fixture
= new SystemCall
[size
];
223 for (int i
= 0; i
< size
; i
++) {
224 fixture
[i
] = (new SystemCall(new InitialInfo(i
, "", Collections
.EMPTY_MAP
), 2 * i
, 0));
226 assertNotNull(fTable
);
227 fTable
.updateModel(fixture
);
228 SWTBotTable tableBot
= new SWTBotTable(fTable
.getTableViewer().getTable());
229 assertEquals("0", tableBot
.cell(0, 2));
230 tableBot
.header("Duration").click();
231 assertEquals("0", tableBot
.cell(0, 2));
232 tableBot
.header("Duration").click();
233 assertEquals("999999", tableBot
.cell(0, 2));
240 public void noiseTest() {
241 Random rnd
= new Random();
243 final int size
= 1000000;
244 SystemCall
[] fixture
= new SystemCall
[size
];
245 for (int i
= 0; i
< size
; i
++) {
246 int start
= Math
.abs(rnd
.nextInt(100000000));
247 int end
= start
+ Math
.abs(rnd
.nextInt(1000000));
248 fixture
[i
] = (new SystemCall(new InitialInfo(start
, "", Collections
.EMPTY_MAP
), end
, 0));
250 assertNotNull(fTable
);
251 fTable
.updateModel(fixture
);
252 SWTBotTable tableBot
= new SWTBotTable(fTable
.getTableViewer().getTable());
253 assertEquals("894633", tableBot
.cell(0, 2));
254 tableBot
.header("Duration").click();
255 assertEquals("0", tableBot
.cell(0, 2));
256 tableBot
.header("Duration").click();
257 assertEquals("999999", tableBot
.cell(0, 2));
261 * Test gaussian noise
264 public void gaussianNoiseTest() {
265 Random rnd
= new Random();
267 List
<@NonNull SystemCall
> fixture
= new ArrayList
<>();
268 for (int i
= 1; i
<= 1000000; i
++) {
269 int start
= Math
.abs(rnd
.nextInt(100000000));
270 final int delta
= Math
.abs(rnd
.nextInt(1000));
271 int end
= start
+ delta
* delta
;
272 fixture
.add(new SystemCall(new InitialInfo(start
, "", Collections
.EMPTY_MAP
), end
, 0));
274 assertNotNull(fTable
);
275 fTable
.updateModel(fixture
);
276 SWTBotTable tableBot
= new SWTBotTable(fTable
.getTableViewer().getTable());
277 assertEquals("400689", tableBot
.cell(0, 2));
278 tableBot
.header("Duration").click();
279 assertEquals("0", tableBot
.cell(0, 2));
280 tableBot
.header("Duration").click();
281 assertEquals("998001", tableBot
.cell(0, 2));
285 * Test with an actual trace, this is more of an integration test than a
286 * unit test. This test is a slow one too. If some analyses are not well
287 * configured, this test will also generates null pointer exceptions. These
288 * are will be logged.
290 * @throws IOException
294 public void testWithTrace() throws IOException
{
296 tracePath
= FileLocator
.toFileURL(CtfTestTrace
.ARM_64_BIT_HEADER
.getTraceURL()).getPath();
297 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
298 SWTBotView view
= bot
.viewById(VIEW_ID
);
300 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
301 SWTBotUtils
.createProject(PROJECT_NAME
);
302 SWTBotUtils
.openTrace(PROJECT_NAME
, tracePath
, TRACE_TYPE
);
303 SWTBotUtils
.waitForJobs();
305 SWTBotUtils
.waitForJobs();
306 SWTBotTable tableBot
= new SWTBotTable(fTable
.getTableViewer().getTable());
307 assertEquals("24100", tableBot
.cell(0, 2));
308 tableBot
.header("Duration").click();
309 assertEquals("1000", tableBot
.cell(0, 2));
310 tableBot
.header("Duration").click();
311 assertEquals("5904091700", tableBot
.cell(0, 2));
312 bot
.closeAllEditors();
313 SWTBotUtils
.deleteProject(PROJECT_NAME
, bot
);