001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core.projects;
043:
044: import java.io.*;
045: import java.util.*;
046:
047: import org.netbeans.performance.Benchmark;
048:
049: import org.openide.loaders.*;
050:
051: import org.netbeans.core.NbTopManager;
052: import org.netbeans.core.projects.FixedFileSystem;
053: import org.openide.cookies.InstanceCookie;
054: import org.openide.filesystems.Repository;
055:
056: /**
057: * Benchmark measuring how fast .settings files can be recognized and probed for instances.
058: * Uses FolderInstance as the means of collecting them together.
059: * @author Jesse Glick
060: */
061: public class XMLSettingsTest extends Benchmark {
062:
063: public static void main(String[] args) {
064: simpleRun(XMLSettingsTest.class);
065: }
066:
067: public XMLSettingsTest(String name) {
068: // Each arg is an Integer[2] of # of unmodified & modified .settings files to make
069: super (name, new int[][] { { 10, 0 }, { 100, 0 }, { 1000, 0 },
070: { 5, 5 }, { 50, 50 }, { 500, 500 }, });
071: }
072:
073: private static byte[] unmodifiedData = null;
074:
075: private static byte[] getUnmodifiedData() {
076: if (unmodifiedData == null) {
077: StringBuffer b = new StringBuffer();
078: b.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
079: b
080: .append("<!DOCTYPE settings PUBLIC \"-//NetBeans//DTD Session settings 1.0//EN\" \"http://www.netbeans.org/dtds/sessionsettings-1_0.dtd\">\n");
081: b.append("<settings version=\"1.0\">\n");
082: b
083: .append(" <instanceof class=\"org.netbeans.core.projects.XMLSettingsTest$SuperClazz\"/>\n");
084: b
085: .append(" <instanceof class=\"org.netbeans.core.projects.XMLSettingsTest$Clazz\"/>\n");
086: b
087: .append(" <instance class=\"org.netbeans.core.projects.XMLSettingsTest$Clazz\"/>\n");
088: b.append("</settings>\n");
089: unmodifiedData = b.toString().getBytes();
090: }
091: return unmodifiedData;
092: }
093:
094: private static List modifiedData = new ArrayList(1000);
095: private static char[] HEX = { '0', '1', '2', '3', '4', '5', '6',
096: '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
097:
098: private static byte[] getModifiedData(int i) throws IOException {
099: while (modifiedData.size() <= i)
100: modifiedData.add(null);
101: if (modifiedData.get(i) == null) {
102: StringBuffer b = new StringBuffer();
103: b.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
104: b
105: .append("<!DOCTYPE settings PUBLIC \"-//NetBeans//DTD Session settings 1.0//EN\" \"http://www.netbeans.org/dtds/sessionsettings-1_0.dtd\">\n");
106: b.append("<settings version=\"1.0\">\n");
107: b
108: .append(" <instanceof class=\"org.netbeans.core.projects.XMLSettingsTest$SuperClazz\"/>\n");
109: b
110: .append(" <instanceof class=\"org.netbeans.core.projects.XMLSettingsTest$Clazz\"/>\n");
111: b
112: .append(" <serialdata class=\"org.netbeans.core.projects.XMLSettingsTest$Clazz\">\n");
113: b.append(" ");
114: ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
115: ObjectOutputStream oos = new ObjectOutputStream(baos);
116: oos.writeObject(new Clazz(i));
117: oos.close();
118: byte[] ser = baos.toByteArray();
119: for (int j = 0; j < ser.length; j++) {
120: int x = ser[j] + 256;
121: b.append(HEX[(x & 0xF0) >> 4]);
122: b.append(HEX[x & 0x0F]);
123: }
124: b.append("\n");
125: b.append(" </serialdata>\n");
126: b.append("</settings>\n");
127: modifiedData.set(i, b.toString().getBytes());
128: }
129: return (byte[]) modifiedData.get(i);
130: }
131:
132: private DataFolder[] folders;
133:
134: protected void setUp() throws Exception {
135: NbTopManager.get();
136: int count = getIterationCount();
137: int[] x = (int[]) getArgument();
138: int unmodified = x[0];
139: int modified = x[1];
140: //System.out.println("setUp: count=" + count + " unmodified=" + unmodified + " modified=" + modified);
141: folders = new DataFolder[count];
142: for (int i = 0; i < count; i++) {
143: FixedFileSystem ffs = FixedFileSystem.getDefault();
144: for (int j = 0; j < unmodified; j++) {
145: FixedFileSystem.Instance inst = new FixedFileSystem.Instance(
146: false, "text/xml", getUnmodifiedData(), null,
147: (String) null);
148: ffs.add("folder" + i + "/unmodified" + j + ".settings",
149: inst);
150: }
151: for (int j = 0; j < modified; j++) {
152: FixedFileSystem.Instance inst = new FixedFileSystem.Instance(
153: false, "text/xml", getModifiedData(j + 1),
154: null, (String) null);
155: ffs.add("folder" + i + "/modified" + j + ".settings",
156: inst);
157: }
158: folders[i] = DataFolder.findFolder(Repository.getDefault()
159: .getDefaultFileSystem().findResource("folder" + i));
160: }
161: }
162:
163: protected void tearDown() throws Exception {
164: folders = null;
165: int count = getIterationCount();
166: int[] x = (int[]) getArgument();
167: int unmodified = x[0];
168: int modified = x[1];
169: for (int i = 0; i < count; i++) {
170: FixedFileSystem ffs = FixedFileSystem.getDefault();
171: for (int j = 0; j < unmodified; j++) {
172: ffs.remove("folder" + i + "/unmodified" + j
173: + ".settings");
174: }
175: for (int j = 0; j < modified; j++) {
176: ffs
177: .remove("folder" + i + "/modified" + j
178: + ".settings");
179: }
180: ffs.remove("folder" + i);
181: }
182: }
183:
184: public void testSettings() throws Exception {
185: int count = getIterationCount();
186: int[] x = (int[]) getArgument();
187: int unmodified = x[0];
188: int modified = x[1];
189: for (int i = 0; i < count; i++) {
190: try {
191: /*
192: System.out.println("folder=" + folders[i]);
193: DataObject[] kids = folders[i].getChildren();
194: System.out.println("folder.children=" + Arrays.asList(kids));
195: InstanceCookie ic = (InstanceCookie)kids[0].getCookie(InstanceCookie.class);
196: System.out.println("folder.children[0].instanceCreate=" + (ic == null ? "null" : ic.instanceCreate()));
197: */
198: InstanceCookie cf = new ClazzFolder(folders[i]);
199: /*
200: FolderInstance cf = new ClazzFolder(folders[i]);
201: cf.run();
202: cf.waitFinished();
203: */
204: SuperClazz[] clazzes = (SuperClazz[]) cf
205: .instanceCreate();
206: //System.out.println("clazzes=" + Arrays.asList(clazzes));
207: int unmodifiedCount = 0;
208: int modifiedCount = 0;
209: for (int j = 0; j < clazzes.length; j++) {
210: int number = clazzes[j].x();
211: if (number == 0) {
212: unmodifiedCount++;
213: } else {
214: modifiedCount += number;
215: }
216: }
217: assertEquals(unmodified, unmodifiedCount);
218: // 0, 1, 3, 6, 10, 15, ...
219: assertEquals((modified * (modified + 1)) / 2,
220: modifiedCount);
221: } catch (Exception e) {
222: e.printStackTrace(System.out);
223: throw e;
224: } catch (Error e) {
225: e.printStackTrace(System.out);
226: throw e;
227: }
228: }
229: }
230:
231: private static final class ClazzFolder extends FolderInstance {
232: public ClazzFolder(DataFolder f) {
233: super (f);
234: }
235:
236: protected Object createInstance(InstanceCookie[] cookies)
237: throws IOException, ClassNotFoundException {
238: //System.out.println("createInstance: " + Arrays.asList(cookies));
239: SuperClazz[] clazzes = new SuperClazz[cookies.length];
240: for (int i = 0; i < clazzes.length; i++) {
241: clazzes[i] = (SuperClazz) cookies[i].instanceCreate();
242: }
243: return clazzes;
244: }
245:
246: protected InstanceCookie acceptCookie(InstanceCookie cookie)
247: throws IOException, ClassNotFoundException {
248: if (cookie instanceof InstanceCookie.Of) {
249: if (((InstanceCookie.Of) cookie)
250: .instanceOf(SuperClazz.class)) {
251: //System.out.println("acceptCookie: OK");
252: return cookie;
253: }
254: System.out.println("acceptCookie: not IC.Of");
255: }
256: System.out.println("acceptCookie: strange class: "
257: + cookie.instanceName());
258: return null;
259: }
260: }
261:
262: public static abstract class SuperClazz implements Serializable {
263: public abstract int x();
264: }
265:
266: public static final class Clazz extends SuperClazz {
267: private static final long serialVersionUID = 24356298473569L;
268: private final int x;
269:
270: public Clazz() {
271: x = 0;
272: }
273:
274: public Clazz(int _x) {
275: x = _x;
276: }
277:
278: public final int x() {
279: return x;
280: }
281:
282: public String toString() {
283: return "Clazz[" + x + "]";
284: }
285: }
286:
287: }
|