Skip to content

Commit

Permalink
Merge pull request #54 from qmuntal/bench-sign1
Browse files Browse the repository at this point in the history
add Sign1Message benchmarks
  • Loading branch information
shizhMSFT committed Apr 22, 2022
2 parents 52f40e3 + 9b59e6b commit 08be6c0
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package cose_test

import (
"io"
"testing"

"github.com/veraison/go-cose"
)

func newSign1Message() *cose.Sign1Message {
return &cose.Sign1Message{
Headers: cose.Headers{
Protected: cose.ProtectedHeader{
cose.HeaderLabelAlgorithm: cose.AlgorithmES256,
},
Unprotected: cose.UnprotectedHeader{
cose.HeaderLabelKeyID: 1,
},
},
Payload: make([]byte, 100),
Signature: make([]byte, 32),
}
}

type noSigner struct{}

func (noSigner) Algorithm() cose.Algorithm {
return cose.AlgorithmES256
}

func (noSigner) Sign(_ io.Reader, digest []byte) ([]byte, error) {
return digest, nil
}

func (noSigner) Verify(_, _ []byte) error {
return nil
}

func BenchmarkSign1Message_MarshalCBOR(b *testing.B) {
msg := newSign1Message()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := msg.MarshalCBOR()
if err != nil {
b.Fatal(err)
}
}
}

func BenchmarkSign1Message_UnmarshalCBOR(b *testing.B) {
data, err := newSign1Message().MarshalCBOR()
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var m cose.Sign1Message
err = m.UnmarshalCBOR(data)
if err != nil {
b.Fatal(err)
}
}
}

func BenchmarkSign1Message_Sign(b *testing.B) {
msg := newSign1Message()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
msg.Signature = nil
err := msg.Sign(zeroSource{}, nil, noSigner{})
if err != nil {
b.Fatal(err)
}
}
}

func BenchmarkSign1Message_Verify(b *testing.B) {
msg := newSign1Message()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := msg.Verify(nil, noSigner{})
if err != nil {
b.Fatal(err)
}
}
}

0 comments on commit 08be6c0

Please sign in to comment.