1   package org.apache.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  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.jcs.JCS;
27  import org.apache.jcs.engine.memory.lru.LRUMemoryCache;
28  
29  /***
30   * Tests the performance difference between the LRU and the MRU
31   *
32   */
33  public class LRUvsMRUPerformanceTest
34      extends TestCase
35  {
36  
37      float ratioPut = 0;
38  
39      float ratioGet = 0;
40  
41      float target = 1.20f;
42  
43      int loops = 20;
44  
45      int tries = 10000;
46  
47      /***
48       * A unit test for JUnit
49       *
50       * @exception Exception
51       *                Description of the Exception
52       */
53      public void testSimpleLoad()
54          throws Exception
55      {
56          Log log1 = LogFactory.getLog( LRUMemoryCache.class );
57          if ( log1.isDebugEnabled() )
58          {
59              System.out.println( "The log level must be at info or above for the a performance test." );
60              return;
61          }
62          Log log2 = LogFactory.getLog( MRUMemoryCache.class );
63          if ( log2.isDebugEnabled() )
64          {
65              System.out.println( "The log level must be at info or above for the a performance test." );
66              return;
67          }
68          doWork();
69  
70          assertTrue( "Ratio is unacceptible.", this.ratioPut < target );
71          assertTrue( "Ratio is unacceptible.", this.ratioGet < target );
72      }
73  
74      /***
75       * Runs the test
76       */
77      public void doWork()
78      {
79  
80          long start = 0;
81          long end = 0;
82          long time = 0;
83          float tPer = 0;
84  
85          long putTotalLRU = 0;
86          long getTotalLRU = 0;
87          long putTotalMRU = 0;
88          long getTotalMRU = 0;
89  
90          try
91          {
92  
93              JCS.setConfigFilename( "/TestMRUCache.ccf" );
94              JCS cache = JCS.getInstance( "lruDefined" );
95              JCS mru = JCS.getInstance( "mruDefined" );
96  
97              System.out.println( "LRU = " + cache );
98  
99              for ( int j = 0; j < loops; j++ )
100             {
101 
102                 System.out.println( "Beginning loop " + j );
103 
104                 String name = "LRU      ";
105                 start = System.currentTimeMillis();
106                 for ( int i = 0; i < tries; i++ )
107                 {
108                     cache.put( "key:" + i, "data" + i );
109                 }
110                 end = System.currentTimeMillis();
111                 time = end - start;
112                 putTotalLRU += time;
113                 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
114                 System.out.println( name + " put time for " + tries + " = " + time + "; millis per = " + tPer );
115 
116                 start = System.currentTimeMillis();
117                 for ( int i = 0; i < tries; i++ )
118                 {
119                     cache.get( "key:" + i );
120                 }
121                 end = System.currentTimeMillis();
122                 time = end - start;
123                 getTotalLRU += time;
124                 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
125                 System.out.println( name + " get time for " + tries + " = " + time + "; millis per = " + tPer );
126 
127                 // /////////////////////////////////////////////////////////////
128                 name = "MRU";
129                 start = System.currentTimeMillis();
130                 for ( int i = 0; i < tries; i++ )
131                 {
132                     mru.put( "key:" + i, "data" + i );
133                 }
134                 end = System.currentTimeMillis();
135                 time = end - start;
136                 putTotalMRU += time;
137                 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
138                 System.out.println( name + " put time for " + tries + " = " + time + "; millis per = " + tPer );
139 
140                 start = System.currentTimeMillis();
141                 for ( int i = 0; i < tries; i++ )
142                 {
143                     mru.get( "key:" + i );
144                 }
145                 end = System.currentTimeMillis();
146                 time = end - start;
147                 getTotalMRU += time;
148                 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
149                 System.out.println( name + " get time for " + tries + " = " + time + "; millis per = " + tPer );
150 
151                 System.out.println( "\n" );
152             }
153 
154         }
155         catch ( Exception e )
156         {
157             e.printStackTrace( System.out );
158             System.out.println( e );
159         }
160 
161         long putAvJCS = putTotalLRU / loops;
162         long getAvJCS = getTotalLRU / loops;
163         long putAvHashtable = putTotalMRU / loops;
164         long getAvHashtable = getTotalMRU / loops;
165 
166         System.out.println( "Finished " + loops + " loops of " + tries + " gets and puts" );
167 
168         System.out.println( "\n" );
169         System.out.println( "Put average for JCS       = " + putAvJCS );
170         System.out.println( "Put average for MRU = " + putAvHashtable );
171         ratioPut = Float.intBitsToFloat( (int) putAvJCS ) / Float.intBitsToFloat( (int) putAvHashtable );
172         System.out.println( "JCS puts took " + ratioPut + " times the Hashtable, the goal is <" + target + "x" );
173 
174         System.out.println( "\n" );
175         System.out.println( "Get average for JCS       = " + getAvJCS );
176         System.out.println( "Get average for MRU = " + getAvHashtable );
177         ratioGet = Float.intBitsToFloat( (int) getAvJCS ) / Float.intBitsToFloat( (int) getAvHashtable );
178         System.out.println( "JCS gets took " + ratioGet + " times the Hashtable, the goal is <" + target + "x" );
179 
180     }
181 
182 }