Skip to content

Commit

Permalink
Add benchmarks using COSE and WebAuthn data
Browse files Browse the repository at this point in the history
  • Loading branch information
fxamacker committed Nov 5, 2019
1 parent 47055e7 commit 22732d7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ Library sizes:
* Encode and decode nil slice/map/pointer/interface values correctly.
* Encode and decode indefinite length bytes/string/array/map (["streaming"](https://tools.ietf.org/html/rfc7049#section-2.2)).
* Encode and decode time.Time as RFC 3339 formatted text string or Unix time.
* Support encoding.BinaryMarshaler and encoding.BinaryUnmarshaler interfaces.
* v1.1 -- Support `encoding.BinaryMarshaler` and `encoding.BinaryUnmarshaler` interfaces.
* v1.2 -- `cbor.RawMessage` can delay CBOR decoding or precompute CBOR encoding.
* v1.2 -- User-defined types can have custom CBOR encoding and decoding by implementing `cbor.Marshaler` and `cbor.Unmarshaler` interfaces.

## Standards

Expand All @@ -65,7 +67,8 @@ It also supports [canonical CBOR encodings](https://tools.ietf.org/html/rfc7049#
## Limitations

* CBOR tags (type 6) are ignored. Decoder simply decodes tagged data after ignoring the tags.
* CBOR negative int (type 1) that cannot fit into Go's int64 are not supported. So RFC 7049 test data -18446744073709551616 is skipped.
* CBOR negative int (type 1) that cannot fit into Go's int64 are not supported, such as RFC 7049 test data -18446744073709551616. Decoding these values returns `cbor.UnmarshalTypeError`.
* CBOR `undefined` value (0xf7) decodes to Go's `nil` value. However, Go's `nil` value encodes to CBOR `Null` value (0xf6).

## System requirements

Expand Down Expand Up @@ -100,9 +103,12 @@ type Encoder struct{ ... }
func (enc *Encoder) StartIndefiniteMap() error
func (enc *Encoder) EndIndefinite() error
type InvalidUnmarshalError struct{ ... }
type Marshaler interface{ ... }
type RawMessage []byte
type SemanticError struct{ ... }
type SyntaxError struct{ ... }
type UnmarshalTypeError struct{ ... }
type Unmarshaler interface{ ... }
type UnsupportedTypeError struct{ ... }
```

Expand Down Expand Up @@ -235,6 +241,10 @@ BenchmarkMarshal/Go_[]int_to_CBOR_array-2 | 561 ns/op | 32 B/op | 1 allocs/op
BenchmarkMarshal/Go_map[string]string_to_CBOR_map-2 | 2181 ns/op | 576 B/op | 28 allocs/op
BenchmarkMarshal/Go_struct_to_CBOR_map-2 | 780 ns/op | 64 B/op | 1 allocs/op

## Code of Conduct

This project has adopted the [Contributor Covenant Code of Conduct](CODE_OF_CONDUCT.md). Contact [[email protected]](mailto:[email protected]) with any questions or comments.

## License

Copyright (c) 2019 [Faye Amacker](https://github.com/fxamacker)
Expand Down
48 changes: 48 additions & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,51 @@ func BenchmarkEncodeStream(b *testing.B) {
}
}
}

func BenchmarkDecodeCOSE(b *testing.B) {
cborData := hexDecode("a50102032620012158205af8047e9085ef79ec321280c7b95844d707d7fe4d73cd648f044c619ee74f6b22582036bb8c00768e90858012dc3831e15a389072bbdbe7e2e19155db9e1197655edf")
for _, ctor := range []func() interface{}{
func() interface{} { return new(interface{}) },
func() interface{} { return new(map[interface{}]interface{}) },
func() interface{} { return new(map[int]interface{}) },
} {
name := "COSE to Go " + reflect.TypeOf(ctor()).Elem().String()
b.Run(name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
v := ctor()
if err := cbor.Unmarshal(cborData, v); err != nil {
b.Fatal("Unmarshal:", err)
}
}
})
}
}

func BenchmarkDecodeWebAuthn(b *testing.B) {
type attestationObject struct {
AuthnData []byte `cbor:"authData"`
Fmt string `cbor:"fmt"`
AttStmt cbor.RawMessage `cbor:"attStmt"`
}
cborData := hexDecode("a363666d74686669646f2d7532666761747453746d74a26373696758483046022100e7ab373cfbd99fcd55fd59b0f6f17fef5b77a20ddec3db7f7e4d55174e366236022100828336b4822125fb56541fb14a8a273876acd339395ec2dad95cf41c1dd2a9ae637835638159024e3082024a30820132a0030201020204124a72fe300d06092a864886f70d01010b0500302e312c302a0603550403132359756269636f2055324620526f6f742043412053657269616c203435373230303633313020170d3134303830313030303030305a180f32303530303930343030303030305a302c312a302806035504030c2159756269636f205532462045452053657269616c203234393431343937323135383059301306072a8648ce3d020106082a8648ce3d030107034200043d8b1bbd2fcbf6086e107471601468484153c1c6d3b4b68a5e855e6e40757ee22bcd8988bf3befd7cdf21cb0bf5d7a150d844afe98103c6c6607d9faae287c02a33b3039302206092b0601040182c40a020415312e332e362e312e342e312e34313438322e312e313013060b2b0601040182e51c020101040403020520300d06092a864886f70d01010b05000382010100a14f1eea0076f6b8476a10a2be72e60d0271bb465b2dfbfc7c1bd12d351989917032631d795d097fa30a26a325634e85721bc2d01a86303f6bc075e5997319e122148b0496eec8d1f4f94cf4110de626c289443d1f0f5bbb239ca13e81d1d5aa9df5af8e36126475bfc23af06283157252762ff68879bcf0ef578d55d67f951b4f32b63c8aea5b0f99c67d7d814a7ff5a6f52df83e894a3a5d9c8b82e7f8bc8daf4c80175ff8972fda79333ec465d806eacc948f1bab22045a95558a48c20226dac003d41fbc9e05ea28a6bb5e10a49de060a0a4f6a2676a34d68c4abe8c61874355b9027e828ca9e064b002d62e8d8cf0744921753d35e3c87c5d5779453e7768617574684461746158c449960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d976341000000000000000000000000000000000000000000408903fd7dfd2c9770e98cae0123b13a2c27828a106349bc6277140e7290b7e9eb7976aa3c04ed347027caf7da3a2fa76304751c02208acfc4e7fc6c7ebbc375c8a5010203262001215820ad7f7992c335b90d882b2802061b97a4fabca7e2ee3e7a51e728b8055e4eb9c7225820e0966ba7005987fece6f0e0e13447aa98cec248e4000a594b01b74c1cb1d40b3")
for _, ctor := range []func() interface{}{
func() interface{} { return new(interface{}) },
func() interface{} { return new(map[interface{}]interface{}) },
func() interface{} { return new(map[string]interface{}) },
func() interface{} { return new(attestationObject) },
} {
t := reflect.TypeOf(ctor()).Elem()
name := "atten object to Go " + t.String()
if t.Kind() == reflect.Struct {
name = "atten object to Go " + t.Kind().String()
}
b.Run(name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
v := ctor()
if err := cbor.Unmarshal(cborData, v); err != nil {
b.Fatal("Unmarshal:", err)
}
}
})
}
}

0 comments on commit 22732d7

Please sign in to comment.