/
FlameGraphTest.java
293 lines (270 loc) · 11.2 KB
/
FlameGraphTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*******************************************************************************
* Copyright (c) 2016 Ericsson
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.tracecompass.analysis.callstack.ui.swtbot.tests;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.StreamSupport;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
import org.eclipse.swtbot.swt.finder.results.Result;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.eclipse.tracecompass.analysis.callstack.core.tests.callgraph.AggregationTreeTest;
import org.eclipse.tracecompass.analysis.callstack.core.tests.stubs.CallGraphAnalysisStub;
import org.eclipse.tracecompass.internal.analysis.callstack.core.flamegraph.FlameGraphDataProvider;
import org.eclipse.tracecompass.internal.analysis.callstack.core.flamegraph.FlameGraphDataProviderFactory;
import org.eclipse.tracecompass.internal.analysis.callstack.ui.flamegraph.FlameGraphView;
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers.SWTBotTestCondition;
import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotTimeGraph;
import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils;
import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphViewer;
import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.NullTimeEvent;
import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeEvent;
import org.eclipse.ui.IViewPart;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.runner.RunWith;
/**
* Unit tests for the flame graph view
*
* @author Matthew Khouzam
*/
@RunWith(SWTBotJunit4ClassRunner.class)
public class FlameGraphTest extends AggregationTreeTest {
private static final String SECONDARY_ID = "org.eclipse.tracecompass.analysis.callstack.ui.swtbot.test";
private static final String FLAMEGRAPH_ID = FlameGraphView.ID;
private SWTWorkbenchBot fBot;
private SWTBotView fViewBot;
private FlameGraphView fFg;
/** The Log4j logger instance. */
private TimeGraphViewer fTimeGraphViewer;
private SWTBotTimeGraph fTimeGraph;
/**
* Initialization
*/
@BeforeClass
public static void beforeClass() {
SWTBotUtils.initialize();
Thread.currentThread().setName("SWTBotTest");
/* set up for swtbot */
SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
SWTWorkbenchBot bot = new SWTWorkbenchBot();
SWTBotUtils.closeView("welcome", bot);
/* Switch perspectives */
SWTBotUtils.switchToTracingPerspective();
/* Finish waiting for eclipse to load */
WaitUtils.waitForJobs();
}
/**
* Open a flamegraph
*/
@Override
@Before
public void before() {
// Open the flame graph first
fBot = new SWTWorkbenchBot();
SWTBotUtils.openView(FLAMEGRAPH_ID, SECONDARY_ID);
SWTBotView view = fBot.viewById(FLAMEGRAPH_ID);
assertNotNull(view);
fViewBot = view;
FlameGraphView flamegraph = UIThreadRunnable.syncExec((Result<FlameGraphView>) () -> {
IViewPart viewRef = fViewBot.getViewReference().getView(true);
return (viewRef instanceof FlameGraphView) ? (FlameGraphView) viewRef : null;
});
assertNotNull(flamegraph);
fTimeGraphViewer = flamegraph.getTimeGraphViewer();
assertNotNull(fTimeGraphViewer);
SWTBotUtils.maximize(flamegraph);
fFg = flamegraph;
fTimeGraph = new SWTBotTimeGraph(view.bot());
// Now open the trace
super.before();
}
/**
* Clean up after a test, reset the views and reset the states of the
* timegraph by pressing reset on all the resets of the legend
*/
@After
public void after() {
// Calling maximize again will minimize
FlameGraphView fg = fFg;
if (fg != null) {
SWTBotUtils.maximize(fg);
}
// Reset the data provider so old data is not used to build view
FlameGraphDataProviderFactory.registerDataProviderWithId(SECONDARY_ID, null);
// Setting the input to null so the view can be emptied, to avoid race
// conditions with subsequent tests
TimeGraphViewer tg = fTimeGraphViewer;
if (tg != null) {
UIThreadRunnable.syncExec(() -> {
tg.setInput(null);
tg.refresh();
});
}
fBot.waitUntil(new SWTBotTestCondition() {
@Override
public boolean test() throws Exception {
return fTimeGraph.getEntries().length == 0;
}
@Override
public String getFailureMessage() {
return "Flame graph not emptied";
}
});
fViewBot.close();
fBot.waitUntil(ConditionHelpers.viewIsClosed(fViewBot));
}
private void loadFlameGraph() {
CallGraphAnalysisStub cga = Objects.requireNonNull(getCga());
ITmfTrace trace = getTrace();
fFg.setTrace(trace);
// Add the new provider and rebuild the view
FlameGraphDataProvider<?, ?, ?> dp = new FlameGraphDataProvider<>(trace, cga, FlameGraphDataProvider.ID + ':' + SECONDARY_ID);
FlameGraphDataProviderFactory.registerDataProviderWithId(SECONDARY_ID, dp);
UIThreadRunnable.syncExec(() -> fFg.buildFlameGraph(trace, null, null));
fBot.waitUntil(new SWTBotTestCondition() {
@Override
public boolean test() throws Exception {
return fTimeGraph.getEntries().length > 0;
}
@Override
public String getFailureMessage() {
return "Flame graph not ready";
}
});
}
private ITimeGraphEntry selectRoot() {
UIThreadRunnable.syncExec(() -> {
fTimeGraphViewer.selectNextItem();
fTimeGraphViewer.selectNextItem();
fTimeGraphViewer.selectNextItem();
});
ITimeGraphEntry entry = fTimeGraphViewer.getSelection();
assertNotNull(entry);
return entry;
}
private static ITimeEvent getFirstEvent(ITimeGraphEntry actualEntry) {
Optional<@NonNull ? extends ITimeEvent> actualEventOpt = StreamSupport.stream(Spliterators.spliteratorUnknownSize(actualEntry.getTimeEventsIterator(), Spliterator.NONNULL), false)
.filter(i -> (i instanceof TimeEvent)).filter(j -> !(j instanceof NullTimeEvent))
.findFirst();
assertTrue(actualEventOpt.isPresent());
return actualEventOpt.get();
}
@Override
public void emptyStateSystemTest() {
super.emptyStateSystemTest();
loadFlameGraph();
ITimeGraphEntry entry = fTimeGraphViewer.getSelection();
assertNull(entry);
}
@Override
public void cascadeTest() {
super.cascadeTest();
loadFlameGraph();
ITimeGraphEntry entry = selectRoot();
assertEquals(3, entry.getChildren().size());
ITimeGraphEntry actualEntry = entry.getChildren().get(1);
ITimeEvent actualEvent = getFirstEvent(actualEntry);
assertNotNull(actualEvent);
assertEquals(996, actualEvent.getDuration());
}
@Override
public void mergeFirstLevelCalleesTest() {
super.mergeFirstLevelCalleesTest();
loadFlameGraph();
ITimeGraphEntry entry = selectRoot();
assertEquals(3, entry.getChildren().size());
ITimeGraphEntry actualEntry = entry.getChildren().get(1);
ITimeEvent actualEvent = getFirstEvent(actualEntry);
assertNotNull(actualEvent);
assertEquals(80, actualEvent.getDuration());
}
@Override
public void multiFunctionRootsSecondTest() {
super.multiFunctionRootsSecondTest();
loadFlameGraph();
ITimeGraphEntry entry = selectRoot();
assertEquals(2, entry.getChildren().size());
ITimeGraphEntry actualEntry = entry.getChildren().get(1);
ITimeEvent actualEvent = getFirstEvent(actualEntry);
assertNotNull(actualEvent);
assertEquals(10, actualEvent.getDuration());
}
@Override
public void mergeSecondLevelCalleesTest() {
super.mergeSecondLevelCalleesTest();
loadFlameGraph();
ITimeGraphEntry entry = selectRoot();
assertEquals(4, entry.getChildren().size());
ITimeGraphEntry actualEntry = entry.getChildren().get(1);
ITimeEvent actualEvent = getFirstEvent(actualEntry);
assertNotNull(actualEvent);
assertEquals(90, actualEvent.getDuration());
}
@Override
public void multiFunctionRootsTest() {
super.multiFunctionRootsTest();
loadFlameGraph();
ITimeGraphEntry entry = selectRoot();
assertEquals(2, entry.getChildren().size());
ITimeGraphEntry actualEntry = entry.getChildren().get(1);
ITimeEvent actualEvent = getFirstEvent(actualEntry);
assertNotNull(actualEvent);
assertEquals(10, actualEvent.getDuration());
}
/**
* Also test statistics tooltip
*/
@Override
public void treeTest() {
super.treeTest();
loadFlameGraph();
ITimeGraphEntry entry = selectRoot();
assertEquals(3, entry.getChildren().size());
ITimeGraphEntry actualEntry = entry.getChildren().get(1);
ITimeEvent actualEvent = getFirstEvent(actualEntry);
assertNotNull(actualEvent);
assertEquals(80, actualEvent.getDuration());
FlameGraphView fg = fFg;
Map<String, String> tooltip = fg.getPresentationProvider().getEventHoverToolTipInfo(actualEvent);
assertTrue(tooltip.toString().contains("duration=80 ns"));
assertTrue(tooltip.toString().contains("duration=40 ns"));
tooltip = fg.getPresentationProvider().getEventHoverToolTipInfo(actualEvent, 5);
assertTrue(tooltip.toString().contains("duration=80 ns"));
assertTrue(tooltip.toString().contains("duration=40 ns"));
}
/**
* Takes too much ram
*/
@Ignore
@Override
public void largeTest() {
// Do nothing
}
}