All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.internal.offheap.MemoryChunkJUnitTestBase Maven / Gradle / Ivy

There is a newer version: 2.0-BETA
Show newest version
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * Licensed 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. See accompanying
 * LICENSE file.
 */
package com.gemstone.gemfire.internal.offheap;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;

import junit.framework.TestCase;

public abstract class MemoryChunkJUnitTestBase extends TestCase {

  
  protected abstract MemoryChunk createChunk(int size);
  
  public void testByteReadWrite() {
    int CHUNK_SIZE = 1024;
    MemoryChunk mc = createChunk(CHUNK_SIZE);
    try {
    for (int i=0; i computeHistogram(AtomicIntegerArray originalValues, final int granualarity) {
    int[] values = new int[originalValues.length()];
    for (int i=0; i < values.length; i++) {
      values[i] = originalValues.get(i);
    }
    Arrays.sort(values);
    ArrayList result = new ArrayList();
    Bucket curBucket = new Bucket(values[0]);
    result.add(curBucket);
    for (int i=1; i < values.length; i++) {
      int curVal = values[i];
      if (!curBucket.addValue(curVal, granualarity)) {
        curBucket = new Bucket(curVal);
        result.add(curBucket);
      }
    }
    return result;
  }
  static private class Bucket {
    public Bucket(long l) {
      base = l;
      total = l;
      count = 1;
    }
    public boolean addValue(long curVal, int granualarity) {
      if (curVal < base || (curVal-base) > granualarity) {
        return false;
      }
      total += curVal;
      count++;
      return true;
    }
    private final long base;
    private long total;
    private int count;
    
    @Override
    public String toString() {
      return "" + (total/count) + ":" + count;
    }
  }
  public void DISABLEtest256ByteArrayPerf() {
    byte[] writeBytes = new byte[256];
    for (int i=0; i < writeBytes.length; i++) {
      writeBytes[i] = 1;
    }
    int ARRAYS_PER_CHUNK = 100000;
    int CHUNK_SIZE = ARRAYS_PER_CHUNK * writeBytes.length;
    MemoryChunk mc = createChunk(CHUNK_SIZE);
    try {
      int WRITE_ITERATIONS = 2000;
      long startWrite = System.nanoTime();
      for (int j=0; j




© 2015 - 2024 Weber Informatics LLC | Privacy Policy