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    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 byte [] got) {
49      if (!org.apache.hadoop.hbase.util.Bytes.equals(expected, got)) {
50        throw new AssertionError("Expected " +
51          org.apache.hadoop.hbase.util.Bytes.toString(expected) +
52          " but got " + org.apache.hadoop.hbase.util.Bytes.toString(got));
53      }
54    }
55  
56    public static void concurrentReads(final Runnable r) {
57      final int count = 1;
58      long now = System.currentTimeMillis();
59      List<Thread> threads = new ArrayList<Thread>(count);
60      for (int i = 0; i < count; i++) {
61        Thread t = new Thread(r);
62        t.setName("" + i);
63        threads.add(t);
64      }
65      for (Thread t: threads) {
66        t.start();
67      }
68      for (Thread t: threads) {
69        try {
70          t.join();
71        } catch (InterruptedException e) {
72          e.printStackTrace();
73        }
74      }
75      LOG.info("Test took " + (System.currentTimeMillis() - now));
76    }
77  }