From 311c091a37c2e7eb907230bc0ade078b49f2ae29 Mon Sep 17 00:00:00 2001 From: "Kenneth J. Shackleton" Date: Sat, 1 Jun 2024 12:48:11 +0100 Subject: [PATCH] JMH benchmark for LruCache. Signed-off-by: Kenneth J. Shackleton --- .../cache/benchmarks/LruCacheBenchmark.kt | 51 +++++++++++++++++++ .../com/bloomberg/selekt/SQLConnection.kt | 1 + .../bloomberg/selekt/{ => cache}/LruCache.kt | 9 ++-- .../com/bloomberg/selekt/LruCacheTest.kt | 1 + 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 selekt-java/src/jmh/kotlin/com/bloomberg/selekt/cache/benchmarks/LruCacheBenchmark.kt rename selekt-java/src/main/kotlin/com/bloomberg/selekt/{ => cache}/LruCache.kt (83%) diff --git a/selekt-java/src/jmh/kotlin/com/bloomberg/selekt/cache/benchmarks/LruCacheBenchmark.kt b/selekt-java/src/jmh/kotlin/com/bloomberg/selekt/cache/benchmarks/LruCacheBenchmark.kt new file mode 100644 index 0000000000..9ae099b5e4 --- /dev/null +++ b/selekt-java/src/jmh/kotlin/com/bloomberg/selekt/cache/benchmarks/LruCacheBenchmark.kt @@ -0,0 +1,51 @@ +/* + * Copyright 2024 Bloomberg Finance L.P. + * + * 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 + * + * https://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 com.bloomberg.selekt.cache.benchmarks + +import com.bloomberg.selekt.cache.LruCache +import org.openjdk.jmh.annotations.Benchmark +import org.openjdk.jmh.annotations.BenchmarkMode +import org.openjdk.jmh.annotations.Level +import org.openjdk.jmh.annotations.Mode +import org.openjdk.jmh.annotations.Scope +import org.openjdk.jmh.annotations.Setup +import org.openjdk.jmh.annotations.State + +@State(Scope.Thread) +open class CacheInput { + internal lateinit var cache: LruCache + + @Setup(Level.Iteration) + fun setUp() { + cache = LruCache(1) {} + } +} + +open class LruCacheBenchmark { + @Benchmark + @BenchmarkMode(Mode.Throughput) + fun getEntry(input: CacheInput) = input.cache.run { + this["1", {}] + } + + @Benchmark + @BenchmarkMode(Mode.Throughput) + fun getEntryWithEviction(input: CacheInput) = input.cache.run { + this["1", {}] + this["2", {}] + } +} diff --git a/selekt-java/src/main/kotlin/com/bloomberg/selekt/SQLConnection.kt b/selekt-java/src/main/kotlin/com/bloomberg/selekt/SQLConnection.kt index 3bbb97edbb..4e71010efd 100644 --- a/selekt-java/src/main/kotlin/com/bloomberg/selekt/SQLConnection.kt +++ b/selekt-java/src/main/kotlin/com/bloomberg/selekt/SQLConnection.kt @@ -16,6 +16,7 @@ package com.bloomberg.selekt +import com.bloomberg.selekt.cache.LruCache import com.bloomberg.selekt.commons.forEachByPosition import com.bloomberg.selekt.commons.forUntil import javax.annotation.concurrent.NotThreadSafe diff --git a/selekt-java/src/main/kotlin/com/bloomberg/selekt/LruCache.kt b/selekt-java/src/main/kotlin/com/bloomberg/selekt/cache/LruCache.kt similarity index 83% rename from selekt-java/src/main/kotlin/com/bloomberg/selekt/LruCache.kt rename to selekt-java/src/main/kotlin/com/bloomberg/selekt/cache/LruCache.kt index 30bf9a32ef..06a50e3a14 100644 --- a/selekt-java/src/main/kotlin/com/bloomberg/selekt/LruCache.kt +++ b/selekt-java/src/main/kotlin/com/bloomberg/selekt/cache/LruCache.kt @@ -14,15 +14,18 @@ * limitations under the License. */ -package com.bloomberg.selekt +package com.bloomberg.selekt.cache import javax.annotation.concurrent.NotThreadSafe private const val NO_RESIZE_LOAD_FACTOR = 1.1f @NotThreadSafe -internal class LruCache(private val maxSize: Int, private val disposal: (T) -> Unit) { - private val store = object : LinkedHashMap(maxSize, NO_RESIZE_LOAD_FACTOR, true) { +class LruCache(private val maxSize: Int, private val disposal: (T) -> Unit) { + @PublishedApi + @JvmField + @JvmSynthetic + internal val store = object : LinkedHashMap(maxSize, NO_RESIZE_LOAD_FACTOR, true) { override fun removeEldestEntry(eldest: MutableMap.MutableEntry) = (size > maxSize).also { if (it) { disposal(eldest.value) diff --git a/selekt-java/src/test/kotlin/com/bloomberg/selekt/LruCacheTest.kt b/selekt-java/src/test/kotlin/com/bloomberg/selekt/LruCacheTest.kt index cba2c475a3..7f7692499d 100644 --- a/selekt-java/src/test/kotlin/com/bloomberg/selekt/LruCacheTest.kt +++ b/selekt-java/src/test/kotlin/com/bloomberg/selekt/LruCacheTest.kt @@ -16,6 +16,7 @@ package com.bloomberg.selekt +import com.bloomberg.selekt.cache.LruCache import org.junit.jupiter.api.Test import org.mockito.kotlin.anyOrNull import org.mockito.kotlin.doReturn