View Javadoc

1   /**
2    *  Copyright 2003-2006 Greg Luck
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache;
18  
19  import junit.framework.TestCase;
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  /**
24   * Test cases for status.
25   *
26   * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
27   * @version $Id: StatusTest.java 28 2006-04-15 05:12:32Z gregluck $
28   */
29  public class StatusTest extends TestCase {
30      private static final Log LOG = LogFactory.getLog(StatusTest.class.getName());
31  
32      private static int int1 = 1;
33      private int int2 = 2;
34      private Status status1 = Status.STATUS_ALIVE;
35  
36      /**
37       * The status is checked in almost every public method.
38       * It has to be fast.
39       * This test keeps it that way.
40       */
41      public void testEqualsPerformance() {
42          StopWatch stopWatch = new StopWatch();
43          stopWatch.getElapsedTime();
44  
45  
46  
47          Status status2 = Status.STATUS_SHUTDOWN;
48  
49          for (int i = 0; i < 10000; i++) {
50              status1.equals(status2);
51          }
52          stopWatch.getElapsedTime();
53          for (int i = 0; i < 10000; i++) {
54              status1.equals(status2);
55          }
56          long statusCompareTime = stopWatch.getElapsedTime();
57          LOG.info("Time to do equals(Status): " + statusCompareTime);
58          assertTrue("Status compare is greater than permitted time", statusCompareTime < 15);
59  
60      }
61  
62      /**
63       * An alternate implementation that is and override of the equals in Object. This would not normally
64       * be used
65       */
66      public void testObjectEqualsPerformance() {
67          StopWatch stopWatch = new StopWatch();
68          stopWatch.getElapsedTime();
69  
70          Object object = new Object();
71          for (int i = 0; i < 10000; i++) {
72              status1.equals(object);
73          }
74          stopWatch.getElapsedTime();
75          for (int i = 0; i < 10000; i++) {
76              status1.equals(object);
77          }
78          long objectCompareTime = stopWatch.getElapsedTime();
79          LOG.info("Time to do equals(Object): " + objectCompareTime);
80          assertTrue("Status compare is greater than permitted time", objectCompareTime < 20);
81  
82  
83      }
84  
85  
86      /**
87       * This was the implementation up to ehcache 1.2
88       */
89      public void testIntEqualsPerformance() {
90          StopWatch stopWatch = new StopWatch();
91          stopWatch.getElapsedTime();
92  
93          int2 = 12;
94          boolean result;
95          for (int i = 0; i < 10000; i++) {
96              result = int1 == int2;
97          }
98          stopWatch.getElapsedTime();
99          for (int i = 0; i < 10000; i++) {
100             result = int1 == int2;
101         }
102         long intCompareTime = stopWatch.getElapsedTime();
103         LOG.info("Time to do int == int: " + intCompareTime);
104         assertTrue("Status compare is greater than permitted time", intCompareTime < 10);
105 
106 
107     }
108 
109 
110 
111 
112 }