View Javadoc
1   package org.apache.commons.jcs.engine.memory.mru;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  import org.apache.commons.jcs.JCS;
24  import org.apache.commons.jcs.access.CacheAccess;
25  import org.apache.commons.jcs.engine.memory.lru.LRUMemoryCache;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  /**
30   * Tests the performance difference between the LRU and the MRU. There should be very little.
31   */
32  public class LRUvsMRUPerformanceTest
33      extends TestCase
34  {
35      /** ration we want */
36      float ratioPut = 0;
37  
38      /** ration we want */
39      float ratioGet = 0;
40  
41      /** ration we want */
42      float target = 1.20f;
43  
44      /** times to run */
45      int loops = 20;
46  
47      /** item per run */
48      int tries = 10000;
49  
50      /**
51       * A unit test for JUnit
52       * @throws Exception Description of the Exception
53       */
54      public void testSimpleLoad()
55          throws Exception
56      {
57          Log log1 = LogFactory.getLog( LRUMemoryCache.class );
58          if ( log1.isDebugEnabled() )
59          {
60              System.out.println( "The log level must be at info or above for the a performance test." );
61              return;
62          }
63          Log log2 = LogFactory.getLog( MRUMemoryCache.class );
64          if ( log2.isDebugEnabled() )
65          {
66              System.out.println( "The log level must be at info or above for the a performance test." );
67              return;
68          }
69          doWork();
70  
71          // these were when the mru was implemented with the jdk linked list
72          //assertTrue( "Ratio is unacceptible.", this.ratioPut < target );
73          ///assertTrue( "Ratio is unacceptible.", this.ratioGet < target );
74      }
75  
76      /**
77       * Runs the test
78       */
79      public void doWork()
80      {
81  
82          long start = 0;
83          long end = 0;
84          long time = 0;
85          float tPer = 0;
86  
87          long putTotalLRU = 0;
88          long getTotalLRU = 0;
89          long putTotalMRU = 0;
90          long getTotalMRU = 0;
91  
92          try
93          {
94  
95              JCS.setConfigFilename( "/TestMRUCache.ccf" );
96              CacheAccess<String, String> cache = JCS.getInstance( "lruDefined" );
97              CacheAccess<String, String> mru = JCS.getInstance( "mruDefined" );
98  
99              System.out.println( "LRU = " + cache );
100 
101             for ( int j = 0; j < loops; j++ )
102             {
103 
104                 System.out.println( "Beginning loop " + j );
105 
106                 String name = "LRU      ";
107                 start = System.currentTimeMillis();
108                 for ( int i = 0; i < tries; i++ )
109                 {
110                     cache.put( "key:" + i, "data" + i );
111                 }
112                 end = System.currentTimeMillis();
113                 time = end - start;
114                 putTotalLRU += time;
115                 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
116                 System.out.println( name + " put time for " + tries + " = " + time + "; millis per = " + tPer );
117 
118                 start = System.currentTimeMillis();
119                 for ( int i = 0; i < tries; i++ )
120                 {
121                     cache.get( "key:" + i );
122                 }
123                 end = System.currentTimeMillis();
124                 time = end - start;
125                 getTotalLRU += time;
126                 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
127                 System.out.println( name + " get time for " + tries + " = " + time + "; millis per = " + tPer );
128 
129                 // /////////////////////////////////////////////////////////////
130                 name = "MRU";
131                 start = System.currentTimeMillis();
132                 for ( int i = 0; i < tries; i++ )
133                 {
134                     mru.put( "key:" + i, "data" + i );
135                 }
136                 end = System.currentTimeMillis();
137                 time = end - start;
138                 putTotalMRU += time;
139                 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
140                 System.out.println( name + " put time for " + tries + " = " + time + "; millis per = " + tPer );
141 
142                 start = System.currentTimeMillis();
143                 for ( int i = 0; i < tries; i++ )
144                 {
145                     mru.get( "key:" + i );
146                 }
147                 end = System.currentTimeMillis();
148                 time = end - start;
149                 getTotalMRU += time;
150                 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
151                 System.out.println( name + " get time for " + tries + " = " + time + "; millis per = " + tPer );
152 
153                 System.out.println( "\n" );
154             }
155 
156         }
157         catch ( Exception e )
158         {
159             e.printStackTrace( System.out );
160             System.out.println( e );
161         }
162 
163         long putAvJCS = putTotalLRU / loops;
164         long getAvJCS = getTotalLRU / loops;
165         long putAvHashtable = putTotalMRU / loops;
166         long getAvHashtable = getTotalMRU / loops;
167 
168         System.out.println( "Finished " + loops + " loops of " + tries + " gets and puts" );
169 
170         System.out.println( "\n" );
171         System.out.println( "Put average for JCS       = " + putAvJCS );
172         System.out.println( "Put average for MRU = " + putAvHashtable );
173         ratioPut = Float.intBitsToFloat( (int) putAvJCS ) / Float.intBitsToFloat( (int) putAvHashtable );
174         System.out.println( "JCS puts took " + ratioPut + " times the Hashtable, the goal is <" + target + "x" );
175 
176         System.out.println( "\n" );
177         System.out.println( "Get average for JCS       = " + getAvJCS );
178         System.out.println( "Get average for MRU = " + getAvHashtable );
179         ratioGet = Float.intBitsToFloat( (int) getAvJCS ) / Float.intBitsToFloat( (int) getAvHashtable );
180         System.out.println( "JCS gets took " + ratioGet + " times the Hashtable, the goal is <" + target + "x" );
181     }
182 
183 }