Fixes the benchmark testing cases
This commit is contained in:
+63
@@ -0,0 +1,63 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 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.test.platform.app.InstrumentationRegistry.getInstrumentation
|
||||||
|
import com.fasterxml.jackson.module.kotlin.readValue
|
||||||
|
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||||
|
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||||
|
import com.vitorpamplona.quartz.nip01Core.jackson.JsonMapper
|
||||||
|
import com.vitorpamplona.quartz.utils.LargeCache
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import java.util.function.Consumer
|
||||||
|
import java.util.zip.GZIPInputStream
|
||||||
|
|
||||||
|
open class BaseLargeCacheBenchmark {
|
||||||
|
fun getEventDB(): List<Event> {
|
||||||
|
// This file includes duplicates
|
||||||
|
val fullDBInputStream = getInstrumentation().context.assets.open("nostr_vitor_startup_data.json")
|
||||||
|
|
||||||
|
return JsonMapper.mapper.readValue<ArrayList<Event>>(
|
||||||
|
GZIPInputStream(fullDBInputStream),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getLargeCache(db: List<Event>): LargeCache<HexKey, Event> {
|
||||||
|
val cache = LargeCache<HexKey, Event>()
|
||||||
|
|
||||||
|
db.forEach {
|
||||||
|
cache.getOrCreate(it.id) { key ->
|
||||||
|
it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cache
|
||||||
|
}
|
||||||
|
|
||||||
|
fun hasId(event: Event) {
|
||||||
|
assertTrue(event.id.isNotEmpty())
|
||||||
|
}
|
||||||
|
|
||||||
|
val consumer =
|
||||||
|
Consumer<Event> {
|
||||||
|
hasId(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,6 +28,7 @@ import com.vitorpamplona.quartz.nip01Core.jackson.JsonMapper
|
|||||||
import com.vitorpamplona.quartz.nip01Core.verify
|
import com.vitorpamplona.quartz.nip01Core.verify
|
||||||
import com.vitorpamplona.quartz.nip01Core.verifyId
|
import com.vitorpamplona.quartz.nip01Core.verifyId
|
||||||
import com.vitorpamplona.quartz.nip01Core.verifySignature
|
import com.vitorpamplona.quartz.nip01Core.verifySignature
|
||||||
|
import com.vitorpamplona.quartz.nip10Notes.TextNoteEvent
|
||||||
import com.vitorpamplona.quartz.utils.TimeUtils
|
import com.vitorpamplona.quartz.utils.TimeUtils
|
||||||
import junit.framework.TestCase.assertTrue
|
import junit.framework.TestCase.assertTrue
|
||||||
import org.junit.Rule
|
import org.junit.Rule
|
||||||
@@ -90,7 +91,7 @@ class EventBenchmark {
|
|||||||
val now = TimeUtils.now()
|
val now = TimeUtils.now()
|
||||||
val tags = arrayOf(arrayOf(""))
|
val tags = arrayOf(arrayOf(""))
|
||||||
benchmarkRule.measureRepeated {
|
benchmarkRule.measureRepeated {
|
||||||
EventFactory.create("id", "pubkey", now, 1, tags, "content", "sig")
|
EventFactory.create<TextNoteEvent>("id", "pubkey", now, 1, tags, "content", "sig")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,7 +100,7 @@ class EventBenchmark {
|
|||||||
val now = TimeUtils.now()
|
val now = TimeUtils.now()
|
||||||
val tags = arrayOf(arrayOf(""))
|
val tags = arrayOf(arrayOf(""))
|
||||||
benchmarkRule.measureRepeated {
|
benchmarkRule.measureRepeated {
|
||||||
EventFactory.create("id", "pubkey", now, 30818, tags, "content", "sig")
|
EventFactory.create<TextNoteEvent>("id", "pubkey", now, 30818, tags, "content", "sig")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+39
-63
@@ -40,12 +40,10 @@ import com.vitorpamplona.quartz.nip59Giftwrap.rumors.Rumor
|
|||||||
import com.vitorpamplona.quartz.nip59Giftwrap.seals.SealedRumorEvent
|
import com.vitorpamplona.quartz.nip59Giftwrap.seals.SealedRumorEvent
|
||||||
import com.vitorpamplona.quartz.nip59Giftwrap.wraps.GiftWrapEvent
|
import com.vitorpamplona.quartz.nip59Giftwrap.wraps.GiftWrapEvent
|
||||||
import junit.framework.TestCase.assertNotNull
|
import junit.framework.TestCase.assertNotNull
|
||||||
import junit.framework.TestCase.assertTrue
|
import kotlinx.coroutines.runBlocking
|
||||||
import org.junit.Rule
|
import org.junit.Rule
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.junit.runner.RunWith
|
import org.junit.runner.RunWith
|
||||||
import java.util.concurrent.CountDownLatch
|
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Benchmark, which will execute on an Android device.
|
* Benchmark, which will execute on an Android device.
|
||||||
@@ -60,78 +58,56 @@ class GiftWrapReceivingBenchmark {
|
|||||||
fun createWrap(
|
fun createWrap(
|
||||||
sender: NostrSigner,
|
sender: NostrSigner,
|
||||||
receiver: NostrSigner,
|
receiver: NostrSigner,
|
||||||
): GiftWrapEvent {
|
): GiftWrapEvent =
|
||||||
val countDownLatch = CountDownLatch(1)
|
runBlocking {
|
||||||
var wrap: GiftWrapEvent? = null
|
GiftWrapEvent.create(
|
||||||
|
event =
|
||||||
sender.sign(
|
SealedRumorEvent.create(
|
||||||
ChatMessageEvent.build(
|
event =
|
||||||
msg = "Hi there! This is a test message",
|
sender.sign(
|
||||||
to =
|
ChatMessageEvent.build(
|
||||||
listOf(
|
msg = "Hi there! This is a test message",
|
||||||
PTag(receiver.pubKey),
|
to = listOf(PTag(receiver.pubKey)),
|
||||||
|
) {
|
||||||
|
changeSubject("Party Tonight")
|
||||||
|
zapraiser(10000)
|
||||||
|
contentWarning("nsfw")
|
||||||
|
},
|
||||||
|
),
|
||||||
|
encryptTo = receiver.pubKey,
|
||||||
|
signer = sender,
|
||||||
),
|
),
|
||||||
) {
|
recipientPubKey = receiver.pubKey,
|
||||||
changeSubject("Party Tonight")
|
)
|
||||||
zapraiser(10000)
|
|
||||||
contentWarning("nsfw")
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
SealedRumorEvent.create(
|
|
||||||
event = it,
|
|
||||||
encryptTo = receiver.pubKey,
|
|
||||||
signer = sender,
|
|
||||||
) {
|
|
||||||
GiftWrapEvent.create(
|
|
||||||
event = it,
|
|
||||||
recipientPubKey = receiver.pubKey,
|
|
||||||
) {
|
|
||||||
wrap = it
|
|
||||||
countDownLatch.countDown()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assertTrue(countDownLatch.await(1, TimeUnit.SECONDS))
|
|
||||||
|
|
||||||
return wrap!!
|
|
||||||
}
|
|
||||||
|
|
||||||
fun createSeal(
|
fun createSeal(
|
||||||
sender: NostrSigner,
|
sender: NostrSigner,
|
||||||
receiver: NostrSigner,
|
receiver: NostrSigner,
|
||||||
): SealedRumorEvent {
|
): SealedRumorEvent =
|
||||||
val countDownLatch = CountDownLatch(1)
|
runBlocking {
|
||||||
var seal: SealedRumorEvent? = null
|
val msg =
|
||||||
|
sender.sign(
|
||||||
|
ChatMessageEvent.build(
|
||||||
|
msg = "Hi there! This is a test message",
|
||||||
|
to =
|
||||||
|
listOf(
|
||||||
|
PTag(receiver.pubKey),
|
||||||
|
),
|
||||||
|
) {
|
||||||
|
changeSubject("Party Tonight")
|
||||||
|
zapraiser(10000)
|
||||||
|
contentWarning("nsfw")
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
sender.sign(
|
|
||||||
ChatMessageEvent.build(
|
|
||||||
msg = "Hi there! This is a test message",
|
|
||||||
to =
|
|
||||||
listOf(
|
|
||||||
PTag(receiver.pubKey),
|
|
||||||
),
|
|
||||||
) {
|
|
||||||
changeSubject("Party Tonight")
|
|
||||||
zapraiser(10000)
|
|
||||||
contentWarning("nsfw")
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
SealedRumorEvent.create(
|
SealedRumorEvent.create(
|
||||||
event = it,
|
event = msg,
|
||||||
encryptTo = receiver.pubKey,
|
encryptTo = receiver.pubKey,
|
||||||
signer = sender,
|
signer = sender,
|
||||||
) {
|
)
|
||||||
seal = it
|
|
||||||
countDownLatch.countDown()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assertTrue(countDownLatch.await(1, TimeUnit.SECONDS))
|
|
||||||
|
|
||||||
return seal!!
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun parseWrapFromString() {
|
fun parseWrapFromString() {
|
||||||
val sender = NostrSignerInternal(KeyPair())
|
val sender = NostrSignerInternal(KeyPair())
|
||||||
|
|||||||
+98
-118
@@ -32,12 +32,10 @@ import com.vitorpamplona.quartz.nip36SensitiveContent.contentWarning
|
|||||||
import com.vitorpamplona.quartz.nip57Zaps.zapraiser.zapraiser
|
import com.vitorpamplona.quartz.nip57Zaps.zapraiser.zapraiser
|
||||||
import com.vitorpamplona.quartz.nip59Giftwrap.seals.SealedRumorEvent
|
import com.vitorpamplona.quartz.nip59Giftwrap.seals.SealedRumorEvent
|
||||||
import com.vitorpamplona.quartz.nip59Giftwrap.wraps.GiftWrapEvent
|
import com.vitorpamplona.quartz.nip59Giftwrap.wraps.GiftWrapEvent
|
||||||
import junit.framework.TestCase.assertTrue
|
import kotlinx.coroutines.runBlocking
|
||||||
import org.junit.Rule
|
import org.junit.Rule
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.junit.runner.RunWith
|
import org.junit.runner.RunWith
|
||||||
import java.util.concurrent.CountDownLatch
|
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Benchmark, which will execute on an Android device.
|
* Benchmark, which will execute on an Android device.
|
||||||
@@ -55,25 +53,21 @@ class GiftWrapSigningBenchmark {
|
|||||||
val receiver = NostrSignerInternal(KeyPair())
|
val receiver = NostrSignerInternal(KeyPair())
|
||||||
|
|
||||||
benchmarkRule.measureRepeated {
|
benchmarkRule.measureRepeated {
|
||||||
val countDownLatch = CountDownLatch(1)
|
runBlocking {
|
||||||
|
sender.sign(
|
||||||
sender.sign(
|
ChatMessageEvent.build(
|
||||||
ChatMessageEvent.build(
|
msg = "Hi there! This is a test message",
|
||||||
msg = "Hi there! This is a test message",
|
to =
|
||||||
to =
|
listOf(
|
||||||
listOf(
|
PTag(receiver.pubKey),
|
||||||
PTag(receiver.pubKey),
|
),
|
||||||
),
|
) {
|
||||||
) {
|
changeSubject("Party Tonight")
|
||||||
changeSubject("Party Tonight")
|
zapraiser(10000)
|
||||||
zapraiser(10000)
|
contentWarning("nsfw")
|
||||||
contentWarning("nsfw")
|
},
|
||||||
},
|
)
|
||||||
) {
|
|
||||||
countDownLatch.countDown()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assertTrue(countDownLatch.await(1, TimeUnit.SECONDS))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,40 +76,31 @@ class GiftWrapSigningBenchmark {
|
|||||||
val sender = NostrSignerInternal(KeyPair())
|
val sender = NostrSignerInternal(KeyPair())
|
||||||
val receiver = NostrSignerInternal(KeyPair())
|
val receiver = NostrSignerInternal(KeyPair())
|
||||||
|
|
||||||
val countDownLatch = CountDownLatch(1)
|
val msg =
|
||||||
|
runBlocking {
|
||||||
var msg: ChatMessageEvent? = null
|
sender.sign(
|
||||||
|
ChatMessageEvent.build(
|
||||||
sender.sign(
|
msg = "Hi there! This is a test message",
|
||||||
ChatMessageEvent.build(
|
to =
|
||||||
msg = "Hi there! This is a test message",
|
listOf(
|
||||||
to =
|
PTag(receiver.pubKey),
|
||||||
listOf(
|
),
|
||||||
PTag(receiver.pubKey),
|
) {
|
||||||
),
|
changeSubject("Party Tonight")
|
||||||
) {
|
zapraiser(10000)
|
||||||
changeSubject("Party Tonight")
|
contentWarning("nsfw")
|
||||||
zapraiser(10000)
|
},
|
||||||
contentWarning("nsfw")
|
)
|
||||||
},
|
|
||||||
) {
|
|
||||||
msg = it
|
|
||||||
countDownLatch.countDown()
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(countDownLatch.await(1, TimeUnit.SECONDS))
|
|
||||||
|
|
||||||
benchmarkRule.measureRepeated {
|
|
||||||
val countDownLatch2 = CountDownLatch(1)
|
|
||||||
SealedRumorEvent.create(
|
|
||||||
event = msg!!,
|
|
||||||
encryptTo = receiver.pubKey,
|
|
||||||
signer = sender,
|
|
||||||
) {
|
|
||||||
countDownLatch2.countDown()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assertTrue(countDownLatch2.await(1, TimeUnit.SECONDS))
|
benchmarkRule.measureRepeated {
|
||||||
|
runBlocking {
|
||||||
|
SealedRumorEvent.create(
|
||||||
|
event = msg,
|
||||||
|
encryptTo = receiver.pubKey,
|
||||||
|
signer = sender,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,44 +109,39 @@ class GiftWrapSigningBenchmark {
|
|||||||
val sender = NostrSignerInternal(KeyPair())
|
val sender = NostrSignerInternal(KeyPair())
|
||||||
val receiver = NostrSignerInternal(KeyPair())
|
val receiver = NostrSignerInternal(KeyPair())
|
||||||
|
|
||||||
val countDownLatch = CountDownLatch(1)
|
val msg =
|
||||||
|
runBlocking {
|
||||||
var seal: SealedRumorEvent? = null
|
sender.sign(
|
||||||
|
ChatMessageEvent.build(
|
||||||
sender.sign(
|
msg = "Hi there! This is a test message",
|
||||||
ChatMessageEvent.build(
|
to =
|
||||||
msg = "Hi there! This is a test message",
|
listOf(
|
||||||
to =
|
PTag(receiver.pubKey),
|
||||||
listOf(
|
),
|
||||||
PTag(receiver.pubKey),
|
) {
|
||||||
),
|
changeSubject("Party Tonight")
|
||||||
) {
|
zapraiser(10000)
|
||||||
changeSubject("Party Tonight")
|
contentWarning("nsfw")
|
||||||
zapraiser(10000)
|
},
|
||||||
contentWarning("nsfw")
|
)
|
||||||
},
|
|
||||||
) {
|
|
||||||
SealedRumorEvent.create(
|
|
||||||
event = it,
|
|
||||||
encryptTo = receiver.pubKey,
|
|
||||||
signer = sender,
|
|
||||||
) {
|
|
||||||
seal = it
|
|
||||||
countDownLatch.countDown()
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(countDownLatch.await(1, TimeUnit.SECONDS))
|
val seal =
|
||||||
|
runBlocking {
|
||||||
|
SealedRumorEvent.create(
|
||||||
|
event = msg,
|
||||||
|
encryptTo = receiver.pubKey,
|
||||||
|
signer = sender,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
benchmarkRule.measureRepeated {
|
benchmarkRule.measureRepeated {
|
||||||
val countDownLatch2 = CountDownLatch(1)
|
runBlocking {
|
||||||
GiftWrapEvent.create(
|
GiftWrapEvent.create(
|
||||||
event = seal!!,
|
event = seal!!,
|
||||||
recipientPubKey = receiver.pubKey,
|
recipientPubKey = receiver.pubKey,
|
||||||
) {
|
)
|
||||||
countDownLatch2.countDown()
|
|
||||||
}
|
}
|
||||||
assertTrue(countDownLatch2.await(1, TimeUnit.SECONDS))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,40 +150,40 @@ class GiftWrapSigningBenchmark {
|
|||||||
val sender = NostrSignerInternal(KeyPair())
|
val sender = NostrSignerInternal(KeyPair())
|
||||||
val receiver = NostrSignerInternal(KeyPair())
|
val receiver = NostrSignerInternal(KeyPair())
|
||||||
|
|
||||||
val countDownLatch = CountDownLatch(1)
|
val msg =
|
||||||
|
runBlocking {
|
||||||
var wrap: GiftWrapEvent? = null
|
sender.sign(
|
||||||
|
ChatMessageEvent.build(
|
||||||
sender.sign(
|
msg = "Hi there! This is a test message",
|
||||||
ChatMessageEvent.build(
|
to =
|
||||||
msg = "Hi there! This is a test message",
|
listOf(
|
||||||
to =
|
PTag(receiver.pubKey),
|
||||||
listOf(
|
),
|
||||||
PTag(receiver.pubKey),
|
) {
|
||||||
),
|
changeSubject("Party Tonight")
|
||||||
) {
|
zapraiser(10000)
|
||||||
changeSubject("Party Tonight")
|
contentWarning("nsfw")
|
||||||
zapraiser(10000)
|
},
|
||||||
contentWarning("nsfw")
|
)
|
||||||
},
|
|
||||||
) {
|
|
||||||
SealedRumorEvent.create(
|
|
||||||
event = it,
|
|
||||||
encryptTo = receiver.pubKey,
|
|
||||||
signer = sender,
|
|
||||||
) {
|
|
||||||
GiftWrapEvent.create(
|
|
||||||
event = it,
|
|
||||||
recipientPubKey = receiver.pubKey,
|
|
||||||
) {
|
|
||||||
wrap = it
|
|
||||||
countDownLatch.countDown()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(countDownLatch.await(1, TimeUnit.SECONDS))
|
val seal =
|
||||||
|
runBlocking {
|
||||||
|
SealedRumorEvent.create(
|
||||||
|
event = msg,
|
||||||
|
encryptTo = receiver.pubKey,
|
||||||
|
signer = sender,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
benchmarkRule.measureRepeated { wrap!!.toJson() }
|
val wrap =
|
||||||
|
runBlocking {
|
||||||
|
GiftWrapEvent.create(
|
||||||
|
event = seal,
|
||||||
|
recipientPubKey = receiver.pubKey,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
benchmarkRule.measureRepeated { wrap.toJson() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
@@ -27,6 +27,7 @@ import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
|
|||||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||||
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
|
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
|
||||||
import com.vitorpamplona.quartz.nip01Core.hints.HintIndexer
|
import com.vitorpamplona.quartz.nip01Core.hints.HintIndexer
|
||||||
|
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.RelayUrlNormalizer
|
||||||
import com.vitorpamplona.quartz.utils.RandomInstance
|
import com.vitorpamplona.quartz.utils.RandomInstance
|
||||||
import org.junit.Rule
|
import org.junit.Rule
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
@@ -52,6 +53,7 @@ class HintIndexerBenchmark {
|
|||||||
.readBytes()
|
.readBytes()
|
||||||
.toString(Charset.forName("utf-8"))
|
.toString(Charset.forName("utf-8"))
|
||||||
.split("\n")
|
.split("\n")
|
||||||
|
.mapNotNull(RelayUrlNormalizer::normalizeOrNull)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
+2
-42
@@ -23,54 +23,14 @@ package com.vitorpamplona.quartz.benchmark
|
|||||||
import androidx.benchmark.junit4.BenchmarkRule
|
import androidx.benchmark.junit4.BenchmarkRule
|
||||||
import androidx.benchmark.junit4.measureRepeated
|
import androidx.benchmark.junit4.measureRepeated
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||||
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
|
|
||||||
import com.fasterxml.jackson.module.kotlin.readValue
|
|
||||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
|
||||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
|
||||||
import com.vitorpamplona.quartz.nip01Core.jackson.JsonMapper
|
|
||||||
import com.vitorpamplona.quartz.utils.LargeCache
|
|
||||||
import org.junit.Assert.assertTrue
|
|
||||||
import org.junit.Rule
|
import org.junit.Rule
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.junit.runner.RunWith
|
import org.junit.runner.RunWith
|
||||||
import java.util.Arrays
|
import java.util.Arrays
|
||||||
import java.util.function.Consumer
|
import kotlin.collections.distinctBy
|
||||||
import java.util.zip.GZIPInputStream
|
|
||||||
|
|
||||||
open class BaseLargeCacheBenchmark {
|
|
||||||
fun getEventDB(): List<Event> {
|
|
||||||
// This file includes duplicates
|
|
||||||
val fullDBInputStream = getInstrumentation().context.assets.open("nostr_vitor_startup_data.json")
|
|
||||||
|
|
||||||
return JsonMapper.mapper.readValue<ArrayList<Event>>(
|
|
||||||
GZIPInputStream(fullDBInputStream),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getLargeCache(db: List<Event>): LargeCache<HexKey, Event> {
|
|
||||||
val cache = LargeCache<HexKey, Event>()
|
|
||||||
|
|
||||||
db.forEach {
|
|
||||||
cache.getOrCreate(it.id) { key ->
|
|
||||||
it
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return cache
|
|
||||||
}
|
|
||||||
|
|
||||||
fun hasId(event: Event) {
|
|
||||||
assertTrue(event.id.isNotEmpty())
|
|
||||||
}
|
|
||||||
|
|
||||||
val consumer =
|
|
||||||
Consumer<Event> {
|
|
||||||
hasId(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@RunWith(AndroidJUnit4::class)
|
@RunWith(AndroidJUnit4::class)
|
||||||
class LargeCacheForEachBenchmark : BaseLargeCacheBenchmark() {
|
class LargeCacheBenchmark : BaseLargeCacheBenchmark() {
|
||||||
@get:Rule val benchmarkRule = BenchmarkRule()
|
@get:Rule val benchmarkRule = BenchmarkRule()
|
||||||
|
|
||||||
// 191,353 ns 0 allocs Trace EMULATOR_LargeCacheForEachBenchmark.benchForEachConsumerList
|
// 191,353 ns 0 allocs Trace EMULATOR_LargeCacheForEachBenchmark.benchForEachConsumerList
|
||||||
|
|||||||
Reference in New Issue
Block a user