1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.io.hfile.slab;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.MediumTests;
27 import org.apache.hadoop.hbase.io.hfile.CacheTestUtils;
28 import org.apache.hadoop.hbase.io.hfile.slab.SlabCache.SlabStats;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.Ignore;
33 import org.junit.experimental.categories.Category;
34
35
36
37
38
39
40
41
42
43
44
45
46 @Category(MediumTests.class)
47 public class TestSlabCache {
48 static final int CACHE_SIZE = 1000000;
49 static final int NUM_BLOCKS = 101;
50 static final int BLOCK_SIZE = CACHE_SIZE / NUM_BLOCKS;
51 static final int NUM_THREADS = 50;
52 static final int NUM_QUERIES = 10000;
53 SlabCache cache;
54
55 @Before
56 public void setup() {
57 cache = new SlabCache(CACHE_SIZE + BLOCK_SIZE * 2, BLOCK_SIZE);
58 cache.addSlabByConf(new Configuration());
59 }
60
61 @After
62 public void tearDown() {
63 cache.shutdown();
64 }
65
66 @Test
67 public void testElementPlacement() {
68 assertEquals(cache.getHigherBlock(BLOCK_SIZE).getKey().intValue(),
69 (BLOCK_SIZE * 11 / 10));
70 assertEquals(cache.getHigherBlock((BLOCK_SIZE * 2)).getKey()
71 .intValue(), (BLOCK_SIZE * 21 / 10));
72 }
73
74 @Test
75 public void testCacheSimple() throws Exception {
76 CacheTestUtils.testCacheSimple(cache, BLOCK_SIZE, NUM_QUERIES);
77 }
78
79 @Test
80 public void testCacheMultiThreaded() throws Exception {
81 CacheTestUtils.testCacheMultiThreaded(cache, BLOCK_SIZE, NUM_THREADS,
82 NUM_QUERIES, 0.80);
83 }
84
85 @Test
86 public void testCacheMultiThreadedSingleKey() throws Exception {
87 CacheTestUtils.hammerSingleKey(cache, BLOCK_SIZE, NUM_THREADS, NUM_QUERIES);
88 }
89
90 @Test
91 public void testCacheMultiThreadedEviction() throws Exception {
92 CacheTestUtils.hammerEviction(cache, BLOCK_SIZE, 10, NUM_QUERIES);
93 }
94
95 @Test
96
97 public void testStatsArithmetic(){
98 SlabStats test = cache.requestStats;
99 for(int i = 0; i < test.NUMDIVISIONS; i++){
100 assertTrue("Upper for index " + i + " is " + test.getUpperBound(i) +
101 " lower " + test.getLowerBound(i + 1),
102 test.getUpperBound(i) <= test.getLowerBound(i + 1));
103 }
104 }
105
106 @Test
107 public void testHeapSizeChanges(){
108 CacheTestUtils.testHeapSizeChanges(cache, BLOCK_SIZE);
109 }
110
111 @org.junit.Rule
112 public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
113 new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
114 }
115