1   /**
2    * Copyright 2009 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.nio.ByteBuffer;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  
30  /**
31   * Code shared by PE tests.
32   */
33  public class PerformanceEvaluationCommons {
34    static final Log LOG =
35      LogFactory.getLog(PerformanceEvaluationCommons.class.getName());
36  
37    public static void assertValueSize(final int expectedSize, final int got) {
38      if (got != expectedSize) {
39        throw new AssertionError("Expected " + expectedSize + " but got " + got);
40      }
41    }
42  
43    public static void assertKey(final byte [] expected, final ByteBuffer got) {
44      byte [] b = new byte[got.limit()];
45      got.get(b, 0, got.limit());
46      assertKey(expected, b);
47    }
48  
49    public static void assertKey(final byte [] expected, final byte [] got) {
50      if (!org.apache.hadoop.hbase.util.Bytes.equals(expected, got)) {
51        throw new AssertionError("Expected " +
52          org.apache.hadoop.hbase.util.Bytes.toString(expected) +
53          " but got " + org.apache.hadoop.hbase.util.Bytes.toString(got));
54      }
55    }
56  
57    public static void concurrentReads(final Runnable r) {
58      final int count = 1;
59      long now = System.currentTimeMillis();
60      List<Thread> threads = new ArrayList<Thread>(count);
61      for (int i = 0; i < count; i++) {
62        Thread t = new Thread(r);
63        t.setName("" + i);
64        threads.add(t);
65      }
66      for (Thread t: threads) {
67        t.start();
68      }
69      for (Thread t: threads) {
70        try {
71          t.join();
72        } catch (InterruptedException e) {
73          e.printStackTrace();
74        }
75      }
76      LOG.info("Test took " + (System.currentTimeMillis() - now));
77    }
78  }