1 package net.sf.ehcache;
2
3 import org.junit.After;
4 import static org.junit.Assert.assertTrue;
5 import org.junit.Before;
6 import org.junit.Test;
7
8
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 /***
13 * Isolated performance test which only runs one cache at a time.
14 *
15 * @author <a href="mailto:gluck@gregluck.com">Greg Luck</a>
16 * @version $Id: CachePerformanceTest.html 13146 2011-08-01 17:12:39Z oletizi $
17 */
18 public class CachePerformanceTest {
19
20 private static final Logger LOG = LoggerFactory.getLogger(CachePerformanceTest.class.getName());
21
22
23 /***
24 * the CacheManager instance
25 */
26 protected CacheManager manager;
27
28 /***
29 * setup test
30 */
31 @Before
32 public void setUp() throws Exception {
33 manager = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-defaultonly.xml");
34 }
35
36 /***
37 * teardown
38 */
39 @After
40 public void tearDown() throws Exception {
41 if (manager != null) {
42 manager.shutdown();
43 }
44 }
45
46
47 /***
48 * With 50,000 gets
49 * Time to get 50000 entries from m500d45500Cache: 5746
50 * Time to get 50000 entries from m500d45500Cache: 3795 if writeback to the memory store is turned off
51 * TODO we should be able to do this optimisation with a small change
52 */
53 @Test
54 public void testGetSpeedMostlyDisk() throws InterruptedException {
55 StopWatch stopWatch = new StopWatch();
56 long time = 0;
57 Cache m500d500Cache = manager.getCache("m500d45500Cache");
58 if (m500d500Cache == null) {
59 m500d500Cache = new Cache("m500d45500Cache", 500, true, true, 5, 2);
60 manager.addCache(m500d500Cache);
61 m500d500Cache = manager.getCache("m500d45500Cache");
62 for (int i = 0; i < 50000; i++) {
63 Integer key = Integer.valueOf(i);
64 m500d500Cache.put(new Element(key, "value" + i));
65 }
66 stopWatch.getElapsedTime();
67
68 }
69 time = stopWatch.getElapsedTime();
70 for (int i = 0; i < 50000; i++) {
71 Integer key = Integer.valueOf(i);
72 m500d500Cache.get(key);
73 }
74 time = stopWatch.getElapsedTime();
75 LOG.info("Time to get 50000 entries from m500d45500Cache: " + time);
76 assertTrue("Time to get 50000 entries from m500d500Cache", time < 20000);
77 }
78
79
80 /***
81 * With 50,000 gets
82 * Time to get 50000 entries from m50000Cache: 174
83 */
84 @Test
85 public void testGetSpeedMemoryOnly() throws InterruptedException {
86 StopWatch stopWatch = new StopWatch();
87 long time = 0;
88 Cache m500d500Cache = manager.getCache("m50000Cache");
89 if (m500d500Cache == null) {
90 m500d500Cache = new Cache("m50000Cache", 50000, true, true, 5, 2);
91 manager.addCache(m500d500Cache);
92 m500d500Cache = manager.getCache("m50000Cache");
93 for (int i = 0; i < 50000; i++) {
94 Integer key = Integer.valueOf(i);
95 m500d500Cache.put(new Element(key, "value" + i));
96 }
97 Thread.sleep(1000);
98 stopWatch.getElapsedTime();
99
100 }
101 time = stopWatch.getElapsedTime();
102 for (int i = 0; i < 50000; i++) {
103 Integer key = Integer.valueOf(i);
104 m500d500Cache.get(key);
105 }
106 time = stopWatch.getElapsedTime();
107 LOG.info("Time to get 50000 entries from m50000Cache: " + time);
108 assertTrue("Time to get 50000 entries from m50000Cache", time < 20000);
109 }
110
111
112 }