1 /*******************************************************************************
2 * Copyright (c) 2016 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
.assertNull
;
18 import static org
.junit
.Assert
.assertTrue
;
19 import static org
.junit
.Assert
.fail
;
21 import java
.io
.ByteArrayOutputStream
;
22 import java
.io
.IOException
;
23 import java
.lang
.reflect
.InvocationTargetException
;
24 import java
.lang
.reflect
.Method
;
25 import java
.util
.List
;
27 import org
.apache
.log4j
.ConsoleAppender
;
28 import org
.apache
.log4j
.Logger
;
29 import org
.apache
.log4j
.SimpleLayout
;
30 import org
.eclipse
.core
.runtime
.FileLocator
;
31 import org
.eclipse
.jdt
.annotation
.NonNull
;
32 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
33 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
34 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
35 import org
.eclipse
.swtbot
.swt
.finder
.junit
.SWTBotJunit4ClassRunner
;
36 import org
.eclipse
.swtbot
.swt
.finder
.results
.BoolResult
;
37 import org
.eclipse
.swtbot
.swt
.finder
.results
.Result
;
38 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTBotPreferences
;
39 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotMenu
;
40 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTree
;
41 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
42 import org
.eclipse
.tracecompass
.analysis
.timing
.ui
.views
.segmentstore
.statistics
.AbstractSegmentStoreStatisticsView
;
43 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.ui
.views
.latency
.statistics
.SystemCallLatencyStatisticsView
;
44 import org
.eclipse
.tracecompass
.testtraces
.ctf
.CtfTestTrace
;
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
;
54 import org
.junit
.Test
;
55 import org
.junit
.runner
.RunWith
;
58 * Tests of the latency table
60 * @author Matthew Khouzam
62 @RunWith(SWTBotJunit4ClassRunner
.class)
63 public class SystemCallLatencyStatisticsTableAnalysisTest
{
65 private static final int MIN_COL
= 1;
66 private static final int MAX_COL
= 2;
67 private static final int AVERAGE_COL
= 3;
68 private static final int STDEV_COL
= 4;
69 private static final int COUNT_COL
= 5;
70 private static final String TRACE_TYPE
= "org.eclipse.linuxtools.lttng2.kernel.tracetype";
71 private static final String PROJECT_NAME
= "test";
72 private static final String VIEW_ID
= SystemCallLatencyStatisticsView
.ID
;
73 private static final String TRACING_PERSPECTIVE_ID
= "org.eclipse.linuxtools.tmf.ui.perspective";
75 /** The Log4j logger instance. */
76 private static final Logger fLogger
= Logger
.getRootLogger();
77 private SWTBotTree fTreeBot
;
83 public static void beforeClass() {
84 SWTBotUtils
.initialize();
85 Thread
.currentThread().setName("SWTBotTest");
86 /* set up for swtbot */
87 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
88 SWTBotPreferences
.KEYBOARD_LAYOUT
= "EN_US";
89 fLogger
.removeAllAppenders();
90 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender
.SYSTEM_OUT
));
91 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
92 final List
<SWTBotView
> openViews
= bot
.views();
93 for (SWTBotView view
: openViews
) {
94 if (view
.getTitle().equals("Welcome")) {
96 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
99 /* Switch perspectives */
100 switchTracingPerspective();
101 /* Finish waiting for eclipse to load */
102 SWTBotUtils
.waitForJobs();
107 * Opens a latency table
110 public void createTree() {
114 SWTBotUtils
.openView(VIEW_ID
);
115 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
116 SWTBotView viewBot
= bot
.viewById(VIEW_ID
);
117 final IViewReference viewReference
= viewBot
.getViewReference();
118 IViewPart viewPart
= UIThreadRunnable
.syncExec(new Result
<IViewPart
>() {
120 public IViewPart
run() {
121 return viewReference
.getView(true);
124 assertNotNull(viewPart
);
125 if (!(viewPart
instanceof SystemCallLatencyStatisticsView
)) {
126 fail("Could not instanciate view");
128 fTreeBot
= viewBot
.bot().tree();
129 assertNotNull(fTreeBot
);
136 public void closeTree() {
137 final SWTWorkbenchBot swtWorkbenchBot
= new SWTWorkbenchBot();
138 SWTBotView viewBot
= swtWorkbenchBot
.viewById(VIEW_ID
);
142 private static void switchTracingPerspective() {
143 final Exception retE
[] = new Exception
[1];
144 if (!UIThreadRunnable
.syncExec(new BoolResult() {
146 public Boolean
run() {
148 PlatformUI
.getWorkbench().showPerspective(TRACING_PERSPECTIVE_ID
,
149 PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
150 } catch (WorkbenchException e
) {
157 fail(retE
[0].getMessage());
163 * Test with an actual trace, this is more of an integration test than a
164 * unit test. This test is a slow one too. If some analyses are not well
165 * configured, this test will also generates null pointer exceptions. These
166 * are will be logged.
168 * @throws IOException
170 * @throws SecurityException
172 * @throws NoSuchMethodException
174 * @throws IllegalArgumentException
178 public void testWithTrace() throws IOException
, NoSuchMethodException
, SecurityException
, IllegalArgumentException
{
180 tracePath
= FileLocator
.toFileURL(CtfTestTrace
.ARM_64_BIT_HEADER
.getTraceURL()).getPath();
181 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
182 SWTBotView view
= bot
.viewById(VIEW_ID
);
184 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
185 SWTBotUtils
.createProject(PROJECT_NAME
);
186 SWTBotUtils
.openTrace(PROJECT_NAME
, tracePath
, TRACE_TYPE
);
187 SWTBotUtils
.waitForJobs();
189 SWTBotUtils
.waitForJobs();
190 assertEquals("1.000 µs", fTreeBot
.cell(0, MIN_COL
));
191 assertEquals("5.904 s", fTreeBot
.cell(0, MAX_COL
));
192 assertEquals("15.628 ms", fTreeBot
.cell(0, AVERAGE_COL
)); // double
193 assertEquals("175.875 ms", fTreeBot
.cell(0, STDEV_COL
));
194 assertEquals("1801", fTreeBot
.cell(0, COUNT_COL
));
195 SWTBotTreeItem treeItem
= fTreeBot
.getTreeItem("Total");
196 treeItem
= treeItem
.getNode(0);
197 assertEquals(55, treeItem
.getNodes().size());
198 validate(treeItem
.getNode(2), "select", "13.600 µs", "1.509 s", "192.251 ms", "386.369 ms", "58");
199 validate(treeItem
.getNode(3), "poll", "6.300 µs", "6.800 µs", "6.550 µs", "---", "2");
200 validate(treeItem
.getNode(5), "set_tid_address", "2.300 µs", "2.300 µs", "2.300 µs", "---", "1");
201 validate(treeItem
.getNode(7), "pipe", "27.900 µs", "29.700 µs", "28.800 µs", "---", "2");
203 SWTBotMenu menuBot
= view
.viewMenu().menu("Export to TSV");
204 assertTrue(menuBot
.isEnabled());
205 assertTrue(menuBot
.isVisible());
207 bot
.closeAllEditors();
208 SWTBotUtils
.deleteProject(PROJECT_NAME
, bot
);
211 private static void testToTsv(SWTBotView view
) throws NoSuchMethodException
{
212 ByteArrayOutputStream os
= new ByteArrayOutputStream();
214 IViewPart viewPart
= view
.getReference().getView(true);
215 assertTrue(viewPart
instanceof SystemCallLatencyStatisticsView
);
216 Class
<@NonNull AbstractSegmentStoreStatisticsView
> clazz
= AbstractSegmentStoreStatisticsView
.class;
217 Method method
= clazz
.getDeclaredMethod("exportToTsv", java
.io
.OutputStream
.class);
218 method
.setAccessible(true);
219 final Exception
[] except
= new Exception
[1];
220 UIThreadRunnable
.syncExec(() -> {
222 method
.invoke((SystemCallLatencyStatisticsView
) viewPart
, os
);
223 } catch (IllegalAccessException
| IllegalArgumentException
| InvocationTargetException e
) {
227 assertNull(except
[0]);
228 @SuppressWarnings("null")
229 String
[] lines
= String
.valueOf(os
).split(System
.getProperty("line.separator"));
230 assertNotNull(lines
);
231 assertEquals("header", "Level\tMinimum\tMaximum\tAverage\tStandard Deviation\tCount\tTotal", lines
[0]);
232 assertEquals("line 1", "Total\t1.000 µs\t5.904 s\t15.628 ms\t175.875 ms\t1801\t28.146 s", lines
[1]);
236 private static void validate(SWTBotTreeItem treeItem
, final String nodeName
, final String min
, final String max
, final String avg
, final String stdev
, final String count
) {
237 assertEquals(nodeName
, treeItem
.cell(0));
238 assertEquals(min
, treeItem
.cell(MIN_COL
));
239 assertEquals(max
, treeItem
.cell(MAX_COL
));
240 assertEquals(avg
, treeItem
.cell(AVERAGE_COL
)); // double
241 assertEquals(stdev
, treeItem
.cell(STDEV_COL
));
242 assertEquals(count
, treeItem
.cell(COUNT_COL
));