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