mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-27 23:36:21 +02:00
Single method to test performance of receiving a GiftWrap.
This commit is contained in:
@@ -3,8 +3,11 @@ package com.vitorpamplona.amethyst.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 com.vitorpamplona.quartz.crypto.CryptoUtils
|
||||||
import com.vitorpamplona.quartz.encoders.toHexKey
|
import com.vitorpamplona.quartz.encoders.toHexKey
|
||||||
import com.vitorpamplona.quartz.crypto.KeyPair
|
import com.vitorpamplona.quartz.crypto.KeyPair
|
||||||
|
import com.vitorpamplona.quartz.events.Event
|
||||||
|
import com.vitorpamplona.quartz.events.GiftWrapEvent
|
||||||
import com.vitorpamplona.quartz.events.NIP24Factory
|
import com.vitorpamplona.quartz.events.NIP24Factory
|
||||||
import com.vitorpamplona.quartz.events.SealedGossipEvent
|
import com.vitorpamplona.quartz.events.SealedGossipEvent
|
||||||
import org.junit.Assert
|
import org.junit.Assert
|
||||||
@@ -53,6 +56,38 @@ class GiftWrapBenchmark {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun receivePerformanceTest(message: String) {
|
||||||
|
val sender = KeyPair()
|
||||||
|
val receiver = KeyPair()
|
||||||
|
|
||||||
|
val giftWrap = NIP24Factory().createMsgNIP24(
|
||||||
|
message,
|
||||||
|
listOf(receiver.pubKey.toHexKey()),
|
||||||
|
sender.privKey!!
|
||||||
|
).first()
|
||||||
|
|
||||||
|
val keyToUse = if (giftWrap.recipientPubKey() == sender.pubKey.toHexKey()) sender.privKey!! else receiver.privKey!!
|
||||||
|
val giftWrapJson = giftWrap.toJson()
|
||||||
|
|
||||||
|
// Simulate Receiver
|
||||||
|
benchmarkRule.measureRepeated {
|
||||||
|
CryptoUtils.clearCache()
|
||||||
|
|
||||||
|
val wrap = Event.fromJson(giftWrapJson) as GiftWrapEvent
|
||||||
|
wrap.checkSignature()
|
||||||
|
|
||||||
|
val seal = wrap.unwrap(keyToUse)
|
||||||
|
seal?.checkSignature()
|
||||||
|
|
||||||
|
if (seal is SealedGossipEvent) {
|
||||||
|
val innerData = seal.unseal(keyToUse)
|
||||||
|
Assert.assertEquals(message, innerData?.content)
|
||||||
|
} else {
|
||||||
|
Assert.fail("Wrong Event")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun tinyMessageHardCoded() {
|
fun tinyMessageHardCoded() {
|
||||||
@@ -77,6 +112,25 @@ class GiftWrapBenchmark {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun receivesTinyMessage() {
|
||||||
|
receivePerformanceTest("Hola, que tal?")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun receivesRegularMessage() {
|
||||||
|
receivePerformanceTest("Hi, honey, can you drop by the market and get some bread?")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun receivesLongMessageHardCoded() {
|
||||||
|
receivePerformanceTest(
|
||||||
|
"My queen, you are nothing short of royalty to me. You possess more beauty in the nail of your pinkie toe than everything else in this world combined. I am astounded by your grace, generosity, and graciousness. I am so lucky to know you. "
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@Test
|
@Test
|
||||||
fun tinyMessageHardCodedCompressed() {
|
fun tinyMessageHardCodedCompressed() {
|
||||||
|
@@ -74,6 +74,50 @@ class GiftWrapReceivingBenchmark {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun checkId() {
|
||||||
|
val sender = KeyPair()
|
||||||
|
val receiver = KeyPair()
|
||||||
|
|
||||||
|
val senderPublicKey = CryptoUtils.pubkeyCreate(sender.privKey!!).toHexKey()
|
||||||
|
val senderMessage = createMessage(sender, receiver)
|
||||||
|
|
||||||
|
val wrap = GiftWrapEvent.create(
|
||||||
|
event = SealedGossipEvent.create(
|
||||||
|
event = senderMessage,
|
||||||
|
encryptTo = senderPublicKey,
|
||||||
|
privateKey = sender.privKey!!
|
||||||
|
),
|
||||||
|
recipientPubKey = senderPublicKey
|
||||||
|
)
|
||||||
|
|
||||||
|
benchmarkRule.measureRepeated {
|
||||||
|
wrap.hasCorrectIDHash()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun checkSignature() {
|
||||||
|
val sender = KeyPair()
|
||||||
|
val receiver = KeyPair()
|
||||||
|
|
||||||
|
val senderPublicKey = CryptoUtils.pubkeyCreate(sender.privKey!!).toHexKey()
|
||||||
|
val senderMessage = createMessage(sender, receiver)
|
||||||
|
|
||||||
|
val wrap = GiftWrapEvent.create(
|
||||||
|
event = SealedGossipEvent.create(
|
||||||
|
event = senderMessage,
|
||||||
|
encryptTo = senderPublicKey,
|
||||||
|
privateKey = sender.privKey!!
|
||||||
|
),
|
||||||
|
recipientPubKey = senderPublicKey
|
||||||
|
)
|
||||||
|
|
||||||
|
benchmarkRule.measureRepeated {
|
||||||
|
wrap.hasVerifedSignature()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun decodeWrapEvent() {
|
fun decodeWrapEvent() {
|
||||||
val sender = KeyPair()
|
val sender = KeyPair()
|
||||||
|
@@ -24,6 +24,11 @@ object CryptoUtils {
|
|||||||
private val random = SecureRandom()
|
private val random = SecureRandom()
|
||||||
private val h02 = Hex.decode("02")
|
private val h02 = Hex.decode("02")
|
||||||
|
|
||||||
|
fun clearCache() {
|
||||||
|
sharedKeyCache04.evictAll()
|
||||||
|
sharedKeyCache24.evictAll()
|
||||||
|
}
|
||||||
|
|
||||||
fun randomInt(bound: Int): Int {
|
fun randomInt(bound: Int): Int {
|
||||||
return random.nextInt(bound)
|
return random.nextInt(bound)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user