|
17 | 17 | */ |
18 | 18 | package org.apache.hadoop.hbase.io; |
19 | 19 |
|
| 20 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 21 | +import static org.hamcrest.Matchers.lessThan; |
20 | 22 | import static org.junit.Assert.assertEquals; |
21 | 23 | import static org.junit.Assert.assertTrue; |
22 | 24 |
|
|
25 | 27 | import java.lang.management.RuntimeMXBean; |
26 | 28 | import java.nio.ByteBuffer; |
27 | 29 | import java.util.ArrayList; |
| 30 | +import java.util.Arrays; |
28 | 31 | import java.util.LinkedList; |
| 32 | +import java.util.List; |
29 | 33 | import java.util.Map; |
30 | 34 | import java.util.TreeMap; |
31 | 35 | import java.util.concurrent.ConcurrentHashMap; |
32 | 36 | import java.util.concurrent.ConcurrentSkipListMap; |
33 | 37 | import java.util.concurrent.CopyOnWriteArrayList; |
34 | 38 | import java.util.concurrent.CopyOnWriteArraySet; |
| 39 | +import java.util.concurrent.TimeUnit; |
35 | 40 | import java.util.concurrent.atomic.AtomicBoolean; |
36 | 41 | import java.util.concurrent.atomic.AtomicInteger; |
37 | 42 | import java.util.concurrent.atomic.AtomicLong; |
|
69 | 74 | import org.apache.hadoop.hbase.testclassification.IOTests; |
70 | 75 | import org.apache.hadoop.hbase.testclassification.SmallTests; |
71 | 76 | import org.apache.hadoop.hbase.util.ClassSize; |
72 | | -import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; |
73 | 77 | import org.junit.BeforeClass; |
74 | 78 | import org.junit.ClassRule; |
75 | 79 | import org.junit.Test; |
@@ -602,18 +606,30 @@ public void testObjectSize() throws IOException { |
602 | 606 | } |
603 | 607 | } |
604 | 608 |
|
605 | | - @Test |
606 | | - public void testAutoCalcFixedOverHead() { |
607 | | - Class[] classList = new Class[] { HFileContext.class, HRegion.class, BlockCacheKey.class, |
608 | | - HFileBlock.class, HStore.class, LruBlockCache.class, StoreContext.class }; |
609 | | - for (Class cl : classList) { |
610 | | - // do estimate in advance to ensure class is loaded |
611 | | - ClassSize.estimateBase(cl, false); |
612 | | - |
613 | | - long startTime = EnvironmentEdgeManager.currentTime(); |
614 | | - ClassSize.estimateBase(cl, false); |
615 | | - long endTime = EnvironmentEdgeManager.currentTime(); |
616 | | - assertTrue(endTime - startTime < 5); |
| 609 | + private long calcFixedOverhead(List<Class<?>> classList) { |
| 610 | + long overhead = 0; |
| 611 | + for (Class<?> clazz : classList) { |
| 612 | + overhead += ClassSize.estimateBase(clazz, false); |
617 | 613 | } |
| 614 | + return overhead; |
| 615 | + } |
| 616 | + |
| 617 | + @Test |
| 618 | + public void testAutoCalcFixedOverhead() throws InterruptedException { |
| 619 | + List<Class<?>> classList = Arrays.asList(HFileContext.class, HRegion.class, BlockCacheKey.class, |
| 620 | + HFileBlock.class, HStore.class, LruBlockCache.class, StoreContext.class); |
| 621 | + for (int i = 0; i < 10; i++) { |
| 622 | + // warm up |
| 623 | + calcFixedOverhead(classList); |
| 624 | + } |
| 625 | + long startNs = System.nanoTime(); |
| 626 | + long overhead = 0; |
| 627 | + for (int i = 0; i < 100; i++) { |
| 628 | + overhead += calcFixedOverhead(classList); |
| 629 | + } |
| 630 | + long costNs = System.nanoTime() - startNs; |
| 631 | + LOG.info("overhead = {}, cost {} ns", overhead, costNs); |
| 632 | + // the single computation cost should be less than 5ms |
| 633 | + assertThat(costNs, lessThan(TimeUnit.MILLISECONDS.toNanos(5) * classList.size() * 100)); |
618 | 634 | } |
619 | 635 | } |
0 commit comments