View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.ipc;
19  
20  import static org.junit.Assert.assertEquals;
21  
22  import java.io.IOException;
23  import java.nio.ByteBuffer;
24  import java.util.Arrays;
25  
26  import org.apache.commons.lang.time.StopWatch;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.commons.logging.impl.Log4JLogger;
30  import org.apache.hadoop.hbase.Cell;
31  import org.apache.hadoop.hbase.CellScanner;
32  import org.apache.hadoop.hbase.CellUtil;
33  import org.apache.hadoop.hbase.HBaseConfiguration;
34  import org.apache.hadoop.hbase.KeyValue;
35  import org.apache.hadoop.hbase.codec.Codec;
36  import org.apache.hadoop.hbase.codec.KeyValueCodec;
37  import org.apache.hadoop.hbase.io.SizedCellScanner;
38  import org.apache.hadoop.hbase.testclassification.ClientTests;
39  import org.apache.hadoop.hbase.testclassification.SmallTests;
40  import org.apache.hadoop.hbase.util.Bytes;
41  import org.apache.hadoop.hbase.util.ClassSize;
42  import org.apache.hadoop.io.compress.CompressionCodec;
43  import org.apache.hadoop.io.compress.DefaultCodec;
44  import org.apache.hadoop.io.compress.GzipCodec;
45  import org.apache.log4j.Level;
46  import org.junit.Before;
47  import org.junit.Test;
48  import org.junit.experimental.categories.Category;
49  
50  @Category({ ClientTests.class, SmallTests.class })
51  public class TestCellBlockBuilder {
52  
53    private static final Log LOG = LogFactory.getLog(TestCellBlockBuilder.class);
54  
55    private CellBlockBuilder builder;
56  
57    @Before
58    public void before() {
59      this.builder = new CellBlockBuilder(HBaseConfiguration.create());
60    }
61  
62    @Test
63    public void testBuildCellBlock() throws IOException {
64      doBuildCellBlockUndoCellBlock(this.builder, new KeyValueCodec(), null);
65      doBuildCellBlockUndoCellBlock(this.builder, new KeyValueCodec(), new DefaultCodec());
66      doBuildCellBlockUndoCellBlock(this.builder, new KeyValueCodec(), new GzipCodec());
67    }
68  
69    static void doBuildCellBlockUndoCellBlock(final CellBlockBuilder builder, final Codec codec,
70        final CompressionCodec compressor) throws IOException {
71      doBuildCellBlockUndoCellBlock(builder, codec, compressor, 10, 1, false);
72    }
73  
74    static void doBuildCellBlockUndoCellBlock(final CellBlockBuilder builder, final Codec codec,
75        final CompressionCodec compressor, final int count, final int size, final boolean sized)
76        throws IOException {
77      Cell[] cells = getCells(count, size);
78      CellScanner cellScanner = sized ? getSizedCellScanner(cells)
79          : CellUtil.createCellScanner(Arrays.asList(cells).iterator());
80      ByteBuffer bb = builder.buildCellBlock(codec, compressor, cellScanner);
81      cellScanner = builder.createCellScanner(codec, compressor, bb);
82      int i = 0;
83      while (cellScanner.advance()) {
84        i++;
85      }
86      assertEquals(count, i);
87    }
88  
89    static CellScanner getSizedCellScanner(final Cell[] cells) {
90      int size = -1;
91      for (Cell cell : cells) {
92        size += CellUtil.estimatedSerializedSizeOf(cell);
93      }
94      final int totalSize = ClassSize.align(size);
95      final CellScanner cellScanner = CellUtil.createCellScanner(cells);
96      return new SizedCellScanner() {
97        @Override
98        public long heapSize() {
99          return totalSize;
100       }
101 
102       @Override
103       public Cell current() {
104         return cellScanner.current();
105       }
106 
107       @Override
108       public boolean advance() throws IOException {
109         return cellScanner.advance();
110       }
111     };
112   }
113 
114   static Cell[] getCells(final int howMany) {
115     return getCells(howMany, 1024);
116   }
117 
118   static Cell[] getCells(final int howMany, final int valueSize) {
119     Cell[] cells = new Cell[howMany];
120     byte[] value = new byte[valueSize];
121     for (int i = 0; i < howMany; i++) {
122       byte[] index = Bytes.toBytes(i);
123       KeyValue kv = new KeyValue(index, Bytes.toBytes("f"), index, value);
124       cells[i] = kv;
125     }
126     return cells;
127   }
128 
129   private static final String COUNT = "--count=";
130   private static final String SIZE = "--size=";
131 
132   /**
133    * Prints usage and then exits w/ passed <code>errCode</code>
134    * @param errCode
135    */
136   private static void usage(final int errCode) {
137     System.out.println("Usage: IPCUtil [options]");
138     System.out.println("Micro-benchmarking how changed sizes and counts work with buffer resizing");
139     System.out.println(" --count  Count of Cells");
140     System.out.println(" --size   Size of Cell values");
141     System.out.println("Example: IPCUtil --count=1024 --size=1024");
142     System.exit(errCode);
143   }
144 
145   private static void timerTests(final CellBlockBuilder builder, final int count, final int size,
146       final Codec codec, final CompressionCodec compressor) throws IOException {
147     final int cycles = 1000;
148     StopWatch timer = new StopWatch();
149     timer.start();
150     for (int i = 0; i < cycles; i++) {
151       timerTest(builder, timer, count, size, codec, compressor, false);
152     }
153     timer.stop();
154     LOG.info("Codec=" + codec + ", compression=" + compressor + ", sized=" + false + ", count=" +
155         count + ", size=" + size + ", + took=" + timer.getTime() + "ms");
156     timer.reset();
157     timer.start();
158     for (int i = 0; i < cycles; i++) {
159       timerTest(builder, timer, count, size, codec, compressor, true);
160     }
161     timer.stop();
162     LOG.info("Codec=" + codec + ", compression=" + compressor + ", sized=" + true + ", count=" +
163         count + ", size=" + size + ", + took=" + timer.getTime() + "ms");
164   }
165 
166   private static void timerTest(final CellBlockBuilder builder, final StopWatch timer,
167       final int count, final int size, final Codec codec, final CompressionCodec compressor,
168       final boolean sized) throws IOException {
169     doBuildCellBlockUndoCellBlock(builder, codec, compressor, count, size, sized);
170   }
171 
172   /**
173    * For running a few tests of methods herein.
174    * @param args
175    * @throws IOException
176    */
177   public static void main(String[] args) throws IOException {
178     int count = 1024;
179     int size = 10240;
180     for (String arg : args) {
181       if (arg.startsWith(COUNT)) {
182         count = Integer.parseInt(arg.replace(COUNT, ""));
183       } else if (arg.startsWith(SIZE)) {
184         size = Integer.parseInt(arg.replace(SIZE, ""));
185       } else {
186         usage(1);
187       }
188     }
189     CellBlockBuilder builder = new CellBlockBuilder(HBaseConfiguration.create());
190     ((Log4JLogger) CellBlockBuilder.LOG).getLogger().setLevel(Level.ALL);
191     timerTests(builder, count, size, new KeyValueCodec(), null);
192     timerTests(builder, count, size, new KeyValueCodec(), new DefaultCodec());
193     timerTests(builder, count, size, new KeyValueCodec(), new GzipCodec());
194   }
195 }