Created
September 17, 2021 21:07
-
-
Save fsarradin/1b2b078caf64326af33c0300c053d69d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.univalence.data.{Stock, StockKey, StockKeyBinarySerde, StockBinarySerde} | |
import java.time.Instant | |
import java.time.temporal.ChronoUnit | |
import org.openjdk.jmh.annotations._ | |
import scala.util.Random | |
abstract class BaseKeyValueStoreBenchmark { | |
val products = List("café", "chocolat", "miel") | |
def keyGenerator: StockKey = { | |
val index = Random.nextInt(products.length) | |
StockKey( | |
store = "Plouzané", | |
product = products(index) | |
) | |
} | |
def stockGenerator(key: StockKey): Stock = { | |
val date = Instant.now() | |
val quantity = Random.nextInt(1000) | |
Stock( | |
key = key, | |
timestamp = date, | |
quantity = quantity, | |
expiry = date.plus(365, ChronoUnit.DAYS) | |
) | |
} | |
var key: StockKey = _ | |
var stock: Stock = _ | |
def createDatabase: KeyValueStore[StockKey, Stock] | |
val database: KeyValueStore[StockKey, Stock] = { | |
val db = createDatabase | |
products.foreach { product => | |
val key = StockKey(store = "Plouzané", product = product) | |
db.update(key, stockGenerator(key)) | |
} | |
db | |
} | |
@Setup | |
def generatorData(): Unit = { | |
key = keyGenerator | |
stock = stockGenerator(key) | |
} | |
@Benchmark | |
def writeData(): Unit = { | |
database.update(key, stock) | |
} | |
@Benchmark | |
def readData(): Unit = { | |
database.find(key) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Benchmark Mode Cnt Score Error Units | |
MapKeyValueStoreBenchmark.readData thrpt 3 35663325.389 ± 16830047.840 ops/s | |
TreeKeyValueStoreBenchmark.readData thrpt 3 5416510.813 ± 3421117.881 ops/s | |
SkipListKeyValueStoreBenchmark.readData thrpt 3 2724573.140 ± 304407.805 ops/s | |
MapKeyValueStoreBenchmark.writeData thrpt 3 36386675.442 ± 2857020.984 ops/s | |
TreeKeyValueStoreBenchmark.writeData thrpt 3 10281612.091 ± 923446.995 ops/s | |
SkipListKeyValueStoreBenchmark.writeData thrpt 3 2722610.652 ± 471604.437 ops/s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.univalence.data.{Stock, StockKey} | |
import io.univalence.database.binary.InMemoryKeyValueStore | |
import org.openjdk.jmh.annotations._ | |
@State(Scope.Thread) | |
@BenchmarkMode(Array(Mode.Throughput)) | |
class InMemoryKeyValueStoreBenchmark extends BaseKeyValueStoreBenchmark { | |
override def createDatabase: KeyValueStore[StockKey, Stock] = | |
new InMemoryKeyValueStore[StockKey, Stock] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InMemoryKVStore[K, V] extends KeyValueStore[K, V] { | |
val data = scala.collection.mutable.Map.empty[K, V] | |
// scala.collection.mutable.TreeMap.empty[K, V] + comparator | |
// new java.util.concurrent.ConcurrentSkipListMap[K, V]() + comparator | |
override def put(key: K, value: V): Unit = | |
data(key) = value | |
override def get(key: K): V = | |
data(key) | |
override def delete(key: K): Unit = | |
data -= key | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment