1   /**
2    * Copyright 2007 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase;
21  
22  import java.io.IOException;
23  import java.nio.ByteBuffer;
24  import java.util.Random;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.commons.math.random.RandomData;
29  import org.apache.commons.math.random.RandomDataImpl;
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.fs.FileSystem;
32  import org.apache.hadoop.fs.Path;
33  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
34  import org.apache.hadoop.hbase.io.hfile.CacheConfig;
35  import org.apache.hadoop.hbase.io.hfile.Compression;
36  import org.apache.hadoop.hbase.io.hfile.HFile;
37  import org.apache.hadoop.hbase.io.hfile.HFileScanner;
38  import org.apache.hadoop.hbase.util.Bytes;
39  
40  /**
41   * <p>
42   * This class runs performance benchmarks for {@link HFile}.
43   * </p>
44   */
45  public class HFilePerformanceEvaluation {
46  
47    private static final int ROW_LENGTH = 10;
48    private static final int ROW_COUNT = 1000000;
49    private static final int RFILE_BLOCKSIZE = 8 * 1024;
50  
51    static final Log LOG =
52      LogFactory.getLog(HFilePerformanceEvaluation.class.getName());
53  
54    static byte [] format(final int i) {
55      String v = Integer.toString(i);
56      return Bytes.toBytes("0000000000".substring(v.length()) + v);
57    }
58  
59    static ImmutableBytesWritable format(final int i, ImmutableBytesWritable w) {
60      w.set(format(i));
61      return w;
62    }
63  
64    private void runBenchmarks() throws Exception {
65      final Configuration conf = new Configuration();
66      final FileSystem fs = FileSystem.get(conf);
67      final Path mf = fs.makeQualified(new Path("performanceevaluation.mapfile"));
68      if (fs.exists(mf)) {
69        fs.delete(mf, true);
70      }
71  
72      runBenchmark(new SequentialWriteBenchmark(conf, fs, mf, ROW_COUNT),
73          ROW_COUNT);
74      PerformanceEvaluationCommons.concurrentReads(new Runnable() {
75        public void run() {
76          try {
77            runBenchmark(new UniformRandomSmallScan(conf, fs, mf, ROW_COUNT),
78              ROW_COUNT);
79          } catch (Exception e) {
80            e.printStackTrace();
81          }
82        }
83      });
84      PerformanceEvaluationCommons.concurrentReads(new Runnable() {
85        public void run() {
86          try {
87            runBenchmark(new UniformRandomReadBenchmark(conf, fs, mf, ROW_COUNT),
88                ROW_COUNT);
89          } catch (Exception e) {
90            e.printStackTrace();
91          }
92        }
93      });
94      PerformanceEvaluationCommons.concurrentReads(new Runnable() {
95        public void run() {
96          try {
97            runBenchmark(new GaussianRandomReadBenchmark(conf, fs, mf, ROW_COUNT),
98                ROW_COUNT);
99          } catch (Exception e) {
100           e.printStackTrace();
101         }
102       }
103     });
104     PerformanceEvaluationCommons.concurrentReads(new Runnable() {
105       public void run() {
106         try {
107           runBenchmark(new SequentialReadBenchmark(conf, fs, mf, ROW_COUNT),
108               ROW_COUNT);
109         } catch (Exception e) {
110           e.printStackTrace();
111         }
112       }
113     });
114 
115   }
116 
117   protected void runBenchmark(RowOrientedBenchmark benchmark, int rowCount)
118     throws Exception {
119     LOG.info("Running " + benchmark.getClass().getSimpleName() + " for " +
120         rowCount + " rows.");
121     long elapsedTime = benchmark.run();
122     LOG.info("Running " + benchmark.getClass().getSimpleName() + " for " +
123         rowCount + " rows took " + elapsedTime + "ms.");
124   }
125 
126   static abstract class RowOrientedBenchmark {
127 
128     protected final Configuration conf;
129     protected final FileSystem fs;
130     protected final Path mf;
131     protected final int totalRows;
132 
133     public RowOrientedBenchmark(Configuration conf, FileSystem fs, Path mf,
134         int totalRows) {
135       this.conf = conf;
136       this.fs = fs;
137       this.mf = mf;
138       this.totalRows = totalRows;
139     }
140 
141     void setUp() throws Exception {
142       // do nothing
143     }
144 
145     abstract void doRow(int i) throws Exception;
146 
147     protected int getReportingPeriod() {
148       return this.totalRows / 10;
149     }
150 
151     void tearDown() throws Exception {
152       // do nothing
153     }
154 
155     /**
156      * Run benchmark
157      * @return elapsed time.
158      * @throws Exception
159      */
160     long run() throws Exception {
161       long elapsedTime;
162       setUp();
163       long startTime = System.currentTimeMillis();
164       try {
165         for (int i = 0; i < totalRows; i++) {
166           if (i > 0 && i % getReportingPeriod() == 0) {
167             LOG.info("Processed " + i + " rows.");
168           }
169           doRow(i);
170         }
171         elapsedTime = System.currentTimeMillis() - startTime;
172       } finally {
173         tearDown();
174       }
175       return elapsedTime;
176     }
177 
178   }
179 
180   static class SequentialWriteBenchmark extends RowOrientedBenchmark {
181     protected HFile.Writer writer;
182     private Random random = new Random();
183     private byte[] bytes = new byte[ROW_LENGTH];
184 
185     public SequentialWriteBenchmark(Configuration conf, FileSystem fs, Path mf,
186         int totalRows) {
187       super(conf, fs, mf, totalRows);
188     }
189 
190     @Override
191     void setUp() throws Exception {
192       writer =
193         HFile.getWriterFactory(conf).createWriter(this.fs,
194             this.mf, RFILE_BLOCKSIZE, (Compression.Algorithm) null, null);
195     }
196 
197     @Override
198     void doRow(int i) throws Exception {
199       writer.append(format(i), generateValue());
200     }
201 
202     private byte[] generateValue() {
203       random.nextBytes(bytes);
204       return bytes;
205     }
206 
207     @Override
208     protected int getReportingPeriod() {
209       return this.totalRows; // don't report progress
210     }
211 
212     @Override
213     void tearDown() throws Exception {
214       writer.close();
215     }
216 
217   }
218 
219   static abstract class ReadBenchmark extends RowOrientedBenchmark {
220 
221     protected HFile.Reader reader;
222 
223     public ReadBenchmark(Configuration conf, FileSystem fs, Path mf,
224         int totalRows) {
225       super(conf, fs, mf, totalRows);
226     }
227 
228     @Override
229     void setUp() throws Exception {
230       reader = HFile.createReader(this.fs, this.mf, new CacheConfig(this.conf));
231       this.reader.loadFileInfo();
232     }
233 
234     @Override
235     void tearDown() throws Exception {
236       reader.close();
237     }
238 
239   }
240 
241   static class SequentialReadBenchmark extends ReadBenchmark {
242     private HFileScanner scanner;
243 
244     public SequentialReadBenchmark(Configuration conf, FileSystem fs,
245       Path mf, int totalRows) {
246       super(conf, fs, mf, totalRows);
247     }
248 
249     @Override
250     void setUp() throws Exception {
251       super.setUp();
252       this.scanner = this.reader.getScanner(false, false);
253       this.scanner.seekTo();
254     }
255 
256     @Override
257     void doRow(int i) throws Exception {
258       if (this.scanner.next()) {
259         ByteBuffer k = this.scanner.getKey();
260         PerformanceEvaluationCommons.assertKey(format(i + 1), k);
261         ByteBuffer v = scanner.getValue();
262         PerformanceEvaluationCommons.assertValueSize(v.limit(), ROW_LENGTH);
263       }
264     }
265 
266     @Override
267     protected int getReportingPeriod() {
268       return this.totalRows; // don't report progress
269     }
270 
271   }
272 
273   static class UniformRandomReadBenchmark extends ReadBenchmark {
274 
275     private Random random = new Random();
276 
277     public UniformRandomReadBenchmark(Configuration conf, FileSystem fs,
278         Path mf, int totalRows) {
279       super(conf, fs, mf, totalRows);
280     }
281 
282     @Override
283     void doRow(int i) throws Exception {
284       HFileScanner scanner = this.reader.getScanner(false, true);
285       byte [] b = getRandomRow();
286       scanner.seekTo(b);
287       ByteBuffer k = scanner.getKey();
288       PerformanceEvaluationCommons.assertKey(b, k);
289       ByteBuffer v = scanner.getValue();
290       PerformanceEvaluationCommons.assertValueSize(v.limit(), ROW_LENGTH);
291     }
292 
293     private byte [] getRandomRow() {
294       return format(random.nextInt(totalRows));
295     }
296   }
297 
298   static class UniformRandomSmallScan extends ReadBenchmark {
299     private Random random = new Random();
300 
301     public UniformRandomSmallScan(Configuration conf, FileSystem fs,
302         Path mf, int totalRows) {
303       super(conf, fs, mf, totalRows/10);
304     }
305 
306     @Override
307     void doRow(int i) throws Exception {
308       HFileScanner scanner = this.reader.getScanner(false, false);
309       byte [] b = getRandomRow();
310       if (scanner.seekTo(b) != 0) {
311         System.out.println("Nonexistent row: " + new String(b));
312         return;
313       }
314       ByteBuffer k = scanner.getKey();
315       PerformanceEvaluationCommons.assertKey(b, k);
316       // System.out.println("Found row: " + new String(b));
317       for (int ii = 0; ii < 30; ii++) {
318         if (!scanner.next()) {
319           System.out.println("NOTHING FOLLOWS");
320         }
321         ByteBuffer v = scanner.getValue();
322         PerformanceEvaluationCommons.assertValueSize(v.limit(), ROW_LENGTH);
323       }
324     }
325 
326     private byte [] getRandomRow() {
327       return format(random.nextInt(totalRows));
328     }
329   }
330 
331   static class GaussianRandomReadBenchmark extends ReadBenchmark {
332 
333     private RandomData randomData = new RandomDataImpl();
334 
335     public GaussianRandomReadBenchmark(Configuration conf, FileSystem fs,
336         Path mf, int totalRows) {
337       super(conf, fs, mf, totalRows);
338     }
339 
340     @Override
341     void doRow(int i) throws Exception {
342       HFileScanner scanner = this.reader.getScanner(false, true);
343       scanner.seekTo(getGaussianRandomRowBytes());
344       for (int ii = 0; ii < 30; ii++) {
345         if (!scanner.next()) {
346           System.out.println("NOTHING FOLLOWS");
347         }
348         scanner.getKey();
349         scanner.getValue();
350       }
351     }
352 
353     private byte [] getGaussianRandomRowBytes() {
354       int r = (int) randomData.nextGaussian((double)totalRows / 2.0,
355           (double)totalRows / 10.0);
356       return format(r);
357     }
358   }
359 
360   /**
361    * @param args
362    * @throws Exception
363    * @throws IOException
364    */
365   public static void main(String[] args) throws Exception {
366     new HFilePerformanceEvaluation().runBenchmarks();
367   }
368 }