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