From 8720b00d4ff98e9f701654bf9bc408282a50bdb0 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Tue, 18 Feb 2025 10:19:51 -0500 Subject: [PATCH] Separates the random benchmark from the Sign/Verify benchmarks --- .../quartz/benchmark/RandomBenchmark.kt | 50 +++++++++++++++++++ .../quartz/benchmark/SignVerifyBenchmark.kt | 8 +-- 2 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 benchmark/src/androidTest/java/com/vitorpamplona/quartz/benchmark/RandomBenchmark.kt diff --git a/benchmark/src/androidTest/java/com/vitorpamplona/quartz/benchmark/RandomBenchmark.kt b/benchmark/src/androidTest/java/com/vitorpamplona/quartz/benchmark/RandomBenchmark.kt new file mode 100644 index 000000000..f230500e4 --- /dev/null +++ b/benchmark/src/androidTest/java/com/vitorpamplona/quartz/benchmark/RandomBenchmark.kt @@ -0,0 +1,50 @@ +/** + * 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.utils.RandomInstance +import junit.framework.TestCase.assertNotNull +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class RandomBenchmark { + @get:Rule val benchmarkRule = BenchmarkRule() + + @Test + fun random32Bytes() { + benchmarkRule.measureRepeated { assertNotNull(RandomInstance.bytes(32)) } + } + + @Test + fun random1000Bytes() { + benchmarkRule.measureRepeated { assertNotNull(RandomInstance.bytes(1000)) } + } + + @Test + fun randomInt() { + benchmarkRule.measureRepeated { assertNotNull(RandomInstance.int(1000)) } + } +} diff --git a/benchmark/src/androidTest/java/com/vitorpamplona/quartz/benchmark/SignVerifyBenchmark.kt b/benchmark/src/androidTest/java/com/vitorpamplona/quartz/benchmark/SignVerifyBenchmark.kt index 29043a4d1..288995e94 100644 --- a/benchmark/src/androidTest/java/com/vitorpamplona/quartz/benchmark/SignVerifyBenchmark.kt +++ b/benchmark/src/androidTest/java/com/vitorpamplona/quartz/benchmark/SignVerifyBenchmark.kt @@ -28,6 +28,7 @@ import com.vitorpamplona.quartz.nip01Core.crypto.Nip01 import com.vitorpamplona.quartz.utils.RandomInstance import com.vitorpamplona.quartz.utils.sha256.sha256 import junit.framework.TestCase.assertNotNull +import junit.framework.TestCase.assertTrue import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @@ -36,11 +37,6 @@ import org.junit.runner.RunWith class SignVerifyBenchmark { @get:Rule val benchmarkRule = BenchmarkRule() - @Test - fun random() { - benchmarkRule.measureRepeated { assertNotNull(RandomInstance.bytes(1000)) } - } - @Test fun sign() { val keyPair = KeyPair() @@ -56,7 +52,7 @@ class SignVerifyBenchmark { val signature = Nip01.sign(msg, keyPair.privKey!!) benchmarkRule.measureRepeated { - assertNotNull(Nip01.verify(signature, msg, keyPair.pubKey)) + assertTrue(Nip01.verify(signature, msg, keyPair.pubKey)) } } }