CacheableBenchmarks.scala 2.8 KB
Newer Older
梦境迷离's avatar
梦境迷离 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/*
 * Copyright (c) 2022 bitlap
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package org.bitlap.cacheable.benchmark

import scala.util.Try

import org.bitlap.cacheable.core.cacheable
import org.openjdk.jmh.annotations._
import zio.ZIO

import java.util.concurrent.TimeUnit
import scala.util.Random

/**
 * benchmark @cacheable
 *
 * @author 梦境迷离
 * @version 1.0,2022/3/22
 */
@State(Scope.Thread)
@BenchmarkMode(Array(Mode.Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
@Measurement(iterations = 5)
@Warmup(iterations = 5)
@Fork(3)
class CacheableBenchmarks extends BenchmarkRuntime {

  // make it use cache only sometimes
  // Smaller numbers are easier to hit the cache
  // 25% hit cache
  @Param(Array("2"))
  var limitRandNum: Int = _

  @Benchmark
梦境迷离's avatar
梦境迷离 已提交
54
  def benchmarkRedisCache(): Unit =
梦境迷离's avatar
梦境迷离 已提交
55 56 57
    execute[String](cacheableRedis(Random.nextInt(limitRandNum), Random.nextInt(limitRandNum) + ""))

  @Benchmark
梦境迷离's avatar
梦境迷离 已提交
58
  def benchmarkCaffeineCache(): Unit =
梦境迷离's avatar
梦境迷离 已提交
59 60 61
    execute[String](cacheableCaffeine(Random.nextInt(limitRandNum), Random.nextInt(limitRandNum) + ""))

  @Benchmark
梦境迷离's avatar
梦境迷离 已提交
62
  def benchmarkNoCache(): Unit =
梦境迷离's avatar
梦境迷离 已提交
63 64 65
    execute[String](unCacheable(Random.nextInt(limitRandNum), Random.nextInt(limitRandNum) + ""))

  @cacheable(local = false) // use RedisExecutor.live, not RedisExecutor.local
梦境迷离's avatar
梦境迷离 已提交
66
  @inline def cacheableRedis(id: Int, key: String): ZIO[Any, Throwable, String] =
梦境迷离's avatar
梦境迷离 已提交
67 68 69 70 71 72
    ZIO.effect {
      Try(Thread.sleep(5)).getOrElse(()) // Simulate a JDBC request
      Random.nextInt() + ""
    }

  @cacheable(local = true)
梦境迷离's avatar
梦境迷离 已提交
73
  @inline def cacheableCaffeine(id: Int, key: String): ZIO[Any, Throwable, String] =
梦境迷离's avatar
梦境迷离 已提交
74 75 76 77 78
    ZIO.effect {
      Try(Thread.sleep(5)).getOrElse(())
      Random.nextInt() + ""
    }

梦境迷离's avatar
梦境迷离 已提交
79
  @inline def unCacheable(id: Int, key: String): ZIO[Any, Throwable, String] =
梦境迷离's avatar
梦境迷离 已提交
80 81 82 83 84
    ZIO.effect {
      Try(Thread.sleep(5)).getOrElse(())
      Random.nextInt() + ""
    }
}