View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase;
20  
21  import java.nio.ByteBuffer;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  
29  /**
30   * Code shared by PE tests.
31   */
32  public class PerformanceEvaluationCommons {
33    private static final Log LOG =
34      LogFactory.getLog(PerformanceEvaluationCommons.class.getName());
35  
36    public static void assertValueSize(final int expectedSize, final int got) {
37      if (got != expectedSize) {
38        throw new AssertionError("Expected " + expectedSize + " but got " + got);
39      }
40    }
41  
42    public static void assertKey(final byte [] expected, final ByteBuffer got) {
43      byte [] b = new byte[got.limit()];
44      got.get(b, 0, got.limit());
45      assertKey(expected, b);
46    }
47  
48    public static void assertKey(final byte [] expected, final Cell c) {
49      assertKey(expected, c.getRowArray(), c.getRowOffset(), c.getRowLength());
50    }
51  
52    public static void assertKey(final byte [] expected, final byte [] got) {
53      assertKey(expected, got, 0, got.length);
54    }
55  
56    public static void assertKey(final byte [] expected, final byte [] gotArray,
57        final int gotArrayOffset, final int gotArrayLength) {
58      if (!org.apache.hadoop.hbase.util.Bytes.equals(expected, 0, expected.length,
59          gotArray, gotArrayOffset, gotArrayLength)) {
60        throw new AssertionError("Expected " +
61          org.apache.hadoop.hbase.util.Bytes.toString(expected) +
62          " but got " +
63          org.apache.hadoop.hbase.util.Bytes.toString(gotArray, gotArrayOffset, gotArrayLength));
64      }
65    }
66  
67    public static void concurrentReads(final Runnable r) {
68      final int count = 1;
69      long now = System.currentTimeMillis();
70      List<Thread> threads = new ArrayList<Thread>(count);
71      for (int i = 0; i < count; i++) {
72        Thread t = new Thread(r);
73        t.setName("" + i);
74        threads.add(t);
75      }
76      for (Thread t: threads) {
77        t.start();
78      }
79      for (Thread t: threads) {
80        try {
81          t.join();
82        } catch (InterruptedException e) {
83          e.printStackTrace();
84        }
85      }
86      LOG.info("Test took " + (System.currentTimeMillis() - now));
87    }
88  }