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

cache.CacheReadBenchmark Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package cache;

import io.scalecube.transport.memoizer.Memoizer;

import com.google.common.base.Optional;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;

import java.util.concurrent.TimeUnit;

import javax.annotation.Nonnull;

@Fork(2)
@State(Scope.Benchmark)
@Threads(4)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class CacheReadBenchmark {

  static final String[] GENERIC_ARGS = new String[] {"java.lang.String", "java.lang.Long", "java.lang.Integer",
      "java.lang.Short", "java.lang.Character", "java.lang.Byte", "io.scalecube.transport.protocol.MessageSchema",};

  static final String[] GENERIC_ARGS_CACHE_MISS = new String[] {"java.lang.String", "xyz", "java.lang.Integer", "abc",
      "java.lang.Character", "qwerty", "io.scalecube.transport.protocol.MessageSchema",};

  LoadingCache> guavaCache = CacheBuilder.newBuilder().build(
      new CacheLoader>() {
        @Override
        public Optional load(@Nonnull String className) {
          try {
            Class dataClass = Class.forName(className);
            return Optional.of(dataClass);
          } catch (ClassNotFoundException e) {
            return Optional.absent();
          }
        }
      });

  Memoizer> memoizer = new Memoizer<>( (String className) -> {
    try {
      Class dataClass = Class.forName(className);
      return Optional.of(dataClass);
    } catch (ClassNotFoundException e) {
      return Optional.absent();
    }
  });

  @Benchmark
  public void readFromGuavaCache() {
    for (String arg : GENERIC_ARGS) {
      guavaCache.getUnchecked(arg);
    }
  }

  @Benchmark
  public void readFromMemoizer() {
    for (String arg : GENERIC_ARGS) {
      memoizer.get(arg);
    }
  }

  @Benchmark
  public void readFromGuavaWithCacheMiss() {
    for (String arg : GENERIC_ARGS_CACHE_MISS) {
      guavaCache.getUnchecked(arg);
    }
  }

  @Benchmark
  public void readFromMemoizerWithCacheMiss() {
    for (String arg : GENERIC_ARGS_CACHE_MISS) {
      memoizer.get(arg);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy