View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
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  
20  import static org.junit.Assert.assertTrue;
21  import org.junit.Test;
22  
23  
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  
28  /***
29   * Test cases for status.
30   *
31   * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
32   * @version $Id: StatusTest.html 13146 2011-08-01 17:12:39Z oletizi $
33   */
34  public class StatusTest {
35  
36      private static final Logger LOG = LoggerFactory.getLogger(StatusTest.class.getName());
37  
38      private static int int1 = 1;
39      private int int2 = 2;
40      private Status status1 = Status.STATUS_ALIVE;
41  
42      /***
43       * The status is checked in almost every public method.
44       * It has to be fast.
45       * This test keeps it that way.
46       */
47      @Test
48      public void testEqualsPerformance() {
49          StopWatch stopWatch = new StopWatch();
50          stopWatch.getElapsedTime();
51  
52  
53          Status status2 = Status.STATUS_SHUTDOWN;
54  
55          for (int i = 0; i < 10000; i++) {
56              status1.equals(status2);
57          }
58          stopWatch.getElapsedTime();
59          for (int i = 0; i < 10000; i++) {
60              status1.equals(status2);
61          }
62          long statusCompareTime = stopWatch.getElapsedTime();
63          LOG.info("Time to do equals(Status): " + statusCompareTime);
64          assertTrue("Status compare is greater than permitted time", statusCompareTime < 35);
65  
66      }
67  
68      /***
69       * An alternate implementation that is and override of the equals in Object. This would not normally
70       * be used
71       */
72      @Test
73      public void testObjectEqualsPerformance() {
74          StopWatch stopWatch = new StopWatch();
75          stopWatch.getElapsedTime();
76  
77          Object object = new Object();
78          for (int i = 0; i < 10000; i++) {
79              status1.equals(object);
80          }
81          stopWatch.getElapsedTime();
82          for (int i = 0; i < 10000; i++) {
83              status1.equals(object);
84          }
85          long objectCompareTime = stopWatch.getElapsedTime();
86          LOG.info("Time to do equals(Object): " + objectCompareTime);
87          assertTrue("Status compare is greater than permitted time", objectCompareTime < 25);
88  
89  
90      }
91  
92  
93      /***
94       * This was the implementation up to ehcache 1.2
95       */
96      @Test
97      public void testIntEqualsPerformance() {
98          StopWatch stopWatch = new StopWatch();
99          stopWatch.getElapsedTime();
100 
101         int2 = 12;
102         boolean result;
103         for (int i = 0; i < 10000; i++) {
104             result = int1 == int2;
105         }
106         stopWatch.getElapsedTime();
107         for (int i = 0; i < 10000; i++) {
108             result = int1 == int2;
109         }
110         long intCompareTime = stopWatch.getElapsedTime();
111         LOG.info("Time to do int == int: " + intCompareTime);
112         assertTrue("Status compare is greater than permitted time", intCompareTime < 10);
113 
114 
115     }
116 
117 
118 }