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.io.hfile.CacheTestUtils;
27 import org.apache.hadoop.hbase.io.hfile.slab.SlabCache.SlabStats;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31
32
33
34
35
36
37
38
39
40
41
42 public class TestSlabCache {
43 static final int CACHE_SIZE = 1000000;
44 static final int NUM_BLOCKS = 101;
45 static final int BLOCK_SIZE = CACHE_SIZE / NUM_BLOCKS;
46 static final int NUM_THREADS = 50;
47 static final int NUM_QUERIES = 10000;
48 SlabCache cache;
49
50 @Before
51 public void setup() {
52 cache = new SlabCache(CACHE_SIZE + BLOCK_SIZE * 2, BLOCK_SIZE);
53 cache.addSlabByConf(new Configuration());
54 }
55
56 @After
57 public void tearDown() {
58 cache.shutdown();
59 }
60
61 @Test
62 public void testElementPlacement() {
63 assertEquals(cache.getHigherBlock(BLOCK_SIZE).getKey().intValue(),
64 (BLOCK_SIZE * 11 / 10));
65 assertEquals(cache.getHigherBlock((BLOCK_SIZE * 2)).getKey()
66 .intValue(), (BLOCK_SIZE * 21 / 10));
67 }
68
69 @Test
70 public void testCacheSimple() throws Exception {
71 CacheTestUtils.testCacheSimple(cache, BLOCK_SIZE, NUM_QUERIES);
72 }
73
74 @Test
75 public void testCacheMultiThreaded() throws Exception {
76 CacheTestUtils.testCacheMultiThreaded(cache, BLOCK_SIZE, NUM_THREADS,
77 NUM_QUERIES, 0.80);
78 }
79
80 @Test
81 public void testCacheMultiThreadedSingleKey() throws Exception {
82 CacheTestUtils.hammerSingleKey(cache, BLOCK_SIZE, NUM_THREADS, NUM_QUERIES);
83 }
84
85 @Test
86 public void testCacheMultiThreadedEviction() throws Exception {
87 CacheTestUtils.hammerEviction(cache, BLOCK_SIZE, 10, NUM_QUERIES);
88 }
89
90 @Test
91
92 public void testStatsArithmetic(){
93 SlabStats test = cache.requestStats;
94 for(int i = 0; i < test.NUMDIVISIONS; i++){
95 assertTrue("Upper for index " + i + " is " + test.getUpperBound(i) +
96 " lower " + test.getLowerBound(i + 1),
97 test.getUpperBound(i) <= test.getLowerBound(i + 1));
98 }
99 }
100
101 @Test
102 public void testHeapSizeChanges(){
103 CacheTestUtils.testHeapSizeChanges(cache, BLOCK_SIZE);
104 }
105 }