Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 229d94cc1073468936a394838cec44816e8ba93f (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2013, 2017 Red Hat Inc.
 * 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:
 *     Red Hat Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.internal.perf.ui;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.compare.CompareUI;
import org.eclipse.compare.IEncodedStreamContentAccessor;
import org.eclipse.compare.ResourceNode;
import org.eclipse.compare.structuremergeviewer.Differencer;
import org.eclipse.compare.structuremergeviewer.ICompareInput;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.linuxtools.internal.perf.ReportComparisonData;
import org.eclipse.linuxtools.internal.perf.handlers.Messages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

/**
 * A view part to display perf comparison reports.
 */
public class ReportComparisonView extends Viewer {

    // Color values constants
    private static final Color RED = new Color(Display.getDefault(), 150, 0, 0);
    private static final Color GREEN = new Color(Display.getDefault(), 0, 50, 0);
    private static final Color LIGHT_GREEN = new Color(Display.getDefault(), 0, 105, 0);
    private static final Color ORANGE = new Color(Display.getDefault(), 150, 100, 0);

    // Regex for a generic entry in a perf comparison report.
    private static final String DIFF_ENTRY = "\\s+(\\d+(\\.\\d+)?)\\%\\s+([\\+\\-]?\\d+(\\.\\d+)?)\\%.*"; //$NON-NLS-1$

    private Composite fComposite;
    private ICompareInput fInput;

    // Comparison result.
    private StyledText result;
    private Label reverseLabel;
    private boolean reverse;

    public ReportComparisonView (Composite parent) {
        fComposite = new Composite (parent, SWT.NONE);
        fComposite.setLayout(new GridLayout(2, false));
        fComposite.setData(CompareUI.COMPARE_VIEWER_TITLE, Messages.ReportComparisonView_label);

        reverseLabel = new Label(fComposite, SWT.NONE);
        reverseLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false));
        reverseLabel.setText(Messages.StatComparisonView_reversedLabel);
        reverseLabel.setVisible(false);

        final Button reverse = new Button(fComposite, SWT.TOGGLE);
        reverse.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_ELCL_SYNCED));
        reverse.setToolTipText(Messages.StatComparisonView_reverseToolTip);
        reverse.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
        reverse.addSelectionListener(new SelectionListener() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                toggleReverse();
                setInput(fInput);
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent e) {
            }
        });

        result = new StyledText(fComposite, SWT.V_SCROLL | SWT.H_SCROLL);
        result.setAlwaysShowScrollBars(false);
        GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
        gd.horizontalSpan = 2;
        result.setLayoutData(gd);
        result.setEditable(false);
    }

    /**
     * Set properties for StlyedText widget.
     * @param input String StyledText content.
     */
    private void setStyledText(String input) {
        result.setText(input);
        result.setJustify(true);
        result.setAlignment(SWT.LEFT);

        result.setFont(JFaceResources.getFont(JFaceResources.TEXT_FONT));

        List<StyleRange> styles = new ArrayList<>();
        int ptr = 0;
        String[] lines = input.split("\n"); //$NON-NLS-1$

        for(String line : lines){
            if (Pattern.matches(DIFF_ENTRY, line)) {
                Matcher m = Pattern.compile(DIFF_ENTRY).matcher(line);
                if (m.matches() && m.group(1) != null && m.group(3) != null) {
                    try {
                        float baseline = Float.parseFloat(m.group(1).trim());
                        float delta = Float.parseFloat(m.group(3).trim());
                        if (baseline > 1 && Math.abs(delta) > 1) {
                            StyleRange curStyleRange;
                            if (delta < 0 ) {
                                curStyleRange = delta < -5 ? new StyleRange(ptr, line.length(), LIGHT_GREEN, null) :
                                    new StyleRange(ptr, line.length(), GREEN, null);
                            } else {
                                curStyleRange = delta < 5 ? new StyleRange(ptr, line .length(), ORANGE, null) :
                                    new StyleRange(ptr, line.length(), RED, null);
                            }
                            styles.add(curStyleRange);
                        }
                    } catch (NumberFormatException e) {
                        // set no StyleRange
                    }
                }
            }
            // + 1 to skip over the '\n' at EOL that the tokenizer eats
            ptr += line.length() + 1;
        }

        result.setStyleRanges(styles.toArray(new StyleRange[0]));
    }

    @Override
    public Control getControl() {
        return fComposite;
    }

    @Override
    public Object getInput() {
        return fInput;
    }

    @Override
    public ISelection getSelection() {
        return null;
    }

    @Override
    public void refresh() {
    }

    @Override
    public void setInput(Object input) {
        if (input instanceof ICompareInput) {
            fInput = (ICompareInput) input;

            if (fInput.getAncestor() != null ||
                    (fInput.getKind() & Differencer.DIRECTION_MASK) != 0) {
                setStyledText(Messages.CompUnsupported);
            } else {
                // get corresponding files
                IPath oldDatum;
                IPath newDatum;
                IProject proj = null;

                if (fInput.getLeft() instanceof ResourceNode) {
                    ResourceNode left = (ResourceNode) fInput.getLeft();
                    IResource oldData = left.getResource();
                    oldDatum = oldData.getLocation();
                    proj = oldData.getProject();
                } else {
                    IEncodedStreamContentAccessor lStream = (IEncodedStreamContentAccessor) fInput.getLeft();
                    oldDatum = generateTempFile(lStream);
                }

                if (fInput.getRight() instanceof ResourceNode) {
                    ResourceNode right = (ResourceNode) fInput.getRight();
                    IResource newData = right.getResource();
                    newDatum = newData.getLocation();
                    proj = newData.getProject();
                } else {
                    IEncodedStreamContentAccessor rStream = (IEncodedStreamContentAccessor) fInput.getRight();
                    newDatum = generateTempFile(rStream);
                }

				String title = MessageFormat.format(Messages.ContentDescription_0, oldDatum.toFile().getName(),
						newDatum.toFile().getName());

                // create comparison data and run comparison.
                ReportComparisonData diffData;
                if (reverse) {
                    diffData = new ReportComparisonData(title, oldDatum, newDatum, proj);
                } else {
                    diffData = new ReportComparisonData(title, newDatum, oldDatum, proj);
                }
                diffData.parse();

                setStyledText(diffData.getPerfData());
            }
        }

        fComposite.layout();
    }

    @Override
    public void setSelection(ISelection selection, boolean reveal) {
    }

    private IPath generateTempFile(IEncodedStreamContentAccessor stream) {
        try {
            Path tmpFile = Files.createTempFile("perf-report-", ".data"); //$NON-NLS-1$ //$NON-NLS-2$
            tmpFile.toFile().delete();
            Files.copy(stream.getContents(), tmpFile);
            return new org.eclipse.core.runtime.Path(tmpFile.toString());
        } catch (IOException|CoreException e) {
            return null;
        }
    }

    private void toggleReverse () {
        if (reverse) {
            reverse = false;
            reverseLabel.setVisible(false);
        } else {
            reverse = true;
            reverseLabel.setVisible(true);
        }
    }
}

Back to the top