You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by al...@apache.org on 2003/05/24 15:29:45 UTC
cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang EntitiesPerformanceTest.java EntitiesTest.java
alex 2003/05/24 06:29:44
Modified: lang/src/java/org/apache/commons/lang Entities.java
lang/src/test/org/apache/commons/lang EntitiesTest.java
Added: lang/src/test/org/apache/commons/lang
EntitiesPerformanceTest.java
Log:
initial performance testing for Entities
Revision Changes Path
1.6 +83 -14 jakarta-commons/lang/src/java/org/apache/commons/lang/Entities.java
Index: Entities.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/Entities.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- Entities.java 24 May 2003 04:35:06 -0000 1.5
+++ Entities.java 24 May 2003 13:29:44 -0000 1.6
@@ -53,8 +53,7 @@
*/
package org.apache.commons.lang;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
/**
* <p>Provides HTML and XML entity utilities.</p>
@@ -79,11 +78,11 @@
{"gt", "62"}, // > - greater-than
};
- static private String[][] apos = {
+ static String[][] apos = {
{"apos", "39"}, // XML apostrophe
};
- static private String[][] iso8859_1 = {
+ static String[][] iso8859_1 = {
{"nbsp", "160"}, // non-breaking space
{"iexcl", "161"}, //inverted exclamation mark
{"cent", "162"}, //cent sign
@@ -385,25 +384,37 @@
static {
HTML40 = new Entities();
- HTML40.addEntities(basic);
- HTML40.addEntities(iso8859_1);
- HTML40.addEntities(html40);
+ fillWithHtml40Entities(HTML40);
}
- static class IntMap {
- private Map mapNameToValue = new HashMap();
- private Map mapValueToName = new HashMap();
+ static void fillWithHtml40Entities(Entities entities) {
+ entities.addEntities(basic);
+ entities.addEntities(iso8859_1);
+ entities.addEntities(html40);
+ }
+
+ static interface IntMap {
+ void add(String name, int value);
+
+ String name(int value);
+
+ int value(String name);
+ }
+
+ static abstract class MapIntMap implements IntMap {
+ protected Map mapNameToValue;
+ protected Map mapValueToName;
public void add(String name, int value) {
mapNameToValue.put(name, new Integer(value));
mapValueToName.put(new Integer(value), name);
}
- private String name(int value) {
+ public String name(int value) {
return (String) mapValueToName.get(new Integer(value));
}
- private int value(String name) {
+ public int value(String name) {
Object value = mapNameToValue.get(name);
if (value == null)
return -1;
@@ -411,7 +422,65 @@
}
}
- IntMap map = new IntMap();
+ static class HashIntMap extends MapIntMap {
+ public HashIntMap() {
+ mapNameToValue = new HashMap();
+ mapValueToName = new HashMap();
+ }
+ }
+
+ static class TreeIntMap extends MapIntMap {
+ public TreeIntMap() {
+ mapNameToValue = new TreeMap();
+ mapValueToName = new TreeMap();
+ }
+ }
+
+ static class ArrayIntMap implements IntMap {
+ int growBy = 100;
+ private int size = 0;
+ private String[] names = new String[growBy];
+ private int[] values = new int[growBy];
+
+ public void add(String name, int value) {
+ ensureCapacity(size + 1);
+ names[size] = name;
+ values[size] = value;
+ size++;
+ }
+
+ private void ensureCapacity(int capacity) {
+ if (capacity > names.length) {
+ int newSize = Math.max(capacity, size + growBy);
+ String[] newNames = new String[newSize];
+ System.arraycopy(names, 0, newNames, 0, size);
+ names = newNames;
+ int[] newValues = new int[newSize];
+ System.arraycopy(values, 0, newValues, 0, size);
+ values = newValues;
+ }
+ }
+
+ public String name(int value) {
+ for (int i = 0; i < size; ++i) {
+ if (values[i] == value) {
+ return names[i];
+ }
+ }
+ return null;
+ }
+
+ public int value(String name) {
+ for (int i = 0; i < size; ++i) {
+ if (names[i].equals(name)) {
+ return values[i];
+ }
+ }
+ return -1;
+ }
+ }
+
+ IntMap map = new HashIntMap();
public void addEntities(String[][] entityArray) {
for (int i = 0; i < entityArray.length; ++i) {
1.3 +14 -1 jakarta-commons/lang/src/test/org/apache/commons/lang/EntitiesTest.java
Index: EntitiesTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/EntitiesTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- EntitiesTest.java 24 May 2003 04:38:05 -0000 1.2
+++ EntitiesTest.java 24 May 2003 13:29:44 -0000 1.3
@@ -132,5 +132,18 @@
assertEquals((int) '>', Entities.XML.entityValue("gt"));
assertEquals(-1, Entities.XML.entityValue("xyzzy"));
}
+
+ public void testArrayIntMap() throws Exception
+ {
+ Entities.ArrayIntMap map = new Entities.ArrayIntMap();
+ map.growBy = 2;
+ map.add("foo", 1);
+ assertEquals(1, map.value("foo"));
+ assertEquals("foo", map.name(1));
+ map.add("bar", 2);
+ map.add("baz", 3);
+ assertEquals(3, map.value("baz"));
+ assertEquals("baz", map.name(3));
+ }
}
1.1 jakarta-commons/lang/src/test/org/apache/commons/lang/EntitiesPerformanceTest.java
Index: EntitiesPerformanceTest.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.lang;
import java.io.IOException;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import junit.extensions.RepeatedTest;
public class EntitiesPerformanceTest extends TestCase {
private int COUNT = 200;
private int STRING_LENGTH = 1000;
private static String stringWithUnicode;
private static String stringWithEntities;
private static Entities treeEntities;
private static Entities hashEntities;
private static Entities arrayEntities;
public EntitiesPerformanceTest(String name) {
super(name);
}
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
TestSuite suite = new TestSuite(EntitiesPerformanceTest.class);
// suite.setName("Entities Performance Tests");
// return new RepeatedTest(suite, 1000);
return suite;
}
public void setUp() {
if (stringWithUnicode == null) {
StringBuffer buf = new StringBuffer(STRING_LENGTH);
for (int i = 0; i < STRING_LENGTH/5; ++i) {
buf.append("xxxx");
String entityValue = Entities.html40[i % Entities.html40.length][1];
char ch = (char) Integer.parseInt(entityValue);
buf.append(ch);
}
stringWithUnicode = buf.toString();
stringWithEntities = Entities.HTML40.unescape(stringWithUnicode);
}
}
public void testBuildHash() throws Exception {
for (int i = 0; i < COUNT; ++i) {
hashEntities = new Entities();
hashEntities.map = new Entities.HashIntMap();
Entities.fillWithHtml40Entities(hashEntities);
}
}
public void testBuildTree() throws Exception {
for (int i = 0; i < COUNT; ++i) {
treeEntities = new Entities();
treeEntities.map = new Entities.TreeIntMap();
Entities.fillWithHtml40Entities(treeEntities);
}
}
public void testBuildArray() throws Exception {
for (int i = 0; i < COUNT; ++i) {
arrayEntities = new Entities();
arrayEntities.map = new Entities.ArrayIntMap();
Entities.fillWithHtml40Entities(arrayEntities);
}
}
public void testEscapeHash() throws Exception {
escapeIt(hashEntities);
}
public void testEscapeTree() throws Exception {
escapeIt(treeEntities);
}
public void testEscapeArray() throws Exception {
escapeIt(arrayEntities);
}
public void testUnscapeHash() throws Exception {
unescapeIt(hashEntities);
}
public void testUnscapeTree() throws Exception {
unescapeIt(treeEntities);
}
public void testUnescapeArray() throws Exception {
unescapeIt(arrayEntities);
}
private void escapeIt(Entities entities) {
for (int i = 0; i < COUNT; ++i) {
String escaped = entities.escape(stringWithUnicode);
assertEquals("xxxxƒ", escaped.substring(0,10));
}
}
private void unescapeIt(Entities entities) {
for (int i = 0; i < COUNT; ++i) {
String unescaped = entities.unescape(stringWithEntities);
assertEquals("xxxx\u0192", unescaped.substring(0,5));
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org