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.apache.yetus.audience.InterfaceAudience;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028/**
029 * Code shared by PE tests.
030 */
031@InterfaceAudience.Private
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 (
059      !org.apache.hadoop.hbase.util.Bytes.equals(expected, 0, expected.length, gotArray,
060        gotArrayOffset, gotArrayLength)
061    ) {
062      throw new AssertionError(
063        "Expected " + org.apache.hadoop.hbase.util.Bytes.toString(expected) + " but got "
064          + org.apache.hadoop.hbase.util.Bytes.toString(gotArray, gotArrayOffset, gotArrayLength));
065    }
066  }
067
068  public static void concurrentReads(final Runnable r) {
069    final int count = 1;
070    long now = EnvironmentEdgeManager.currentTime();
071    List<Thread> threads = new ArrayList<>(count);
072    for (int i = 0; i < count; i++) {
073      threads.add(new Thread(r, "concurrentRead-" + i));
074    }
075    for (Thread t : threads) {
076      t.start();
077    }
078    for (Thread t : threads) {
079      try {
080        t.join();
081      } catch (InterruptedException e) {
082        e.printStackTrace();
083      }
084    }
085    LOG.info("Test took " + (EnvironmentEdgeManager.currentTime() - now));
086  }
087}