aboutsummaryrefslogtreecommitdiff
path: root/basebuilder-3.6.2/org.eclipse.releng.basebuilder/plugins/org.eclipse.test.performance.ui/src/org/eclipse/test/internal/performance/results/ui/ComponentsView.java
blob: 06b77d31a449ab32fc300075468a2ffb20d8cfe5 (plain)
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*******************************************************************************
 * Copyright (c) 2000, 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.test.internal.performance.results.ui;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.AbstractTreeViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.test.internal.performance.results.model.BuildResultsElement;
import org.eclipse.test.internal.performance.results.model.ComponentResultsElement;
import org.eclipse.test.internal.performance.results.model.ConfigResultsElement;
import org.eclipse.test.internal.performance.results.model.ResultsElement;
import org.eclipse.test.internal.performance.results.model.ScenarioResultsElement;
import org.eclipse.test.internal.performance.results.utils.IPerformancesConstants;
import org.eclipse.test.internal.performance.results.utils.Util;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.model.WorkbenchContentProvider;
import org.eclipse.ui.model.WorkbenchLabelProvider;

/**
 * View to see the performance results of all the components in a hierarchical tree.
 * <p>
 * A component defines several performance scenarios which are run on several
 * machines (aka config). All builds results are stored onto each configuration
 * and 2 dimensions have been stored for each result: the "Elapsed Process Time"
 * and the "CPU Time".
 * </p><p>
 * There's only one available action from this view: read the local data files. This
 * populates the hierarchy with the numbers stored in these files.
 * </p><p>
 * There's also the possibility to filter the results:
 * 	<ul>
 *	<li>Filter for builds:
 *		<ul>
 *		<li>Filter baselines:	hide the baselines (starting with R-3.x)</li>
 *		<li>Filter nightly:	hide the nightly builds (starting with 'N')</li>
 *		<li>Filter non-important builds:	hide all non-important builds, which means non-milestone builds and those after the last milestone</li>
 *		</ul>
 *	</li>
 *	</li>Filter for scenarios:
 *		<ul>
 *		<li>Filter non-fingerprints: hide the scenarios which are not in the fingerprints</li>
 *		</ul>
 *	</li>
 *	</ul>
 * </p>
 * @see ComponentResultsView
 */
public class ComponentsView extends PerformancesView {

	// Viewer filters
	final static ViewerFilter FILTER_ADVANCED_SCENARIOS = new ViewerFilter() {
		public boolean select(Viewer v, Object parentElement, Object element) {
			if (element instanceof ScenarioResultsElement) {
				ScenarioResultsElement scenarioElement = (ScenarioResultsElement) element;
				return scenarioElement.hasSummary();
			}
	        return true;
        }
	};

	// Views
	PerformancesView buildsView;
	ComponentResultsView componentResultsView = null;

	// Internal
	Set expandedComponents = new HashSet();
	File resultsDir = null;

	// Actions
	Action filterAdvancedScenarios;
	Action writeStatus;

	// SWT resources
	Font boldFont;

	// Write Status
	static int WRITE_STATUS;

/**
 * Default constructor.
 */
public ComponentsView() {
//	this.onlyFingerprintsImageDescriptor = ImageDescriptor.createFromFile(getClass(), "filter_ps.gif");
	super();

	// Get preferences
	this.preferences = new InstanceScope().getNode(IPerformancesConstants.PLUGIN_ID);

	// Init status
	WRITE_STATUS = this.preferences.getInt(IPerformancesConstants.PRE_WRITE_STATUS, IPerformancesConstants.DEFAULT_WRITE_STATUS);

}

/*
 * (non-Javadoc)
 * @see org.eclipse.test.internal.performance.results.ui.PerformancesView#createPartControl(org.eclipse.swt.widgets.Composite)
 */
public void createPartControl(Composite parent) {
	super.createPartControl(parent);

	// Create the viewer
	this.viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);

	// Set the content provider: first level is components list
	WorkbenchContentProvider contentProvider = new WorkbenchContentProvider() {
		public Object[] getElements(Object o) {
			return ComponentsView.this.getElements();
		}
	};
	this.viewer.setContentProvider(contentProvider);

	// Set the label provider
	WorkbenchLabelProvider labelProvider = new WorkbenchLabelProvider() {

		protected String decorateText(String input, Object element) {
			String text = super.decorateText(input, element);
			if (element instanceof BuildResultsElement) {
				BuildResultsElement buildElement = (BuildResultsElement) element;
				if (buildElement.isMilestone()) {
					text = Util.getMilestoneName(buildElement.getName()) + " - "+text;
				}
			}
			return text;
		}

		// When all scenarios are displayed, then set fingerprints one in bold.
		public Font getFont(Object element) {
			Font font = super.getFont(element);
			if (element instanceof ScenarioResultsElement) {
//				Action fingerprints = ComponentsView.this.filterNonFingerprints;
//				if (fingerprints != null && !fingerprints.isChecked()) {
				boolean fingerprints = ComponentsView.this.preferences.getBoolean(IPerformancesConstants.PRE_FILTER_ADVANCED_SCENARIOS, IPerformancesConstants.DEFAULT_FILTER_ADVANCED_SCENARIOS);
				if (!fingerprints) {
					ScenarioResultsElement scenarioElement = (ScenarioResultsElement) element;
					if (scenarioElement.hasSummary()) {
						return getBoldFont(font);
					}
				}
			}
			if (element instanceof BuildResultsElement) {
				BuildResultsElement buildElement = (BuildResultsElement) element;
				if (Util.isMilestone(buildElement.getName())) {
					return getBoldFont(font);
				}
			}
			return font;
		}
	};
	this.viewer.setLabelProvider(labelProvider);

	// Set the children sorter
	ViewerSorter nameSorter = new ViewerSorter() {

		// Sort children using specific comparison (see the implementation
		// of the #compareTo(Object) in the ResultsElement hierarchy
		public int compare(Viewer view, Object e1, Object e2) {
			// Config and Build results are sorted in reverse order
			if (e1 instanceof BuildResultsElement) {
				ResultsElement element = (ResultsElement) e2;
				return element.compareTo(e1);
			}
			if (e1 instanceof ResultsElement) {
				ResultsElement element = (ResultsElement) e1;
				return element.compareTo(e2);
			}
			return super.compare(view, e1, e2);
		}
	};
	this.viewer.setSorter(nameSorter);

	// Add results view as listener to viewer selection changes
	Display.getDefault().asyncExec(new Runnable() {
		public void run() {
			ISelectionChangedListener listener = getResultsView();
			if (listener != null) {
				ComponentsView.this.viewer.addSelectionChangedListener(listener);
			}
		}
	});

	// Finalize viewer initialization
	PlatformUI.getWorkbench().getHelpSystem().setHelp(this.viewer.getControl(), "org.eclipse.test.performance.ui.components");
	finalizeViewerCreation();
}

/*
 * (non-Javadoc)
 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
 */
public void dispose() {
	if (this.boldFont != null) {
		this.boldFont.dispose();
	}
//	JFaceResources.getResources().destroyImage(this.onlyFingerprintsImageDescriptor);
	super.dispose();
}

/*
 * (non-Javadoc)
 * @see org.eclipse.test.internal.performance.results.ui.PerformancesView#fillLocalPullDown(org.eclipse.jface.action.IMenuManager)
 */
void fillFiltersDropDown(IMenuManager manager) {
	super.fillFiltersDropDown(manager);
	manager.add(this.filterOldBuilds);
	manager.add(this.filterLastBuilds);
	manager.add(new Separator());
	manager.add(this.filterAdvancedScenarios);
}

void fillLocalPullDown(IMenuManager manager) {
	super.fillLocalPullDown(manager);
	manager.add(new Separator());
	manager.add(this.writeStatus);
}

/*
 * Filter non fingerprints scenarios action run.
 */
void filterAdvancedScenarios(boolean fingerprints, boolean updatePreference) {
	this.results.setFingerprints(fingerprints);
	if (fingerprints) {
		this.viewFilters.add(FILTER_ADVANCED_SCENARIOS);
	} else {
		this.viewFilters.remove(FILTER_ADVANCED_SCENARIOS);
	}
	this.preferences.putBoolean(IPerformancesConstants.PRE_FILTER_ADVANCED_SCENARIOS, fingerprints);
	updateFilters();
}

/*
 * Returns the bold font.
 */
Font getBoldFont(Font font) {
	if (this.boldFont == null) {
		FontData[] fontData = (font==null ? JFaceResources.getDefaultFont() : font).getFontData();
		FontData boldFontData = new FontData(fontData[0].getName(), fontData[0].getHeight(), SWT.BOLD);
		this.boldFont = new Font(this.display, boldFontData);
	}
	return this.boldFont;
}

/*
 * Get all the components from the model.
 */
Object[] getElements() {
	if (this.results == null) {
		initResults();
		if (this.filterAdvancedScenarios != null) {
			this.results.setFingerprints(this.filterAdvancedScenarios.isChecked());
		}
	}
	return this.results.getElements();
}

/*
 * Return the components results view.
 */
ComponentResultsView getResultsView() {
	if (this.componentResultsView == null) {
		this.componentResultsView = (ComponentResultsView) getWorkbenchView("org.eclipse.test.internal.performance.results.ui.ComponentsResultsView");
	}
	return this.componentResultsView;
}

/*
 * Return the builds view.
 */
PerformancesView getSiblingView() {
	if (this.buildsView == null) {
		this.buildsView = (PerformancesView) getWorkbenchView("org.eclipse.test.internal.performance.results.ui.BuildsView");
	}
	return this.buildsView;
}

/*
 * (non-Javadoc)
 * @see org.eclipse.test.internal.performance.results.ui.PerformancesView#makeActions()
 */
void makeActions() {

	super.makeActions();

	// Filter non-fingerprints action
	this.filterAdvancedScenarios = new Action("Advanced &Scenarios", IAction.AS_CHECK_BOX) {
		public void run() {
			filterAdvancedScenarios(isChecked(), true/*update preference*/);
        }
	};
	this.filterAdvancedScenarios.setChecked(true);
	this.filterAdvancedScenarios.setToolTipText("Filter advanced scenarios (i.e. not fingerprint ones)");

	// Write status
	this.writeStatus = new Action("Write status") {
		public void run() {

			// Get write directory
			String filter = (ComponentsView.this.resultsDir == null) ? null : ComponentsView.this.resultsDir.getPath();
			final File writeDir = changeDir(filter, "Select a directory to write the status");
			if (writeDir != null) {
				writeStatus(writeDir);
			}
        }
	};
	this.writeStatus.setEnabled(true);
	this.writeStatus.setToolTipText("Write component status to a file");

	// Set filters default
	this.filterBaselineBuilds.setChecked(true);
	this.filterNightlyBuilds.setChecked(false);
}

/* (non-Javadoc)
 * @see org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener#preferenceChange(org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent)
 */
public void preferenceChange(PreferenceChangeEvent event) {
	String propertyName = event.getKey();
	Object newValue = event.getNewValue();

	// Filter non-fingerprints change
	if (propertyName.equals(IPerformancesConstants.PRE_FILTER_ADVANCED_SCENARIOS)) {
		boolean checked = newValue == null ? IPerformancesConstants.DEFAULT_FILTER_ADVANCED_SCENARIOS : "true".equals(newValue);
		filterAdvancedScenarios(checked, false/*do not update preference*/);
		this.filterAdvancedScenarios.setChecked(checked);
	}

	// Filter non-milestone change
	if (propertyName.equals(IPerformancesConstants.PRE_FILTER_OLD_BUILDS)) {
		boolean checked = newValue == null ? IPerformancesConstants.DEFAULT_FILTER_OLD_BUILDS : "true".equals(newValue);
		filterOldBuilds(checked, false/*do not update preference*/);
		this.filterOldBuilds.setChecked(checked);
	}

	// Write status
	if (propertyName.equals(IPerformancesConstants.PRE_WRITE_STATUS)) {
		WRITE_STATUS = newValue == null ? IPerformancesConstants.DEFAULT_WRITE_STATUS : Integer.parseInt((String)newValue);
	}

	super.preferenceChange(event);
}

void restoreState() {
	super.restoreState();

	// Filter baselines action default
	if (this.viewState == null) {
		this.filterBaselineBuilds.setChecked(true);
		this.viewFilters.add(FILTER_BASELINE_BUILDS);
	} else {
		String dir = this.viewState.getString(IPerformancesConstants.PRE_WRITE_RESULTS_DIR);
		if (dir != null) {
			this.resultsDir = new File(dir);
		}
	}

	// Filter non fingerprints action state
	boolean checked = this.preferences.getBoolean(IPerformancesConstants.PRE_FILTER_ADVANCED_SCENARIOS, IPerformancesConstants.DEFAULT_FILTER_ADVANCED_SCENARIOS);
	this.filterAdvancedScenarios.setChecked(checked);
	if (checked) {
		this.viewFilters.add(FILTER_ADVANCED_SCENARIOS);
	}
}

public void saveState(IMemento memento) {
	if (this.resultsDir != null) {
		memento.putString(IPerformancesConstants.PRE_WRITE_RESULTS_DIR, this.resultsDir.getPath());
	}
	super.saveState(memento);
}

/**
 * Select a results element in the tree.
 */
public void select(ComponentResultsElement componentResults, String configName, String scenarioName, String buildName) {

	// Collapse previous expanded components except the requested one
	// TODO (frederic) also collapse expanded components children elements
	this.expandedComponents.remove(componentResults);
	Iterator iterator = this.expandedComponents.iterator();
	while (iterator.hasNext()) {
		this.viewer.collapseToLevel(iterator.next(), AbstractTreeViewer.ALL_LEVELS);
	}
	this.expandedComponents.clear();

	// Set the tree selection
	ScenarioResultsElement scenarioResultsElement = (ScenarioResultsElement) componentResults.getResultsElement(scenarioName);
	if (scenarioResultsElement != null) {
		ConfigResultsElement configResultsElement = (ConfigResultsElement) scenarioResultsElement.getResultsElement(configName);
		if (configResultsElement != null) {
			BuildResultsElement buildResultsElement = (BuildResultsElement) configResultsElement.getResultsElement(buildName);
			if (buildResultsElement != null) {
				this.viewer.setSelection(new StructuredSelection(buildResultsElement), true);
				this.setFocus();
			}
		}
	}
}

/*
 * (non-Javadoc)
 * @see org.eclipse.test.internal.performance.results.ui.PerformancesView#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
 */
public void selectionChanged(SelectionChangedEvent event) {
	super.selectionChanged(event);
	ResultsElement eventResultsElement = (ResultsElement) ((StructuredSelection)event.getSelection()).getFirstElement();
	if (eventResultsElement != null) {
		ResultsElement eventComponentElement = eventResultsElement;
		if (!(eventComponentElement instanceof ComponentResultsElement)) {
			while (!(eventComponentElement instanceof ComponentResultsElement)) {
				eventComponentElement = (ResultsElement) eventComponentElement.getParent(null);
			}
			this.expandedComponents.add(eventComponentElement);
		}
	}
}

protected void writeStatus(File writeDir) {
		this.resultsDir = writeDir;
		if (this.filterAdvancedScenarios.isChecked()) {
			writeDir = new File(writeDir, "fingerprints");
		} else {
			writeDir = new File(writeDir, "all");
		}
		writeDir.mkdir();
		if ((WRITE_STATUS & IPerformancesConstants.STATUS_VALUES) != 0) {
			writeDir = new File(writeDir, "values");
		}
		int buildsNumber = WRITE_STATUS & IPerformancesConstants.STATUS_BUILDS_NUMBER_MASK;
		if (buildsNumber > 1) {
			writeDir = new File(writeDir, Integer.toString(buildsNumber));
		}
		writeDir.mkdirs();
		String prefix = this.results.getName();
		File resultsFile = new File(writeDir, prefix+".log");
		File exclusionDir = new File(writeDir, "excluded");
		exclusionDir.mkdir();
		File exclusionFile = new File(exclusionDir, prefix+".log");
		if (resultsFile.exists()) {
			int i=0;
			File saveDir = new File(writeDir, "save");
			saveDir.mkdir();
			while (true) {
				String newFileName = prefix+"_";
				if (i<10) newFileName += "0";
				newFileName += i;
				File renamedFile = new File(saveDir, newFileName+".log");
				if (resultsFile.renameTo(renamedFile)) {
					File renamedExclusionFile = new File(exclusionDir, newFileName+".log");
					exclusionFile.renameTo(renamedExclusionFile);
					break;
				}
				i++;
			}
		}

		// Write status
		StringBuffer excluded = this.results.writeStatus(resultsFile, WRITE_STATUS);
		if (excluded == null) {
			MessageDialog.openWarning(this.shell, getTitleToolTip(), "The component is not read, hence no results can be written!");
		}

		// Write exclusion file
		try {
			DataOutputStream stream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(exclusionFile)));
			try {
				stream.write(excluded.toString().getBytes());
			}
			finally {
				stream.close();
			}
		} catch (FileNotFoundException e) {
			System.err.println("Can't create exclusion file"+exclusionFile); //$NON-NLS-1$
		} catch (IOException e) {
			e.printStackTrace();
		}
}
}