Fixes the benchmark testing cases

This commit is contained in:
Vitor Pamplona
2025-07-25 14:29:14 -04:00
parent ca9777be83
commit 3a37d5e42d
6 changed files with 207 additions and 225 deletions
@@ -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")
} }
} }
} }
@@ -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,50 +58,35 @@ 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 =
SealedRumorEvent.create(
event =
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(PTag(receiver.pubKey)),
listOf(
PTag(receiver.pubKey),
),
) { ) {
changeSubject("Party Tonight") changeSubject("Party Tonight")
zapraiser(10000) zapraiser(10000)
contentWarning("nsfw") contentWarning("nsfw")
}, },
) { ),
SealedRumorEvent.create(
event = it,
encryptTo = receiver.pubKey, encryptTo = receiver.pubKey,
signer = sender, signer = sender,
) { ),
GiftWrapEvent.create(
event = it,
recipientPubKey = receiver.pubKey, 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( sender.sign(
ChatMessageEvent.build( ChatMessageEvent.build(
msg = "Hi there! This is a test message", msg = "Hi there! This is a test message",
@@ -116,20 +99,13 @@ class GiftWrapReceivingBenchmark {
zapraiser(10000) zapraiser(10000)
contentWarning("nsfw") 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
@@ -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,8 +53,7 @@ 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",
@@ -69,11 +66,8 @@ class GiftWrapSigningBenchmark {
zapraiser(10000) zapraiser(10000)
contentWarning("nsfw") contentWarning("nsfw")
}, },
) { )
countDownLatch.countDown()
} }
assertTrue(countDownLatch.await(1, TimeUnit.SECONDS))
} }
} }
@@ -82,10 +76,8 @@ 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( sender.sign(
ChatMessageEvent.build( ChatMessageEvent.build(
msg = "Hi there! This is a test message", msg = "Hi there! This is a test message",
@@ -98,24 +90,17 @@ class GiftWrapSigningBenchmark {
zapraiser(10000) zapraiser(10000)
contentWarning("nsfw") contentWarning("nsfw")
}, },
) { )
msg = it
countDownLatch.countDown()
} }
assertTrue(countDownLatch.await(1, TimeUnit.SECONDS))
benchmarkRule.measureRepeated { benchmarkRule.measureRepeated {
val countDownLatch2 = CountDownLatch(1) runBlocking {
SealedRumorEvent.create( SealedRumorEvent.create(
event = msg!!, event = msg,
encryptTo = receiver.pubKey, encryptTo = receiver.pubKey,
signer = sender, signer = sender,
) { )
countDownLatch2.countDown()
} }
assertTrue(countDownLatch2.await(1, TimeUnit.SECONDS))
} }
} }
@@ -124,10 +109,8 @@ 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( sender.sign(
ChatMessageEvent.build( ChatMessageEvent.build(
msg = "Hi there! This is a test message", msg = "Hi there! This is a test message",
@@ -140,28 +123,25 @@ class GiftWrapSigningBenchmark {
zapraiser(10000) zapraiser(10000)
contentWarning("nsfw") contentWarning("nsfw")
}, },
) { )
}
val seal =
runBlocking {
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))
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,10 +150,8 @@ 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( sender.sign(
ChatMessageEvent.build( ChatMessageEvent.build(
msg = "Hi there! This is a test message", msg = "Hi there! This is a test message",
@@ -186,24 +164,26 @@ class GiftWrapSigningBenchmark {
zapraiser(10000) zapraiser(10000)
contentWarning("nsfw") contentWarning("nsfw")
}, },
) { )
}
val seal =
runBlocking {
SealedRumorEvent.create( SealedRumorEvent.create(
event = it, event = msg,
encryptTo = receiver.pubKey, encryptTo = receiver.pubKey,
signer = sender, signer = sender,
) { )
}
val wrap =
runBlocking {
GiftWrapEvent.create( GiftWrapEvent.create(
event = it, event = seal,
recipientPubKey = receiver.pubKey, recipientPubKey = receiver.pubKey,
) { )
wrap = it
countDownLatch.countDown()
}
}
} }
assertTrue(countDownLatch.await(1, TimeUnit.SECONDS)) benchmarkRule.measureRepeated { wrap.toJson() }
benchmarkRule.measureRepeated { wrap!!.toJson() }
} }
} }
@@ -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
@@ -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