-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/improveMptPerformance' into phase/daedalus
- Loading branch information
Showing
8 changed files
with
143 additions
and
58 deletions.
There are no files selected for viewing
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
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
90 changes: 90 additions & 0 deletions
90
src/benchmark/scala/io/iohk/ethereum/mpt/MerklePatriciaTreeSpeedSpec.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package io.iohk.ethereum.mpt | ||
|
||
import java.io.File | ||
import java.nio.file.Files | ||
|
||
import io.iohk.ethereum.db.dataSource.{EphemDataSource, LevelDBDataSource, LevelDbConfig} | ||
import io.iohk.ethereum.db.storage.{ArchiveNodeStorage, NodeStorage} | ||
import io.iohk.ethereum.mpt.MerklePatriciaTrie.defaultByteArraySerializable | ||
import io.iohk.ethereum.utils.Logger | ||
import io.iohk.ethereum.{ObjectGenerators, crypto} | ||
import org.scalatest.FunSuite | ||
import org.scalatest.prop.PropertyChecks | ||
import org.spongycastle.util.encoders.Hex | ||
|
||
class MerklePatriciaTreeSpeedSpec extends FunSuite | ||
with PropertyChecks | ||
with ObjectGenerators | ||
with Logger | ||
with PersistentStorage { | ||
|
||
test("Performance test (From: https://github.com/ethereum/wiki/wiki/Benchmarks)") { | ||
val Rounds = 1000 | ||
val Symmetric = true | ||
|
||
val start: Long = System.currentTimeMillis | ||
val emptyTrie = MerklePatriciaTrie[Array[Byte], Array[Byte]](new ArchiveNodeStorage(new NodeStorage(EphemDataSource()))) | ||
var seed: Array[Byte] = Array.fill(32)(0.toByte) | ||
|
||
val trieResult = (0 until Rounds).foldLeft(emptyTrie) { case (recTrie, i) => | ||
seed = Node.hashFn(seed) | ||
if (!Symmetric) recTrie.put(seed, seed) | ||
else { | ||
val mykey = seed | ||
seed = Node.hashFn(seed) | ||
val myval = if ((seed(0) & 0xFF) % 2 == 1) Array[Byte](seed.last) else seed | ||
recTrie.put(mykey, myval) | ||
} | ||
} | ||
val rootHash = Hex.toHexString(trieResult.getRootHash) | ||
|
||
log.debug("Time taken(ms): " + (System.currentTimeMillis - start)) | ||
log.debug("Root hash obtained: " + rootHash) | ||
|
||
if (Symmetric) assert(rootHash.take(4) == "36f6" && rootHash.drop(rootHash.length - 4) == "93a3") | ||
else assert(rootHash.take(4) == "da8a" && rootHash.drop(rootHash.length - 4) == "0ca4") | ||
} | ||
|
||
test("MPT benchmark") { | ||
withNodeStorage { ns => | ||
val hashFn = crypto.kec256(_: Array[Byte]) | ||
|
||
val defaultByteArraySer = MerklePatriciaTrie.defaultByteArraySerializable | ||
val EmptyTrie = MerklePatriciaTrie[Array[Byte], Array[Byte]](ns)(defaultByteArraySer, defaultByteArraySer) | ||
|
||
var t = System.currentTimeMillis() | ||
(1 to 20000000).foldLeft(EmptyTrie){case (trie, i) => | ||
val k = hashFn(("hello" + i).getBytes) | ||
val v = hashFn(("world" + i).getBytes) | ||
|
||
if (i % 100000 == 0) { | ||
val newT = System.currentTimeMillis() | ||
val delta = (newT - t) / 1000.0 | ||
t = newT | ||
log.debug(s"=== $i elements put, time for batch is: $delta sec") | ||
} | ||
trie.put(k, v) | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
trait PersistentStorage { | ||
def withNodeStorage(testCode: NodesKeyValueStorage => Unit): Unit = { | ||
val dbPath = Files.createTempDirectory("testdb").toAbsolutePath.toString | ||
val dataSource = LevelDBDataSource(new LevelDbConfig { | ||
override val verifyChecksums: Boolean = true | ||
override val paranoidChecks: Boolean = true | ||
override val createIfMissing: Boolean = true | ||
override val path: String = dbPath | ||
}) | ||
|
||
try { | ||
testCode(new ArchiveNodeStorage(new NodeStorage(dataSource))) | ||
} finally { | ||
val dir = new File(dbPath) | ||
!dir.exists() || dir.delete() | ||
} | ||
} | ||
} |
File renamed without changes.
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
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
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
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