initial performance testing for Entities
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137338 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e2211f3db6
commit
0766ea1f69
|
@ -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>
|
||||
|
@ -68,7 +67,7 @@ import java.util.Map;
|
|||
* @author <a href="mailto:alex@purpletech.com">Alexander Day Chaffee</a>
|
||||
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
|
||||
* @since 2.0
|
||||
* @version $Id: Entities.java,v 1.5 2003/05/24 04:35:06 alex Exp $
|
||||
* @version $Id: Entities.java,v 1.6 2003/05/24 13:29:44 alex Exp $
|
||||
*/
|
||||
class Entities {
|
||||
|
||||
|
@ -79,11 +78,11 @@ class Entities {
|
|||
{"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 @@ class Entities {
|
|||
|
||||
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 @@ class Entities {
|
|||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
|
|
|
@ -0,0 +1,169 @@
|
|||
/* ====================================================================
|
||||
* 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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -65,7 +65,7 @@ import junit.textui.TestRunner;
|
|||
*
|
||||
* @author of original StringUtilsTest.testEscape = ?
|
||||
* @author <a href="mailto:alex@purpletech.com">Alexander Day Chaffee</a>
|
||||
* @version $Id: EntitiesTest.java,v 1.2 2003/05/24 04:38:05 alex Exp $
|
||||
* @version $Id: EntitiesTest.java,v 1.3 2003/05/24 13:29:44 alex Exp $
|
||||
*/
|
||||
public class EntitiesTest extends TestCase
|
||||
{
|
||||
|
@ -132,5 +132,18 @@ public class EntitiesTest extends TestCase
|
|||
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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue