coderplay
6/22/2015 - 11:02 PM

BytesToBytesMapBenchmark

BytesToBytesMapBenchmark

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.spark.unsafe.bench;


import org.apache.spark.unsafe.PlatformDependent;
import org.apache.spark.unsafe.map.BytesToBytesMap;
import org.apache.spark.unsafe.memory.ExecutorMemoryManager;
import org.apache.spark.unsafe.memory.MemoryAllocator;
import org.apache.spark.unsafe.memory.TaskMemoryManager;

import java.util.Random;


/**
 * This benchmark measures the time to fill a BytesToBytesMap.
 */
public class BytesToBytesMapBenchmark {

  private static final int kvLenInInt = 16;

  public static BytesToBytesMap createMap(int numRecords, int len, long keys, long values,
                                          TaskMemoryManager memoryManager) {

    BytesToBytesMap map = new BytesToBytesMap(memoryManager, (int) (numRecords * 1.05), true);

    long offset = 0;
    for (int i = 0; i < numRecords; i++) {
      offset += Integer.BYTES * kvLenInInt;
      BytesToBytesMap.Location loc = map.lookup(null, keys + offset, Integer.BYTES * kvLenInInt);
      loc.putNewKey(
          null, keys + offset, Integer.BYTES * kvLenInInt,
          null, values + offset, Integer.BYTES * kvLenInInt);
    }
    return map;
  }

  public static void main(String[] args) {
    int NUM_WARMUPS = 2;
    int NUM_ITERATIONS = 10;


    for (int exp = 32; exp <= 1024; exp <<= 1) {
      int numRecords = exp * 1000000;
      int len = numRecords;

      long keys = PlatformDependent.UNSAFE.allocateMemory(Integer.BYTES * kvLenInInt * len);
      long values = PlatformDependent.UNSAFE.allocateMemory(Integer.BYTES * kvLenInInt * len);

      Random rand = new Random(System.nanoTime());
      long offset = 0;
      for (int i = 0; i < len; i++) {
        for(int j = 0; j < kvLenInInt; j++) {
          PlatformDependent.UNSAFE.putInt(null, keys + offset, rand.nextInt());
          PlatformDependent.UNSAFE.putInt(null, values + offset, rand.nextInt());
          offset += Integer.BYTES;
        }
      }


      TaskMemoryManager memoryManager = new TaskMemoryManager(new ExecutorMemoryManager(MemoryAllocator.UNSAFE));

      for (int i = 0; i < NUM_WARMUPS; i++) {
        BytesToBytesMap map = createMap(numRecords, len, keys, values, memoryManager);
        map.free();
      }
      for (int i = 0; i < NUM_ITERATIONS; i++) {
        System.gc();
        long startTime = System.currentTimeMillis();
        BytesToBytesMap map = createMap(numRecords, len, keys, values, memoryManager);
        long endTime = System.currentTimeMillis();
        System.out.println("records: " + numRecords + "\t ops: " + (double) numRecords / (endTime - startTime) * 1000
            + "\t avg probe:" + map.getAverageProbesPerLookup());
        map.free();
      }

      PlatformDependent.UNSAFE.freeMemory(keys);
      PlatformDependent.UNSAFE.freeMemory(values);

    }
  }
}