Speeding MurMur up

Adding more test cases
Mapping out memory used by filter
This commit is contained in:
Vitor Pamplona
2025-02-20 17:10:24 -05:00
parent aaf86bf53e
commit 84a52a1ce0
7 changed files with 388 additions and 126 deletions
@@ -24,6 +24,8 @@ import androidx.benchmark.junit4.BenchmarkRule
import androidx.benchmark.junit4.measureRepeated
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
import com.vitorpamplona.quartz.nip01Core.hints.HintIndexer
import com.vitorpamplona.quartz.utils.RandomInstance
import org.junit.Rule
@@ -35,20 +37,22 @@ import java.nio.charset.Charset
class HintIndexerBenchmark {
@get:Rule val benchmarkRule = BenchmarkRule()
val keys =
mutableListOf<ByteArray>().apply {
for (seed in 0..1_000_000) {
add(RandomInstance.bytes(32))
companion object {
val keys =
mutableListOf<HexKey>().apply {
for (seed in 0..1_000_000) {
add(RandomInstance.bytes(32).toHexKey())
}
}
}
val relays =
getInstrumentation()
.context.assets
.open("relayDB.txt")
.readBytes()
.toString(Charset.forName("utf-8"))
.split("\n")
val relays =
getInstrumentation()
.context.assets
.open("relayDB.txt")
.readBytes()
.toString(Charset.forName("utf-8"))
.split("\n")
}
@Test
fun relayUriHashcode() {
@@ -63,14 +67,14 @@ class HintIndexerBenchmark {
keys.forEach { key ->
(0..5).map {
indexer.index(key, relays.random())
indexer.addKey(key, relays.random())
}
}
val key = keys.random()
benchmarkRule.measureRepeated {
indexer.get(key)
indexer.getKey(key)
}
}
@@ -80,7 +84,7 @@ class HintIndexerBenchmark {
val indexer = HintIndexer()
keys.forEach { key ->
(0..5).map {
indexer.index(key, relays.random())
indexer.addKey(key, relays.random())
}
}
}
@@ -0,0 +1,46 @@
/**
* Copyright (c) 2024 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.quartz.benchmark
import androidx.benchmark.junit4.BenchmarkRule
import androidx.benchmark.junit4.measureRepeated
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.vitorpamplona.quartz.nip01Core.hints.bloom.MurmurHash3
import com.vitorpamplona.quartz.utils.RandomInstance
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class MurMurBenchmark {
@get:Rule val benchmarkRule = BenchmarkRule()
@Test
fun hash() {
val hasher = MurmurHash3()
val byteArray = RandomInstance.bytes(32)
benchmarkRule.measureRepeated {
hasher.hash(byteArray, 293)
}
}
}