~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to gprof/org.eclipse.linuxtools.gprof.test/src/org/eclipse/linuxtools/internal/gprof/test/GprofTest.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (c) 2009 STMicroelectronics.
 
3
 * All rights reserved. This program and the accompanying materials
 
4
 * are made available under the terms of the Eclipse Public License v1.0
 
5
 * which accompanies this distribution, and is available at
 
6
 * http://www.eclipse.org/legal/epl-v10.html
 
7
 *
 
8
 * Contributors:
 
9
 *   Xavier Raynaud <xavier.raynaud@st.com> - initial API and implementation
 
10
 *******************************************************************************/
 
11
package org.eclipse.linuxtools.internal.gprof.test;
 
12
 
 
13
 
 
14
 
 
15
import java.io.File;
 
16
import java.io.IOException;
 
17
import java.io.InputStream;
 
18
import java.io.InputStreamReader;
 
19
import java.io.LineNumberReader;
 
20
 
 
21
import junit.framework.Test;
 
22
import junit.framework.TestCase;
 
23
import junit.framework.TestSuite;
 
24
 
 
25
import org.eclipse.cdt.core.IBinaryParser.IBinaryObject;
 
26
import org.eclipse.core.runtime.Path;
 
27
import org.eclipse.jface.viewers.ITreeContentProvider;
 
28
import org.eclipse.linuxtools.binutils.utils.STSymbolManager;
 
29
import org.eclipse.linuxtools.dataviewers.abstractviewers.AbstractSTTreeViewer;
 
30
import org.eclipse.linuxtools.internal.gprof.action.SwitchContentProviderAction;
 
31
import org.eclipse.linuxtools.internal.gprof.action.SwitchSampleTimeAction;
 
32
import org.eclipse.linuxtools.internal.gprof.parser.GmonDecoder;
 
33
import org.eclipse.linuxtools.internal.gprof.view.CallGraphContentProvider;
 
34
import org.eclipse.linuxtools.internal.gprof.view.FileHistogramContentProvider;
 
35
import org.eclipse.linuxtools.internal.gprof.view.FlatHistogramContentProvider;
 
36
import org.eclipse.linuxtools.internal.gprof.view.FunctionHistogramContentProvider;
 
37
import org.eclipse.linuxtools.internal.gprof.view.GmonView;
 
38
import org.eclipse.linuxtools.internal.gprof.view.fields.SampleProfField;
 
39
import org.eclipse.swt.widgets.TreeColumn;
 
40
 
 
41
 
 
42
/**
 
43
 * @author Xavier Raynaud <xavier.raynaud@st.com>
 
44
 */
 
45
public class GprofTest extends TestCase {
 
46
        private static final String GMON_BINARY_FILE = "a.out"; 
 
47
        private static final String GMON_OUTPUT_FILE = "gmon.out"; 
 
48
        private static final String GMON_DIRECTORY_SUFFIX = "_gprof_input"; 
 
49
 
 
50
        public GprofTest() {
 
51
        }
 
52
 
 
53
        public static Test suite() {
 
54
                TestSuite ats = new TestSuite("GProf:View CSV Export");
 
55
                boolean addr2line2_16 = false;
 
56
                try {
 
57
                        Process p = Runtime.getRuntime().exec("addr2line --version");
 
58
                        InputStream is = p.getInputStream();
 
59
                        LineNumberReader reader = new LineNumberReader(new InputStreamReader(is));
 
60
                        String line;
 
61
                        while ((line = reader.readLine()) != null) {
 
62
                                if (line.contains("addr2line 2.16.")) {
 
63
                                        addr2line2_16 = true;
 
64
                                        break;
 
65
                                }
 
66
                        }
 
67
                } catch (Exception _) {
 
68
                }
 
69
                
 
70
                File[] testDirs = STJunitUtils.getTestDirs("org.eclipse.linuxtools.gprof.test",
 
71
                                ".*" + GMON_DIRECTORY_SUFFIX);
 
72
                for (File testDir : testDirs) {
 
73
                        final File gmonFile = new File(testDir, GMON_OUTPUT_FILE);
 
74
                        final File binaryFile = new File(testDir, GMON_BINARY_FILE);
 
75
 
 
76
                        File view_cg_RefFile_default = new File(testDir, "testCallgraphView.ref");
 
77
                        File view_cg_RefFile_alternate = new File(testDir, "testCallgraphView.ref.binutils-2.16");
 
78
                        File view_cg2_RefFile_default = new File(testDir, "testCallgraphTimeView.ref");
 
79
                        File view_cg2_RefFile_alternate = new File(testDir, "testCallgraphTimeView.ref.binutils-2.16");
 
80
                        final File view_cg_RefFile;
 
81
                        final File view_cg2_RefFile;
 
82
                        if (addr2line2_16 && view_cg_RefFile_alternate.exists()) {
 
83
                                view_cg_RefFile = view_cg_RefFile_alternate;
 
84
                        } else {
 
85
                                view_cg_RefFile = view_cg_RefFile_default;
 
86
                        }
 
87
                        if (addr2line2_16 && view_cg2_RefFile_alternate.exists()) {
 
88
                                view_cg2_RefFile = view_cg2_RefFile_alternate;
 
89
                        } else {
 
90
                                view_cg2_RefFile = view_cg2_RefFile_default;
 
91
                        }
 
92
                        final File view_cg2_DumpFile = new File(testDir, "testCallgraphTimeView.dump");
 
93
                        final File view_cg_DumpFile = new File(testDir, "testCallgraphView.dump");
 
94
 
 
95
                        final File view_samplesFile_RefFile = new File(testDir, "testSampleView.ref");
 
96
                        final File view_samplesFile_DumpFile = new File(testDir, "testSampleView.dump");
 
97
                        final File view_samplesFileT_RefFile = new File(testDir, "testTimeView.ref");
 
98
                        final File view_samplesFileT_DumpFile = new File(testDir, "testTimeView.dump");
 
99
 
 
100
                        final File view_samplesFunction_RefFile = new File(testDir, "testFunctionSampleView.ref");
 
101
                        final File view_samplesFunction_DumpFile = new File(testDir, "testFunctionSampleView.dump");
 
102
                        final File view_samplesFunctionT_RefFile = new File(testDir, "testFunctionTimeView.ref");
 
103
                        final File view_samplesFunctionT_DumpFile = new File(testDir, "testFunctionTimeView.dump");
 
104
                        final File view_samplesFlat_RefFile = new File(testDir, "testFlatSampleView.ref");
 
105
                        final File view_samplesFlat_DumpFile = new File(testDir, "testFlatSampleView.dump");
 
106
                        final File view_samplesFlatT_RefFile = new File(testDir, "testFlatTimeView.ref");
 
107
                        final File view_samplesFlatT_DumpFile = new File(testDir, "testFlatTimeView.dump");
 
108
 
 
109
                        IBinaryObject binary = STSymbolManager.sharedInstance.getBinaryObject(
 
110
                                        new Path(binaryFile.getAbsolutePath()));
 
111
                        final GmonDecoder gd = new GmonDecoder(binary, null);
 
112
                        try {
 
113
                                gd.read(gmonFile.getAbsolutePath());
 
114
                        } catch (IOException e) {
 
115
                                e.printStackTrace();
 
116
                        }
 
117
 
 
118
                        ats.addTest(
 
119
                                        new TestCase(testDir.getName() + ":CSV-CALLGRAPH") {
 
120
                                                public void runTest() throws Throwable {
 
121
                                                        testView(gmonFile, gd, view_cg_RefFile, view_cg_DumpFile,
 
122
                                                                        CallGraphContentProvider.sharedInstance, false);
 
123
                                                }
 
124
                                        }
 
125
                        );
 
126
                        ats.addTest(
 
127
                                        new TestCase(testDir.getName() + ":CSV-CALLGRAPH-TIMED") {
 
128
                                                public void runTest() throws Throwable {
 
129
                                                        testView(gmonFile, gd, view_cg2_RefFile, view_cg2_DumpFile,
 
130
                                                                        CallGraphContentProvider.sharedInstance, true);
 
131
                                                }
 
132
                                        }
 
133
                        );
 
134
                        ats.addTest(
 
135
                                        new TestCase(testDir.getName() + ":CSV-FILE") {
 
136
                                                public void runTest() throws Throwable {
 
137
                                                        testView(gmonFile, gd, view_samplesFile_RefFile,
 
138
                                                                        view_samplesFile_DumpFile,
 
139
                                                                        FileHistogramContentProvider.sharedInstance, false);
 
140
                                                }
 
141
                                        }
 
142
                        );
 
143
                        ats.addTest(
 
144
                                        new TestCase(testDir.getName() + ":CSV-FILE-TIMED") {
 
145
                                                public void runTest() throws Throwable {
 
146
                                                        testView(gmonFile, gd, view_samplesFileT_RefFile,
 
147
                                                                        view_samplesFileT_DumpFile,
 
148
                                                                        FileHistogramContentProvider.sharedInstance, true);
 
149
                                                }
 
150
                                        }
 
151
                        );
 
152
                        ats.addTest(
 
153
                                        new TestCase(testDir.getName() + ":CSV-FUNCTION") {
 
154
                                                public void runTest() throws Throwable {
 
155
                                                        testView(gmonFile, gd, view_samplesFunction_RefFile,
 
156
                                                                        view_samplesFunction_DumpFile,
 
157
                                                                        FunctionHistogramContentProvider.sharedInstance, false);
 
158
                                                }
 
159
                                        }
 
160
                        );
 
161
                        ats.addTest(
 
162
                                        new TestCase(testDir.getName() + ":CSV-FUNCTION-TIMED") {
 
163
                                                public void runTest() throws Throwable {
 
164
                                                        testView(gmonFile, gd, view_samplesFunctionT_RefFile,
 
165
                                                                        view_samplesFunctionT_DumpFile,
 
166
                                                                        FunctionHistogramContentProvider.sharedInstance, true);
 
167
                                                }
 
168
                                        }
 
169
                        );
 
170
                        ats.addTest(
 
171
                                        new TestCase(testDir.getName() + ":CSV-FLAT") {
 
172
                                                public void runTest() throws Throwable {
 
173
                                                        testView(gmonFile, gd, view_samplesFlat_RefFile,
 
174
                                                                        view_samplesFlat_DumpFile,
 
175
                                                                        FlatHistogramContentProvider.sharedInstance, false);
 
176
                                                }
 
177
                                        }
 
178
                        );
 
179
                        ats.addTest(
 
180
                                        new TestCase(testDir.getName() + ":CSV-FLAT-TIMED") {
 
181
                                                public void runTest() throws Throwable {
 
182
                                                        testView(gmonFile, gd, view_samplesFlatT_RefFile,
 
183
                                                                        view_samplesFlatT_DumpFile,
 
184
                                                                        FlatHistogramContentProvider.sharedInstance, true);
 
185
                                                }
 
186
                                        }
 
187
                        );
 
188
                }       
 
189
                return ats;
 
190
        }
 
191
 
 
192
 
 
193
        public static void changeMode(GmonView view, boolean timeModeRequested) {
 
194
                AbstractSTTreeViewer gmonViewer = (AbstractSTTreeViewer)view.getSTViewer();
 
195
                GmonDecoder decoder = (GmonDecoder) gmonViewer.getInput();
 
196
                int prof_rate = decoder.getHistogramDecoder().getProf_rate();
 
197
                if (prof_rate == 0) {
 
198
                        return;
 
199
                }
 
200
 
 
201
                TreeColumn tc = gmonViewer.getViewer().getTree().getColumn(1);
 
202
                SampleProfField spf = (SampleProfField) tc.getData();
 
203
 
 
204
                if (spf.getColumnHeaderText().endsWith("Samples") ^ !timeModeRequested) {
 
205
                        new SwitchSampleTimeAction(view).run();
 
206
                }
 
207
        }
 
208
 
 
209
        public static void testView(File gmonFile, GmonDecoder gd,
 
210
                        File refFile, File dumpFile,
 
211
                        ITreeContentProvider contentProvider, boolean timeMode) throws Exception {
 
212
                GmonView view = GmonView.displayGprofView(gd, gmonFile.getAbsolutePath());
 
213
                SwitchContentProviderAction action = new SwitchContentProviderAction("testAction",
 
214
                                "icons/ch_callees.png" /*to avoid error*/, view.getSTViewer().getViewer(),
 
215
                                contentProvider);
 
216
                action.run();
 
217
                changeMode(view, timeMode);
 
218
                STJunitUtils.testCSVExport(view, dumpFile.getAbsolutePath(), refFile.getAbsolutePath());
 
219
        }
 
220
}