|
| 1 | +package com.target.nativememoryallocator.examples.map.offheap.eviction.operationcounters |
| 2 | + |
| 3 | +import com.target.nativememoryallocator.allocator.NativeMemoryAllocatorBuilder |
| 4 | +import com.target.nativememoryallocator.examples.map.utils.CacheObject |
| 5 | +import com.target.nativememoryallocator.examples.map.utils.CacheObjectSerializer |
| 6 | +import com.target.nativememoryallocator.examples.map.utils.buildRandomString |
| 7 | +import com.target.nativememoryallocator.map.NativeMemoryMapBackend |
| 8 | +import com.target.nativememoryallocator.map.NativeMemoryMapBuilder |
| 9 | +import kotlinx.coroutines.* |
| 10 | +import mu.KotlinLogging |
| 11 | +import kotlin.random.Random |
| 12 | + |
| 13 | +private val logger = KotlinLogging.logger {} |
| 14 | + |
| 15 | + |
| 16 | +/** |
| 17 | + * Same as OffHeapEviction but enables operation counters |
| 18 | + */ |
| 19 | +private class OffHeapEvictionOperationCounters { |
| 20 | + |
| 21 | + private val numEntries = 20_000 |
| 22 | + |
| 23 | + private val randomIndex = Random.nextInt(0, numEntries) |
| 24 | + |
| 25 | + private val nativeMemoryAllocator = NativeMemoryAllocatorBuilder( |
| 26 | + pageSizeBytes = 4_096, // 4 KB |
| 27 | + nativeMemorySizeBytes = (20L * 1_024L * 1_024L * 1_024L), // 20 GB |
| 28 | + ).build() |
| 29 | + |
| 30 | + private val nativeMemoryMap = NativeMemoryMapBuilder<Int, CacheObject>( |
| 31 | + valueSerializer = CacheObjectSerializer(), |
| 32 | + nativeMemoryAllocator = nativeMemoryAllocator, |
| 33 | + backend = NativeMemoryMapBackend.CAFFEINE, |
| 34 | + caffeineConfigFunction = { caffeineConfigBuilder -> |
| 35 | + caffeineConfigBuilder |
| 36 | + .maximumSize(10_000) |
| 37 | + .recordStats() |
| 38 | + }, |
| 39 | + enableOperationCounters = true |
| 40 | + ).build() |
| 41 | + |
| 42 | + private fun putValueIntoMap(i: Int) { |
| 43 | + if ((i % 100) == 0) { |
| 44 | + logger.info { "put i = $i" } |
| 45 | + } |
| 46 | + val value = buildRandomString(length = 500 * 1_024) |
| 47 | + if (i == randomIndex) { |
| 48 | + logger.info { "put randomIndex = $randomIndex value.length = ${value.length}" } |
| 49 | + logger.info { "value.substring(0,20) = ${value.substring(0, 20)}" } |
| 50 | + } |
| 51 | + nativeMemoryMap.put( |
| 52 | + key = i, |
| 53 | + value = CacheObject( |
| 54 | + s = value, |
| 55 | + ), |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + suspend fun run() { |
| 60 | + logger.info { "begin run randomIndex = $randomIndex" } |
| 61 | + |
| 62 | + coroutineScope { |
| 63 | + (0 until numEntries).forEach { i -> |
| 64 | + launch { |
| 65 | + putValueIntoMap(i = i) |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + logger.info { "nativeMemoryMap.size = ${nativeMemoryMap.size}" } |
| 71 | + logger.info { "nativeMemoryAllocator.nativeMemoryAllocatorMetadata = ${nativeMemoryAllocator.nativeMemoryAllocatorMetadata}" } |
| 72 | + |
| 73 | + val randomIndexValue = nativeMemoryMap.get(key = randomIndex) |
| 74 | + randomIndexValue?.let { |
| 75 | + logger.info { "get randomIndex = $randomIndex" } |
| 76 | + logger.info { "randomIndexValue.s.length = ${it.s.length}" } |
| 77 | + logger.info { "randomIndexValue.s.substring(0,20) = ${it.s.substring(0, 20)}" } |
| 78 | + } |
| 79 | + |
| 80 | + logger.info { "caffeine eviction count = ${nativeMemoryMap.stats.caffeineStats?.evictionCount()}" } |
| 81 | + |
| 82 | + logger.info { "nativeMemoryMap.operationCounters = ${nativeMemoryMap.operationCounters}" } |
| 83 | + |
| 84 | + while (true) { |
| 85 | + delay(1_000) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +suspend fun main() { |
| 92 | + withContext(Dispatchers.Default) { |
| 93 | + OffHeapEvictionOperationCounters().run() |
| 94 | + } |
| 95 | +} |
0 commit comments