Moves Sha256 to a pool of digests to double the hashing performance
This commit is contained in:
+22
-1
@@ -26,6 +26,8 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import com.vitorpamplona.quartz.EventFactory
|
||||
import com.vitorpamplona.quartz.nip01Core.jackson.EventMapper
|
||||
import com.vitorpamplona.quartz.nip01Core.verify
|
||||
import com.vitorpamplona.quartz.nip01Core.verifyId
|
||||
import com.vitorpamplona.quartz.nip01Core.verifySignature
|
||||
import com.vitorpamplona.quartz.utils.TimeUtils
|
||||
import junit.framework.TestCase.assertTrue
|
||||
import org.junit.Rule
|
||||
@@ -42,6 +44,15 @@ import org.junit.runner.RunWith
|
||||
class EventBenchmark {
|
||||
@get:Rule val benchmarkRule = BenchmarkRule()
|
||||
|
||||
@Test
|
||||
fun parseComplete() {
|
||||
benchmarkRule.measureRepeated {
|
||||
val tree = EventMapper.mapper.readTree(reqResponseEvent)
|
||||
val event = EventMapper.fromJson(tree[2])
|
||||
assertTrue(event.verify())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseREQString() {
|
||||
benchmarkRule.measureRepeated { EventMapper.mapper.readTree(reqResponseEvent) }
|
||||
@@ -54,13 +65,23 @@ class EventBenchmark {
|
||||
benchmarkRule.measureRepeated { EventMapper.fromJson(msg[2]) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun checkId() {
|
||||
val msg = EventMapper.mapper.readTree(reqResponseEvent)
|
||||
val event = EventMapper.fromJson(msg[2])
|
||||
benchmarkRule.measureRepeated {
|
||||
// Should pass
|
||||
assertTrue(event.verifyId())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun checkSignature() {
|
||||
val msg = EventMapper.mapper.readTree(reqResponseEvent)
|
||||
val event = EventMapper.fromJson(msg[2])
|
||||
benchmarkRule.measureRepeated {
|
||||
// Should pass
|
||||
assertTrue(event.verify())
|
||||
assertTrue(event.verifySignature())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+27
-2
@@ -25,11 +25,12 @@ import androidx.benchmark.junit4.measureRepeated
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import com.vitorpamplona.quartz.nip01Core.EventHasher
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import com.vitorpamplona.quartz.utils.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
import junit.framework.TestCase.assertNotNull
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import java.security.MessageDigest
|
||||
|
||||
/**
|
||||
* Benchmark, which will execute on an Android device.
|
||||
@@ -43,7 +44,7 @@ class Sha256Benchmark {
|
||||
val benchmarkRule = BenchmarkRule()
|
||||
|
||||
@Test
|
||||
fun sha256() {
|
||||
fun sha256Pool() {
|
||||
val event = Event.fromJson(largeKind1Event)
|
||||
val byteArray = EventHasher.makeJsonForId(event.pubKey, event.createdAt, event.kind, event.tags, event.content).toByteArray()
|
||||
|
||||
@@ -52,4 +53,28 @@ class Sha256Benchmark {
|
||||
assertNotNull(sha256(byteArray))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sha256NewEachTime() {
|
||||
val event = Event.fromJson(largeKind1Event)
|
||||
val byteArray = EventHasher.makeJsonForId(event.pubKey, event.createdAt, event.kind, event.tags, event.content).toByteArray()
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
val digest = MessageDigest.getInstance("SHA-256")
|
||||
assertNotNull(digest.digest(byteArray))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sha256Reuse() {
|
||||
val event = Event.fromJson(largeKind1Event)
|
||||
val byteArray = EventHasher.makeJsonForId(event.pubKey, event.createdAt, event.kind, event.tags, event.content).toByteArray()
|
||||
|
||||
val digest = MessageDigest.getInstance("SHA-256")
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
assertNotNull(digest.digest(byteArray))
|
||||
digest.reset()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair
|
||||
import com.vitorpamplona.quartz.nip01Core.crypto.Nip01
|
||||
import com.vitorpamplona.quartz.utils.RandomInstance
|
||||
import com.vitorpamplona.quartz.utils.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
import junit.framework.TestCase.assertNotNull
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
|
||||
Reference in New Issue
Block a user