2ebc595fb4bcb4a9bc934998ebf5a9fefc3a9eb6
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.ui.swtbot.tests / src / org / eclipse / tracecompass / analysis / os / linux / ui / swtbot / tests / latency / SystemCallLatencyTableAnalysisTest.java
1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.analysis.os.linux.ui.swtbot.tests.latency;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.fail;
18
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;
24
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;
54
55 /**
56 * Tests of the latency table
57 *
58 * @author Matthew Khouzam
59 */
60 @RunWith(SWTBotJunit4ClassRunner.class)
61 public class SystemCallLatencyTableAnalysisTest {
62
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";
67
68 /** The Log4j logger instance. */
69 private static final Logger fLogger = Logger.getRootLogger();
70 private SystemCallLatencyView fLatencyView;
71 private AbstractSegmentStoreTableViewer fTable;
72
73 /**
74 * Things to setup
75 */
76 @BeforeClass
77 public static void beforeClass() {
78
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")) {
90 view.close();
91 bot.waitUntil(ConditionHelpers.ViewIsClosed(view));
92 }
93 }
94 /* Switch perspectives */
95 switchTracingPerspective();
96 /* Finish waiting for eclipse to load */
97 SWTBotUtils.waitForJobs();
98
99 }
100
101 /**
102 * Opens a latency table
103 */
104 @Before
105 public void createTable() {
106 /*
107 * Open latency view
108 */
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>() {
114 @Override
115 public IViewPart run() {
116 return viewReference.getView(true);
117 }
118 });
119 assertNotNull(viewPart);
120 if (!(viewPart instanceof SystemCallLatencyView)) {
121 fail("Could not instanciate view");
122 }
123 fLatencyView = (SystemCallLatencyView) viewPart;
124 fTable = fLatencyView.getSegmentStoreViewer();
125 assertNotNull(fTable);
126 }
127
128 /**
129 * Closes the view
130 */
131 @After
132 public void closeTable() {
133 final SWTWorkbenchBot swtWorkbenchBot = new SWTWorkbenchBot();
134 SWTBotView viewBot = swtWorkbenchBot.viewById(VIEW_ID);
135 viewBot.close();
136 }
137
138 private static void switchTracingPerspective() {
139 final Exception retE[] = new Exception[1];
140 if (!UIThreadRunnable.syncExec(new BoolResult() {
141 @Override
142 public Boolean run() {
143 try {
144 PlatformUI.getWorkbench().showPerspective(TRACING_PERSPECTIVE_ID,
145 PlatformUI.getWorkbench().getActiveWorkbenchWindow());
146 } catch (WorkbenchException e) {
147 retE[0] = e;
148 return false;
149 }
150 return true;
151 }
152 })) {
153 fail(retE[0].getMessage());
154 }
155
156 }
157
158 /**
159 * Test incrementing
160 */
161 @Test
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));
166 }
167
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));
176 }
177
178 /**
179 * Test decrementing
180 */
181 @Test
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));
186 }
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));
195 }
196
197 /**
198 * Test small table
199 */
200 @Test
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));
205 }
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));
214 }
215
216 /**
217 * Test large
218 */
219 @Test
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));
225 }
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));
234 }
235
236 /**
237 * Test noise
238 */
239 @Test
240 public void noiseTest() {
241 Random rnd = new Random();
242 rnd.setSeed(1234);
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));
249 }
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));
258 }
259
260 /**
261 * Test gaussian noise
262 */
263 @Test
264 public void gaussianNoiseTest() {
265 Random rnd = new Random();
266 rnd.setSeed(1234);
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));
273 }
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));
282 }
283
284 /**
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.
289 *
290 * @throws IOException
291 * trace not found?
292 */
293 @Test
294 public void testWithTrace() throws IOException {
295 String tracePath;
296 tracePath = FileLocator.toFileURL(CtfTestTrace.ARM_64_BIT_HEADER.getTraceURL()).getPath();
297 SWTWorkbenchBot bot = new SWTWorkbenchBot();
298 SWTBotView view = bot.viewById(VIEW_ID);
299 view.close();
300 bot.waitUntil(ConditionHelpers.ViewIsClosed(view));
301 SWTBotUtils.createProject(PROJECT_NAME);
302 SWTBotUtils.openTrace(PROJECT_NAME, tracePath, TRACE_TYPE);
303 SWTBotUtils.waitForJobs();
304 createTable();
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);
314 }
315 }
This page took 0.039518 seconds and 5 git commands to generate.