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 djava.util.jar;
043:
044: import java.io.*;
045: import java.util.jar.*;
046:
047: import org.netbeans.performance.Benchmark;
048:
049: // Rough results (P6/1.2GHz):
050: // Without sections:
051: // 5 - 75us
052: // 10 - 107us
053: // 25 - 216us
054: // 100 - 754us
055: // I.e. approx. 8us per main attr asymptotic
056: // With sections:
057: // 5 - 126us
058: // 25 - 450us
059: // 100 - 1650us
060: // I.e. approx. 17us per main attr + sect. (w/ one attr)
061: // or a cost of approx. 9us per sect (w/ one attr)
062:
063: /**
064: * Benchmark measuring manifest parsing speed.
065: *
066: * @author Jesse Glick
067: */
068: public class ManifestTest extends Benchmark {
069:
070: public static void main(String[] args) {
071: simpleRun(ManifestTest.class);
072: }
073:
074: public ManifestTest(String name) {
075: super (name, new Integer[] { new Integer(5), new Integer(10),
076: new Integer(25), new Integer(100), new Integer(-5),
077: new Integer(-25), new Integer(-100) });
078: }
079:
080: protected byte[] mani;
081:
082: protected void setUp() throws Exception {
083: int magnitude = ((Integer) getArgument()).intValue();
084: boolean doSects = (magnitude < 0);
085: magnitude = Math.abs(magnitude);
086: ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
087: try {
088: PrintWriter pw = new PrintWriter(baos);
089: pw.println("Manifest-Version: 1.0");
090: for (int i = 0; i < magnitude; i++) {
091: pw.println("Attribute-Number-" + i
092: + ": Somewhat-lengthy-value-number-" + i);
093: }
094: pw.println();
095: if (doSects) {
096: for (int i = 0; i < magnitude; i++) {
097: pw
098: .println("Name: some/jar/file/section/number"
099: + i);
100: pw.println("Some-Attribute: Some-value");
101: pw.println();
102: }
103: }
104: pw.close();
105: } finally {
106: baos.close();
107: }
108: mani = baos.toByteArray();
109: /*
110: System.out.println("Manifest:");
111: System.out.print(new String(mani, "UTF-8"));
112: */
113: }
114:
115: public void testManifestParsing() throws Exception {
116: int count = getIterationCount();
117: int magnitude = ((Integer) getArgument()).intValue();
118: boolean doSects = (magnitude < 0);
119: magnitude = Math.abs(magnitude);
120: for (int i = 0; i < count; i++) {
121: InputStream is = new ByteArrayInputStream(mani);
122: try {
123: Manifest m = new Manifest(is);
124: assertEquals("Somewhat-lengthy-value-number-"
125: + (magnitude - 1),
126: m.getMainAttributes().getValue(
127: "attribute-number-" + (magnitude - 1)));
128: if (doSects) {
129: Attributes a = m
130: .getAttributes("some/jar/file/section/number"
131: + (magnitude - 1));
132: assertNotNull(a);
133: assertEquals("Some-value", a
134: .getValue("some-attribute"));
135: }
136: } finally {
137: is.close();
138: }
139: }
140: }
141:
142: }
|