Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Sign1Message benchmarks #54

Merged
merged 3 commits into from
Apr 22, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
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 zeroReader struct{}

func (zeroReader) Read(b []byte) (int, error) {
for i := range b {
b[i] = 0
}
return len(b), nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this a dup of zeroSource in conformance_test.go?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!


type noSigner struct{}

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

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

type noVerifier struct{}

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

func (noVerifier) Verify(_, _ []byte) error {
return nil
}
qmuntal marked this conversation as resolved.
Show resolved Hide resolved

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(zeroReader{}, 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, noVerifier{})
if err != nil {
b.Fatal(err)
}
}
}