| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2016 Ericsson |
| 3 | * |
| 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 | *******************************************************************************/ |
| 9 | |
| 10 | package org.eclipse.tracecompass.analysis.timing.ui.swtbot.tests.flamegraph; |
| 11 | |
| 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; |
| 17 | |
| 18 | import java.util.Map; |
| 19 | import java.util.Optional; |
| 20 | import java.util.Spliterator; |
| 21 | import java.util.Spliterators; |
| 22 | import java.util.stream.StreamSupport; |
| 23 | |
| 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; |
| 55 | |
| 56 | /** |
| 57 | * Unit tests for the flame graph view |
| 58 | * |
| 59 | * @author Matthew Khouzam |
| 60 | */ |
| 61 | public class FlameGraphTest extends AggregationTreeTest { |
| 62 | |
| 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; |
| 70 | |
| 71 | /** |
| 72 | * Initialization |
| 73 | */ |
| 74 | @BeforeClass |
| 75 | public static void beforeClass() { |
| 76 | |
| 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(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Open a flamegraph |
| 94 | */ |
| 95 | @Before |
| 96 | public void before() { |
| 97 | fBot = new SWTWorkbenchBot(); |
| 98 | SWTBotUtils.openView(FLAMEGRAPH_ID); |
| 99 | SWTBotView view = fBot.viewById(FLAMEGRAPH_ID); |
| 100 | assertNotNull(view); |
| 101 | fView = view; |
| 102 | FlameGraphView flamegraph = UIThreadRunnable.syncExec((Result<FlameGraphView>) () -> { |
| 103 | IViewPart viewRef = fView.getViewReference().getView(true); |
| 104 | return (viewRef instanceof FlameGraphView) ? (FlameGraphView) viewRef : null; |
| 105 | }); |
| 106 | assertNotNull(flamegraph); |
| 107 | fTimeGraphViewer = flamegraph.getTimeGraphViewer(); |
| 108 | assertNotNull(fTimeGraphViewer); |
| 109 | maximize(flamegraph); |
| 110 | fFg = flamegraph; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Maximize a view by reference |
| 115 | * <p> |
| 116 | * TODO: if this is useful, maybe uplift to SWTViewBot |
| 117 | * |
| 118 | * @param view |
| 119 | * the view reference |
| 120 | */ |
| 121 | private static void maximize(@NonNull IViewPart view) { |
| 122 | assertNotNull(view); |
| 123 | IWorkbenchPartSite site = view.getSite(); |
| 124 | assertNotNull(site); |
| 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; |
| 129 | try { |
| 130 | handlerService.executeCommand(IWorkbenchCommandConstants.WINDOW_MAXIMIZE_ACTIVE_VIEW_OR_EDITOR, null); |
| 131 | } catch (ExecutionException | NotDefinedException | NotEnabledException | NotHandledException e) { |
| 132 | fail(e.getMessage()); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | private void loadFlameGraph() { |
| 137 | UIThreadRunnable.syncExec(() -> fFg.buildFlameGraph(getCga())); |
| 138 | try { |
| 139 | fFg.waitForUpdate(); |
| 140 | } catch (InterruptedException e) { |
| 141 | fail(e.getMessage()); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | private ITimeGraphEntry selectRoot() { |
| 146 | UIThreadRunnable.syncExec(() -> fTimeGraphViewer.selectNextItem()); |
| 147 | ITimeGraphEntry entry = fTimeGraphViewer.getSelection(); |
| 148 | assertNotNull(entry); |
| 149 | return entry; |
| 150 | } |
| 151 | |
| 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)) |
| 155 | .findFirst(); |
| 156 | assertTrue(actualEventOpt.isPresent()); |
| 157 | ITimeEvent actualEvent = actualEventOpt.get(); |
| 158 | return actualEvent; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void emptyStateSystemTest() { |
| 163 | super.emptyStateSystemTest(); |
| 164 | loadFlameGraph(); |
| 165 | ITimeGraphEntry entry = fTimeGraphViewer.getSelection(); |
| 166 | assertNull(entry); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void cascadeTest() { |
| 171 | super.cascadeTest(); |
| 172 | loadFlameGraph(); |
| 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()); |
| 179 | |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public void mergeFirstLevelCalleesTest() { |
| 184 | super.mergeFirstLevelCalleesTest(); |
| 185 | loadFlameGraph(); |
| 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()); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public void multiFunctionRootsSecondTest() { |
| 196 | super.multiFunctionRootsSecondTest(); |
| 197 | loadFlameGraph(); |
| 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()); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void mergeSecondLevelCalleesTest() { |
| 208 | super.mergeSecondLevelCalleesTest(); |
| 209 | loadFlameGraph(); |
| 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()); |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public void multiFunctionRootsTest() { |
| 220 | super.multiFunctionRootsTest(); |
| 221 | loadFlameGraph(); |
| 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()); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Also test statistics tooltip |
| 232 | */ |
| 233 | @Override |
| 234 | public void treeTest() { |
| 235 | super.treeTest(); |
| 236 | loadFlameGraph(); |
| 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); |
| 244 | assertNull(tooltip); |
| 245 | tooltip = new FlameGraphPresentationProvider().getEventHoverToolTipInfo(actualEvent, 5); |
| 246 | assertTrue(tooltip.toString().contains("duration=80 ns")); |
| 247 | assertTrue(tooltip.toString().contains("duration=40 ns")); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Takes too much ram |
| 252 | */ |
| 253 | @Ignore |
| 254 | @Override |
| 255 | public void largeTest() { |
| 256 | // Do nothing |
| 257 | } |
| 258 | |
| 259 | } |