|
Added
Link Here
|
| 1 |
/********************************************************************** |
| 2 |
* Copyright (c) 2002, 2005 IBM Corporation and others. |
| 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 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
/* |
| 12 |
* Created on Jun 19, 2003 |
| 13 |
*/ |
| 14 |
package org.eclipse.cdt.core.indexer.tests; |
| 15 |
|
| 16 |
import java.io.FileOutputStream; |
| 17 |
import java.io.IOException; |
| 18 |
import java.io.InputStream; |
| 19 |
import java.io.OutputStream; |
| 20 |
import java.util.ArrayList; |
| 21 |
import java.util.Enumeration; |
| 22 |
import java.util.zip.ZipEntry; |
| 23 |
import java.util.zip.ZipFile; |
| 24 |
|
| 25 |
import junit.framework.Test; |
| 26 |
import junit.framework.TestCase; |
| 27 |
import junit.framework.TestSuite; |
| 28 |
|
| 29 |
import org.eclipse.cdt.core.CCorePlugin; |
| 30 |
import org.eclipse.cdt.core.ICDescriptor; |
| 31 |
import org.eclipse.cdt.core.ICDescriptorOperation; |
| 32 |
import org.eclipse.cdt.core.index.IIndexChangeListener; |
| 33 |
import org.eclipse.cdt.core.index.IIndexDelta; |
| 34 |
import org.eclipse.cdt.core.index.IndexChangeEvent; |
| 35 |
import org.eclipse.cdt.core.testplugin.CProjectHelper; |
| 36 |
import org.eclipse.cdt.core.testplugin.CTestPlugin; |
| 37 |
import org.eclipse.cdt.internal.core.index.domsourceindexer.DOMSourceIndexer; |
| 38 |
import org.eclipse.cdt.internal.core.search.indexing.IndexManager; |
| 39 |
import org.eclipse.core.resources.IFile; |
| 40 |
import org.eclipse.core.resources.IProject; |
| 41 |
import org.eclipse.core.runtime.CoreException; |
| 42 |
import org.eclipse.core.runtime.IPath; |
| 43 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 44 |
import org.eclipse.core.runtime.IStatus; |
| 45 |
import org.eclipse.core.runtime.NullProgressMonitor; |
| 46 |
import org.eclipse.core.runtime.Path; |
| 47 |
|
| 48 |
/** |
| 49 |
* @author bgheorgh |
| 50 |
*/ |
| 51 |
public class DOMSourceIndexerPerfTests extends TestCase { |
| 52 |
IProject testProject; |
| 53 |
DOMSourceIndexer sourceIndexer; |
| 54 |
IndexManager indexManager; |
| 55 |
static long timeC = 0; |
| 56 |
static long timeCPP = 0; |
| 57 |
static int countC = 0; |
| 58 |
static int countCPP = 0; |
| 59 |
static final int MAXCOUNT = 10; |
| 60 |
|
| 61 |
/** |
| 62 |
* @param name |
| 63 |
*/ |
| 64 |
public DOMSourceIndexerPerfTests(String name) { |
| 65 |
super(name); |
| 66 |
} |
| 67 |
|
| 68 |
public void resetIndexer(final String indexerId){ |
| 69 |
if ( testProject != null) { |
| 70 |
ICDescriptorOperation op = new ICDescriptorOperation() { |
| 71 |
|
| 72 |
public void execute(ICDescriptor descriptor, IProgressMonitor monitor) throws CoreException { |
| 73 |
descriptor.remove(CCorePlugin.INDEXER_UNIQ_ID); |
| 74 |
descriptor.create(CCorePlugin.INDEXER_UNIQ_ID,indexerId); |
| 75 |
} |
| 76 |
}; |
| 77 |
try { |
| 78 |
CCorePlugin.getDefault().getCDescriptorManager().runDescriptorOperation(testProject, op, new NullProgressMonitor()); |
| 79 |
CCorePlugin.getDefault().getCoreModel().getIndexManager().indexerChangeNotification(testProject); |
| 80 |
} catch (CoreException e) {} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public static void main(String[] args) {} |
| 85 |
|
| 86 |
/* |
| 87 |
* @see TestCase#setUp() |
| 88 |
*/ |
| 89 |
protected void setUp() throws Exception { |
| 90 |
super.setUp(); |
| 91 |
testProject = CProjectHelper.createCCProject("IndexerTestProject", "bin").getProject(); //$NON-NLS-1$ |
| 92 |
assertNotNull("Unable to create project", testProject); //$NON-NLS-1$ |
| 93 |
|
| 94 |
resetIndexer(CCorePlugin.DEFAULT_INDEXER_UNIQ_ID); |
| 95 |
indexManager = CCorePlugin.getDefault().getCoreModel().getIndexManager(); |
| 96 |
sourceIndexer = (DOMSourceIndexer) indexManager.getIndexerForProject(testProject); |
| 97 |
} |
| 98 |
/* |
| 99 |
* @see TestCase#tearDown() |
| 100 |
*/ |
| 101 |
protected void tearDown() { |
| 102 |
try { |
| 103 |
super.tearDown(); |
| 104 |
} catch (Exception e1) {} |
| 105 |
//Delete project |
| 106 |
if (testProject.exists()) { |
| 107 |
try { |
| 108 |
System.gc(); |
| 109 |
System.runFinalization(); |
| 110 |
testProject.delete(true, null); |
| 111 |
} catch (CoreException e) { |
| 112 |
fail(getMessage(e.getStatus())); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Makes error message from multistatus |
| 119 |
* |
| 120 |
* @param status |
| 121 |
* @return |
| 122 |
*/ |
| 123 |
private String getMessage(IStatus status) { |
| 124 |
StringBuffer message = new StringBuffer("["); //$NON-NLS-1$ |
| 125 |
message.append(status.getMessage()); |
| 126 |
if (status.isMultiStatus()) { |
| 127 |
IStatus children[] = status.getChildren(); |
| 128 |
for (int i = 0; i < children.length; i++) { |
| 129 |
message.append(getMessage(children[i])); |
| 130 |
} |
| 131 |
} |
| 132 |
message.append("]"); //$NON-NLS-1$ |
| 133 |
return message.toString(); |
| 134 |
} |
| 135 |
|
| 136 |
public static Test suite() { |
| 137 |
timeC = 0; |
| 138 |
timeCPP = 0; |
| 139 |
countC = 0; |
| 140 |
countCPP = 0; |
| 141 |
TestSuite suite = new TestSuite(DOMSourceIndexerPerfTests.class.getName()); |
| 142 |
Test t1 = new DOMSourceIndexerPerfTests("testC"); //$NON-NLS-1$ |
| 143 |
Test t2 = new DOMSourceIndexerPerfTests("testCPP"); //$NON-NLS-1$ |
| 144 |
|
| 145 |
for (int i = 0; i < MAXCOUNT; i++) { |
| 146 |
suite.addTest(t1); |
| 147 |
suite.addTest(t2); |
| 148 |
} |
| 149 |
return suite; |
| 150 |
|
| 151 |
} |
| 152 |
/** |
| 153 |
* Unpack archive contents to project directory |
| 154 |
* |
| 155 |
* @param name - name of archive file w/o extension |
| 156 |
* @param mask - pattern to check file extensions |
| 157 |
* @return - list of files which fit to pattern |
| 158 |
*/ |
| 159 |
|
| 160 |
private ArrayList unzip(String name, String mask) { |
| 161 |
ArrayList lst = new ArrayList(20); |
| 162 |
|
| 163 |
byte[] buffer = new byte[512]; |
| 164 |
String archname = "resources/zips/" + name + ".zip"; |
| 165 |
ZipFile zipFile = null; |
| 166 |
|
| 167 |
try { |
| 168 |
zipFile = new ZipFile(CTestPlugin.getDefault().getFileInPlugin(new Path(archname))); |
| 169 |
IPath projPath = testProject.getLocation(); |
| 170 |
Enumeration entries = zipFile.entries(); |
| 171 |
|
| 172 |
while (entries.hasMoreElements()) { |
| 173 |
ZipEntry entry = (ZipEntry)entries.nextElement(); |
| 174 |
if (!entry.isDirectory()) { |
| 175 |
IPath entryPath = projPath.append(entry.getName()); |
| 176 |
InputStream in = zipFile.getInputStream(entry); |
| 177 |
OutputStream out = new FileOutputStream(entryPath.toFile()); |
| 178 |
for (int n = in.read(buffer); n >= 0; n = in.read(buffer)) |
| 179 |
out.write(buffer, 0, n); |
| 180 |
in.close(); |
| 181 |
out.close(); |
| 182 |
if (entryPath.lastSegment().endsWith(mask)) { |
| 183 |
lst.add(entryPath.lastSegment()); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
zipFile.close(); |
| 188 |
} catch (IOException e) { |
| 189 |
fail("Unzip error with " + archname + " : " + e.getLocalizedMessage()); |
| 190 |
} |
| 191 |
return lst; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Adds files from list to index and counts indexing time |
| 196 |
* |
| 197 |
* @param lst - list of file names to be added |
| 198 |
* @return - time (in ms) spent for indexing |
| 199 |
*/ |
| 200 |
private long indexFiles(ArrayList lst) { |
| 201 |
// wait for finish of other indexing processes, if any |
| 202 |
while(indexManager.awaitingJobsCount()>0) { |
| 203 |
try { Thread.sleep(20); |
| 204 |
} catch (InterruptedException e) {} |
| 205 |
} |
| 206 |
System.gc(); |
| 207 |
long t0 = System.currentTimeMillis(); |
| 208 |
for (int i=0; i<lst.size(); i++) { |
| 209 |
IFile f = testProject.getProject().getFile((String)(lst.get(i))); |
| 210 |
sourceIndexer.addResource(testProject, f); |
| 211 |
} |
| 212 |
while(indexManager.awaitingJobsCount()>0) { |
| 213 |
try { Thread.sleep(20); |
| 214 |
} catch (InterruptedException e) {} |
| 215 |
} |
| 216 |
return (System.currentTimeMillis() - t0); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Test C source indexation |
| 221 |
* @throws Exception |
| 222 |
*/ |
| 223 |
public void testC() throws Exception { |
| 224 |
long t0 = indexFiles(unzip("perf1", ".c")); |
| 225 |
timeC += t0; countC++; |
| 226 |
System.out.println("Index performance: " + t0 + " ms on C source[" + countC + "]"); |
| 227 |
if (countC == MAXCOUNT) { |
| 228 |
System.out.println("Average performance on " + MAXCOUNT + " C passes: " + (timeC/MAXCOUNT)); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Test C++ source indexation |
| 234 |
* @throws Exception |
| 235 |
*/ |
| 236 |
public void testCPP() throws Exception { |
| 237 |
long t0 = indexFiles(unzip("perf2", ".cpp")); |
| 238 |
timeCPP += t0; countCPP++; |
| 239 |
System.out.println("Index performance: " + t0 + " ms on C++ source[" + countCPP + "]"); |
| 240 |
if (countCPP == MAXCOUNT) { |
| 241 |
System.out.println("Average performance on " + MAXCOUNT + " C++ passes: " + (timeCPP/MAXCOUNT)); |
| 242 |
} |
| 243 |
} |
| 244 |
} |