Skip to content

Commit

Permalink
ed25519consensus: improve batch benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
FiloSottile committed Jan 18, 2024
1 parent d8a485d commit d877647
Showing 1 changed file with 26 additions and 48 deletions.
74 changes: 26 additions & 48 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,74 +61,52 @@ func TestEmptyBatchFails(t *testing.T) {
}
}

func BenchmarkVerifyBatch(b *testing.B) {
for _, n := range []int{1, 8, 64, 1024} {
b.Run(fmt.Sprint(n), func(b *testing.B) {
b.ReportAllocs()
v := NewBatchVerifier()
for i := 0; i < n; i++ {
pub, priv, _ := ed25519.GenerateKey(nil)
msg := []byte("BatchVerifyTest")
v.Add(pub, msg, ed25519.Sign(priv, msg))
}
// NOTE: dividing by n so that metrics are per-signature
for i := 0; i < b.N/n; i++ {
if !v.Verify() {
b.Fatal("signature set failed batch verification")
}
}
})
}
}

func BenchmarkCreateBatch(b *testing.B) {
func BenchmarkBatch(b *testing.B) {
for _, n := range []int{1, 8, 64, 1024, 4096, 16384} {
b.Run(fmt.Sprint(n), func(b *testing.B) {
b.StopTimer()
var (
msg = []byte("CreateBatch")
pubs = make([][]byte, n)
sigs = make([][]byte, n)
)
for i := 0; i < n; i++ {
pub, priv, _ := ed25519.GenerateKey(nil)
pubs[i] = pub
sigs[i] = ed25519.Sign(priv, msg)
}
b.StartTimer()
var msg = []byte("ed25519consensus")
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
v := NewBatchVerifier()
for j := 0; j < n; j++ {
v.Add(pubs[j], msg, sigs[j])
b.StopTimer()
pub, priv, _ := ed25519.GenerateKey(nil)
sig := ed25519.Sign(priv, msg)
b.StartTimer()
v.Add(pub, msg, sig)
}
if !v.Verify() {
b.Fail()
}
}
// Divide by n to get per-signature values.
b.ReportMetric(float64(b.Elapsed().Nanoseconds())/float64(b.N)/float64(n), "ns/sig")
})
}
}

func BenchmarkCreatePreallocatedBatch(b *testing.B) {
func BenchmarkPreallocatedBatch(b *testing.B) {
for _, n := range []int{1, 8, 64, 1024, 4096, 16384} {
b.Run(fmt.Sprint(n), func(b *testing.B) {
b.StopTimer()
var (
msg = []byte("CreatePreallocatedBatch")
pubs = make([][]byte, n)
sigs = make([][]byte, n)
)
for i := 0; i < n; i++ {
pub, priv, _ := ed25519.GenerateKey(nil)
pubs[i] = pub
sigs[i] = ed25519.Sign(priv, msg)
}
b.StartTimer()
var msg = []byte("ed25519consensus")
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
v := NewPreallocatedBatchVerifier(n)
for j := 0; j < n; j++ {
v.Add(pubs[j], msg, sigs[j])
b.StopTimer()
pub, priv, _ := ed25519.GenerateKey(nil)
sig := ed25519.Sign(priv, msg)
b.StartTimer()
v.Add(pub, msg, sig)
}
if !v.Verify() {
b.Fail()
}
}
// Divide by n to get per-signature values.
b.ReportMetric(float64(b.Elapsed().Nanoseconds())/float64(b.N)/float64(n), "ns/sig")
})
}
}
Expand Down

0 comments on commit d877647

Please sign in to comment.