1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.analysis
.timing
.ui
.swtbot
.tests
.flamegraph
;
12 import static org
.junit
.Assert
.assertEquals
;
13 import static org
.junit
.Assert
.assertNotNull
;
14 import static org
.junit
.Assert
.assertNull
;
15 import static org
.junit
.Assert
.assertTrue
;
16 import static org
.junit
.Assert
.fail
;
19 import java
.util
.Optional
;
20 import java
.util
.Spliterator
;
21 import java
.util
.Spliterators
;
22 import java
.util
.stream
.StreamSupport
;
24 import org
.apache
.log4j
.ConsoleAppender
;
25 import org
.apache
.log4j
.Logger
;
26 import org
.apache
.log4j
.SimpleLayout
;
27 import org
.eclipse
.core
.commands
.ExecutionException
;
28 import org
.eclipse
.core
.commands
.NotEnabledException
;
29 import org
.eclipse
.core
.commands
.NotHandledException
;
30 import org
.eclipse
.core
.commands
.common
.NotDefinedException
;
31 import org
.eclipse
.jdt
.annotation
.NonNull
;
32 import org
.eclipse
.jdt
.annotation
.Nullable
;
33 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
34 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
35 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
36 import org
.eclipse
.swtbot
.swt
.finder
.results
.Result
;
37 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTBotPreferences
;
38 import org
.eclipse
.tracecompass
.analysis
.timing
.core
.tests
.flamegraph
.AggregationTreeTest
;
39 import org
.eclipse
.tracecompass
.internal
.analysis
.timing
.ui
.flamegraph
.FlameGraphPresentationProvider
;
40 import org
.eclipse
.tracecompass
.internal
.analysis
.timing
.ui
.flamegraph
.FlameGraphView
;
41 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotUtils
;
42 import org
.eclipse
.tracecompass
.tmf
.ui
.tests
.shared
.WaitUtils
;
43 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphViewer
;
44 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
45 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
46 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.NullTimeEvent
;
47 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
48 import org
.eclipse
.ui
.IViewPart
;
49 import org
.eclipse
.ui
.IWorkbenchCommandConstants
;
50 import org
.eclipse
.ui
.IWorkbenchPartSite
;
51 import org
.eclipse
.ui
.handlers
.IHandlerService
;
52 import org
.junit
.Before
;
53 import org
.junit
.BeforeClass
;
54 import org
.junit
.Ignore
;
57 * Unit tests for the flame graph view
59 * @author Matthew Khouzam
61 public class FlameGraphTest
extends AggregationTreeTest
{
63 private static final String FLAMEGRAPH_ID
= FlameGraphView
.ID
;
64 private SWTWorkbenchBot fBot
;
65 private SWTBotView fView
;
66 private FlameGraphView fFg
;
67 /** The Log4j logger instance. */
68 private static final Logger fLogger
= Logger
.getRootLogger();
69 private TimeGraphViewer fTimeGraphViewer
;
75 public static void beforeClass() {
77 SWTBotUtils
.initialize();
78 Thread
.currentThread().setName("SWTBotTest");
79 /* set up for swtbot */
80 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
81 SWTBotPreferences
.KEYBOARD_LAYOUT
= "EN_US";
82 fLogger
.removeAllAppenders();
83 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender
.SYSTEM_OUT
));
84 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
85 SWTBotUtils
.closeView("welcome", bot
);
86 /* Switch perspectives */
87 SWTBotUtils
.switchToTracingPerspective();
88 /* Finish waiting for eclipse to load */
89 WaitUtils
.waitForJobs();
96 public void before() {
97 fBot
= new SWTWorkbenchBot();
98 SWTBotUtils
.openView(FLAMEGRAPH_ID
);
99 SWTBotView view
= fBot
.viewById(FLAMEGRAPH_ID
);
102 FlameGraphView flamegraph
= UIThreadRunnable
.syncExec((Result
<FlameGraphView
>) () -> {
103 IViewPart viewRef
= fView
.getViewReference().getView(true);
104 return (viewRef
instanceof FlameGraphView
) ?
(FlameGraphView
) viewRef
: null;
106 assertNotNull(flamegraph
);
107 fTimeGraphViewer
= flamegraph
.getTimeGraphViewer();
108 assertNotNull(fTimeGraphViewer
);
109 maximize(flamegraph
);
114 * Maximize a view by reference
116 * TODO: if this is useful, maybe uplift to SWTViewBot
121 private static void maximize(@NonNull IViewPart view
) {
123 IWorkbenchPartSite site
= view
.getSite();
125 // The annotation is to make the compiler not complain.
126 @Nullable Object handlerServiceObject
= site
.getService(IHandlerService
.class);
127 assertTrue(handlerServiceObject
instanceof IHandlerService
);
128 IHandlerService handlerService
= (IHandlerService
) handlerServiceObject
;
130 handlerService
.executeCommand(IWorkbenchCommandConstants
.WINDOW_MAXIMIZE_ACTIVE_VIEW_OR_EDITOR
, null);
131 } catch (ExecutionException
| NotDefinedException
| NotEnabledException
| NotHandledException e
) {
132 fail(e
.getMessage());
136 private void loadFlameGraph() {
137 UIThreadRunnable
.syncExec(() -> fFg
.buildFlameGraph(getCga()));
140 } catch (InterruptedException e
) {
141 fail(e
.getMessage());
145 private ITimeGraphEntry
selectRoot() {
146 UIThreadRunnable
.syncExec(() -> fTimeGraphViewer
.selectNextItem());
147 ITimeGraphEntry entry
= fTimeGraphViewer
.getSelection();
148 assertNotNull(entry
);
152 private static ITimeEvent
getFirstEvent(ITimeGraphEntry actualEntry
) {
153 Optional
<@NonNull ?
extends ITimeEvent
> actualEventOpt
= StreamSupport
.stream(Spliterators
.spliteratorUnknownSize(actualEntry
.getTimeEventsIterator(), Spliterator
.NONNULL
), false)
154 .filter(i
-> (i
instanceof TimeEvent
)).filter(j
-> !(j
instanceof NullTimeEvent
))
156 assertTrue(actualEventOpt
.isPresent());
157 ITimeEvent actualEvent
= actualEventOpt
.get();
162 public void emptyStateSystemTest() {
163 super.emptyStateSystemTest();
165 ITimeGraphEntry entry
= fTimeGraphViewer
.getSelection();
170 public void cascadeTest() {
173 ITimeGraphEntry entry
= selectRoot();
174 assertEquals(3, entry
.getChildren().size());
175 ITimeGraphEntry actualEntry
= entry
.getChildren().get(1);
176 ITimeEvent actualEvent
= getFirstEvent(actualEntry
);
177 assertNotNull(actualEvent
);
178 assertEquals(996, actualEvent
.getDuration());
183 public void mergeFirstLevelCalleesTest() {
184 super.mergeFirstLevelCalleesTest();
186 ITimeGraphEntry entry
= selectRoot();
187 assertEquals(3, entry
.getChildren().size());
188 ITimeGraphEntry actualEntry
= entry
.getChildren().get(1);
189 ITimeEvent actualEvent
= getFirstEvent(actualEntry
);
190 assertNotNull(actualEvent
);
191 assertEquals(80, actualEvent
.getDuration());
195 public void multiFunctionRootsSecondTest() {
196 super.multiFunctionRootsSecondTest();
198 ITimeGraphEntry entry
= selectRoot();
199 assertEquals(2, entry
.getChildren().size());
200 ITimeGraphEntry actualEntry
= entry
.getChildren().get(1);
201 ITimeEvent actualEvent
= getFirstEvent(actualEntry
);
202 assertNotNull(actualEvent
);
203 assertEquals(10, actualEvent
.getDuration());
207 public void mergeSecondLevelCalleesTest() {
208 super.mergeSecondLevelCalleesTest();
210 ITimeGraphEntry entry
= selectRoot();
211 assertEquals(4, entry
.getChildren().size());
212 ITimeGraphEntry actualEntry
= entry
.getChildren().get(1);
213 ITimeEvent actualEvent
= getFirstEvent(actualEntry
);
214 assertNotNull(actualEvent
);
215 assertEquals(90, actualEvent
.getDuration());
219 public void multiFunctionRootsTest() {
220 super.multiFunctionRootsTest();
222 ITimeGraphEntry entry
= selectRoot();
223 assertEquals(2, entry
.getChildren().size());
224 ITimeGraphEntry actualEntry
= entry
.getChildren().get(1);
225 ITimeEvent actualEvent
= getFirstEvent(actualEntry
);
226 assertNotNull(actualEvent
);
227 assertEquals(10, actualEvent
.getDuration());
231 * Also test statistics tooltip
234 public void treeTest() {
237 ITimeGraphEntry entry
= selectRoot();
238 assertEquals(3, entry
.getChildren().size());
239 ITimeGraphEntry actualEntry
= entry
.getChildren().get(1);
240 ITimeEvent actualEvent
= getFirstEvent(actualEntry
);
241 assertNotNull(actualEvent
);
242 assertEquals(80, actualEvent
.getDuration());
243 Map
<String
, String
> tooltip
= new FlameGraphPresentationProvider().getEventHoverToolTipInfo(actualEvent
);
245 tooltip
= new FlameGraphPresentationProvider().getEventHoverToolTipInfo(actualEvent
, 5);
246 assertTrue(tooltip
.toString().contains("duration=80 ns"));
247 assertTrue(tooltip
.toString().contains("duration=40 ns"));
255 public void largeTest() {