commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang EntitiesPerformanceTest.java EntitiesTest.java
Date Sat, 24 May 2003 15:11:36 GMT
alex        2003/05/24 08:11:36

  Modified:    lang/src/java/org/apache/commons/lang Entities.java
               lang/src/test/org/apache/commons/lang
                        EntitiesPerformanceTest.java EntitiesTest.java
  Log:
  continuing performance improvements-- now uses a binary lookup
  
  Revision  Changes    Path
  1.7       +65 -7     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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Entities.java	24 May 2003 13:29:44 -0000	1.6
  +++ Entities.java	24 May 2003 15:11:36 -0000	1.7
  @@ -437,10 +437,21 @@
       }
   
       static class ArrayIntMap implements IntMap {
  -        int growBy = 100;
  -        private int size = 0;
  -        private String[] names = new String[growBy];
  -        private int[] values = new int[growBy];
  +        protected int growBy = 100;
  +        protected int size = 0;
  +        protected String[] names;
  +        protected int[] values;
  +
  +        public ArrayIntMap() {
  +            names = new String[growBy];
  +            values = new int[growBy];
  +        }
  +
  +        public ArrayIntMap(int growBy) {
  +            this.growBy = growBy;
  +            names = new String[growBy];
  +            values = new int[growBy];
  +        }
   
           public void add(String name, int value) {
               ensureCapacity(size + 1);
  @@ -449,7 +460,7 @@
               size++;
           }
   
  -        private void ensureCapacity(int capacity) {
  +        protected void ensureCapacity(int capacity) {
               if (capacity > names.length) {
                   int newSize = Math.max(capacity, size + growBy);
                   String[] newNames = new String[newSize];
  @@ -480,7 +491,54 @@
           }
       }
   
  -    IntMap map = new HashIntMap();
  +    static class BinaryIntMap extends ArrayIntMap {
  +
  +        public BinaryIntMap() {
  +        }
  +
  +        public BinaryIntMap(int growBy) {
  +            super(growBy);
  +        }
  +
  +        // based on code in java.util.Arrays
  +        private int binarySearch(int key) {
  +            int low = 0;
  +            int high = size - 1;
  +
  +            while (low <= high) {
  +                int mid = (low + high) >> 1;
  +                int midVal = values[mid];
  +
  +                if (midVal < key)
  +                    low = mid + 1;
  +                else if (midVal > key)
  +                    high = mid - 1;
  +                else
  +                    return mid; // key found
  +            }
  +            return -(low + 1);  // key not found.
  +        }
  +
  +        public void add(String name, int value) {
  +            ensureCapacity(size + 1);
  +            int insertAt = binarySearch(value);
  +            if (insertAt > 0) return;    // note: this means you can't insert the same
value twice
  +            insertAt = -(insertAt + 1);  // binarySearch returns it negative and off-by-one
  +            System.arraycopy(values, insertAt, values, insertAt + 1, size - insertAt);
  +            values[insertAt] = value;
  +            System.arraycopy(names, insertAt, names, insertAt + 1, size - insertAt);
  +            names[insertAt] = name;
  +            size++;
  +        }
  +
  +        public String name(int value) {
  +            int index = binarySearch(value);
  +            if (index < 0) return null;
  +            return names[index];
  +        }
  +    }
  +
  +    IntMap map = new BinaryIntMap();
   
       public void addEntities(String[][] entityArray) {
           for (int i = 0; i < entityArray.length; ++i) {
  
  
  
  1.2       +46 -11    jakarta-commons/lang/src/test/org/apache/commons/lang/EntitiesPerformanceTest.java
  
  Index: EntitiesPerformanceTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/EntitiesPerformanceTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- EntitiesPerformanceTest.java	24 May 2003 13:29:44 -0000	1.1
  +++ EntitiesPerformanceTest.java	24 May 2003 15:11:36 -0000	1.2
  @@ -53,16 +53,13 @@
    */
   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 COUNT = 10000;
       private int STRING_LENGTH = 1000;
   
       private static String stringWithUnicode;
  @@ -70,6 +67,7 @@
       private static Entities treeEntities;
       private static Entities hashEntities;
       private static Entities arrayEntities;
  +    private static Entities binaryEntities;
   
       public EntitiesPerformanceTest(String name) {
           super(name);
  @@ -81,8 +79,6 @@
   
       public static Test suite() {
           TestSuite suite = new TestSuite(EntitiesPerformanceTest.class);
  -//        suite.setName("Entities Performance Tests");
  -//        return new RepeatedTest(suite, 1000);
           return suite;
       }
   
  @@ -91,14 +87,18 @@
               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);
  +                char ch = html40value(i);
                   buf.append(ch);
               }
               stringWithUnicode = buf.toString();
               stringWithEntities = Entities.HTML40.unescape(stringWithUnicode);
           }
  +    }
   
  +    private char html40value(int i) {
  +        String entityValue = Entities.html40[i % Entities.html40.length][1];
  +        char ch = (char) Integer.parseInt(entityValue);
  +        return ch;
       }
   
       public void testBuildHash() throws Exception {
  @@ -125,6 +125,30 @@
           }
       }
   
  +    public void testBuildBinary() throws Exception {
  +        for (int i = 0; i < COUNT; ++i) {
  +            binaryEntities = new Entities();
  +            binaryEntities.map = new Entities.BinaryIntMap();
  +            Entities.fillWithHtml40Entities(binaryEntities);
  +        }
  +    }
  +
  +    public void testLookupHash() throws Exception {
  +        lookup(hashEntities);
  +    }
  +
  +    public void testLookupTree() throws Exception {
  +        lookup(treeEntities);
  +    }
  +
  +    public void testLookupArray() throws Exception {
  +        lookup(arrayEntities);
  +    }
  +
  +    public void testLookupBinary() throws Exception {
  +        lookup(binaryEntities);
  +    }
  +
       public void testEscapeHash() throws Exception {
           escapeIt(hashEntities);
       }
  @@ -137,6 +161,10 @@
           escapeIt(arrayEntities);
       }
   
  +    public void testEscapeBinary() throws Exception {
  +        escapeIt(binaryEntities);
  +    }
  +
       public void testUnscapeHash() throws Exception {
           unescapeIt(hashEntities);
       }
  @@ -149,6 +177,16 @@
           unescapeIt(arrayEntities);
       }
   
  +    public void testUnescapeBinary() throws Exception {
  +        unescapeIt(binaryEntities);
  +    }
  +
  +    private void lookup(Entities entities) {
  +        for (int i = 0; i < COUNT*1000; ++i) {
  +            entities.entityName(html40value(i));
  +        }
  +    }
  +
       private void escapeIt(Entities entities) {
           for (int i = 0; i < COUNT; ++i) {
               String escaped  = entities.escape(stringWithUnicode);
  @@ -162,8 +200,5 @@
               assertEquals("xxxx\u0192", unescaped.substring(0,5));
           }
       }
  -
  -
  -
   }
   
  
  
  
  1.4       +24 -3     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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- EntitiesTest.java	24 May 2003 13:29:44 -0000	1.3
  +++ EntitiesTest.java	24 May 2003 15:11:36 -0000	1.4
  @@ -135,8 +135,29 @@
   
       public void testArrayIntMap() throws Exception
       {
  -        Entities.ArrayIntMap map = new Entities.ArrayIntMap();
  -        map.growBy = 2;
  +        Entities.ArrayIntMap map = new Entities.ArrayIntMap(2);
  +        checkSomeIntMap(map);
  +    }
  +
  +    public void testTreeIntMap() throws Exception
  +    {
  +        Entities.IntMap map = new Entities.TreeIntMap();
  +        checkSomeIntMap(map);
  +    }
  +
  +    public void testHashIntMap() throws Exception
  +    {
  +        Entities.IntMap map = new Entities.HashIntMap();
  +        checkSomeIntMap(map);
  +    }
  +
  +    public void testBinaryIntMap() throws Exception
  +    {
  +        Entities.BinaryIntMap map = new Entities.BinaryIntMap(2);
  +        checkSomeIntMap(map);
  +    }
  +
  +    private void checkSomeIntMap(Entities.IntMap map) {
           map.add("foo", 1);
           assertEquals(1, map.value("foo"));
           assertEquals("foo", map.name(1));
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message