001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase;
020
021import java.nio.ByteBuffer;
022import java.util.ArrayList;
023import java.util.List;
024
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028
029/**
030 * Code shared by PE tests.
031 */
032public class PerformanceEvaluationCommons {
033  private static final Logger LOG =
034    LoggerFactory.getLogger(PerformanceEvaluationCommons.class.getName());
035
036  public static void assertValueSize(final int expectedSize, final int got) {
037    if (got != expectedSize) {
038      throw new AssertionError("Expected " + expectedSize + " but got " + got);
039    }
040  }
041
042  public static void assertKey(final byte [] expected, final ByteBuffer got) {
043    byte [] b = new byte[got.limit()];
044    got.get(b, 0, got.limit());
045    assertKey(expected, b);
046  }
047
048  public static void assertKey(final byte [] expected, final Cell c) {
049    assertKey(expected, c.getRowArray(), c.getRowOffset(), c.getRowLength());
050  }
051
052  public static void assertKey(final byte [] expected, final byte [] got) {
053    assertKey(expected, got, 0, got.length);
054  }
055
056  public static void assertKey(final byte [] expected, final byte [] gotArray,
057      final int gotArrayOffset, final int gotArrayLength) {
058    if (!org.apache.hadoop.hbase.util.Bytes.equals(expected, 0, expected.length,
059        gotArray, gotArrayOffset, gotArrayLength)) {
060      throw new AssertionError("Expected " +
061        org.apache.hadoop.hbase.util.Bytes.toString(expected) +
062        " but got " +
063        org.apache.hadoop.hbase.util.Bytes.toString(gotArray, gotArrayOffset, gotArrayLength));
064    }
065  }
066
067  public static void concurrentReads(final Runnable r) {
068    final int count = 1;
069    long now = System.currentTimeMillis();
070    List<Thread> threads = new ArrayList<>(count);
071    for (int i = 0; i < count; i++) {
072      threads.add(new Thread(r, "concurrentRead-" + i));
073    }
074    for (Thread t: threads) {
075      t.start();
076    }
077    for (Thread t: threads) {
078      try {
079        t.join();
080      } catch (InterruptedException e) {
081        e.printStackTrace();
082      }
083    }
084    LOG.info("Test took " + (System.currentTimeMillis() - now));
085  }
086}