-
Notifications
You must be signed in to change notification settings - Fork 167
/
LDBStoreBench.scala
95 lines (77 loc) · 3.14 KB
/
LDBStoreBench.scala
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package org.ergoplatform.db
import com.google.common.primitives.Longs
import org.ergoplatform.modifiers.history.BlockTransactions
import org.ergoplatform.settings.Algos
import org.ergoplatform.utils.generators.ErgoTransactionGenerators
import org.ergoplatform.wallet.utils.TestFileUtils
import org.iq80.leveldb.Options
import org.scalameter.KeyValue
import org.scalameter.api.{Bench, Gen, _}
import org.scalameter.picklers.Implicits._
import scorex.crypto.hash.Digest32
import scorex.util.idToBytes
import scorex.db.LDBFactory.factory
import scorex.db.{LDBKVStore, LDBVersionedStore}
import scala.util.Random
object LDBStoreBench
extends Bench.ForkedTime
with ErgoTransactionGenerators
with TestFileUtils {
private val options = new Options()
options.createIfMissing(true)
private val db1 = factory.open(createTempDir, options)
private def storeLDB() = new LDBKVStore(db1)
private def storeLVDB() = new LDBVersionedStore(createTempDir, initialKeepVersions = 400)
private val modsNumGen = Gen.enumeration("modifiers number")(1000)
val txsGen: Gen[Seq[BlockTransactions]] = modsNumGen.map { num =>
(0 to num).flatMap { _ =>
invalidBlockTransactionsGen(defaultMinerPk, 10).sample
}
}
val txsWithDbGen: Gen[(Seq[BlockTransactions], LDBKVStore)] = txsGen.map { bts =>
val toInsert = bts.map(bt => idToBytes(bt.headerId) -> bt.bytes).toArray
val db = storeLDB()
toInsert.grouped(5).foreach(kv => db.insert(kv.map(_._1), kv.map(_._2)).get)
bts -> storeLDB
}
private val config = Seq[KeyValue](
exec.minWarmupRuns -> 1,
exec.maxWarmupRuns -> 1,
exec.benchRuns -> 10,
exec.requireGC -> true
)
private def randomVersion: Digest32 = Algos.hash(Longs.toByteArray(Random.nextLong()))
private def benchWriteLDB(bts: Seq[BlockTransactions]): Unit = {
val toInsert = bts.map(bt => idToBytes(bt.headerId) -> bt.bytes).toArray
val db = storeLDB()
toInsert.grouped(5).foreach(kv => db.insert(kv.map(_._1), kv.map(_._2)).get)
}
private def benchReadLDB(bts: Seq[BlockTransactions], db: LDBKVStore): Unit = {
bts.foreach { bt => db.get(idToBytes(bt.headerId)) }
}
private def benchWriteLVDB(bts: Seq[BlockTransactions]): Unit = {
val toInsert = bts.map(bt => idToBytes(bt.headerId) -> bt.bytes)
val db = storeLVDB()
db.update(randomVersion, List.empty, toInsert).get
}
private def benchWriteReadLVDB(bts: Seq[BlockTransactions]): Unit = {
val toInsert = bts.map(bt => idToBytes(bt.headerId) -> bt.bytes)
val db = storeLVDB()
db.update(randomVersion, List.empty, toInsert).get
bts.foreach { bt => db.get(idToBytes(bt.headerId)) }
}
performance of "LDBStore" in {
performance of "LDBStore write" in {
using(txsGen) config(config: _*) in (bts => benchWriteLDB(bts))
}
performance of "LDBStore read" in {
using(txsWithDbGen) config(config: _*) in { case (bts, db) => benchReadLDB(bts, db) }
}
performance of "LVDBStore write" in {
using(txsGen) config(config: _*) in (bts => benchWriteLVDB(bts))
}
performance of "LVDBStore write/read" in {
using(txsGen) config(config: _*) in (bts => benchWriteReadLVDB(bts))
}
}
}