jwt: Slow Sign
No benchmarks so I did a small comparison.
goos: darwin
goarch: amd64
pkg: github.com/pascaldekloe/jwt
BenchmarkCristalHQ/sign-HS256-4 543394 2212 ns/op
BenchmarkCristalHQ/check-HS256-4 271428 4300 ns/op
BenchmarkHMAC/sign-HS256-4 589506 2034 ns/op 123 B/token
BenchmarkHMAC/check-HS256-4 274357 4277 ns/op
PASS
ok github.com/pascaldekloe/jwt 5.242s
func BenchmarkCristalHQ(b *testing.B) {
// 512-bit key
secret := make([]byte, 64)
signer, err := cjwt.NewHS256(secret)
if err != nil {
b.Fatal(err)
}
builder := cjwt.NewTokenBuilder(signer)
claims := &cjwt.StandardClaims{
Issuer: benchClaims.Issuer,
IssuedAt: cjwt.Timestamp(*benchClaims.Issued),
}
var token []byte
b.Run("sign-HS256", func(b *testing.B) {
for i := 0; i < b.N; i++ {
obj, err := builder.Build(claims)
if err != nil {
b.Fatal(err)
}
token = obj.Raw()
}
})
b.Run("check-HS256", func(b *testing.B) {
for i := 0; i < b.N; i++ {
obj, err := cjwt.ParseAndVerify(token, signer)
if err != nil {
b.Fatal(err)
}
err = json.Unmarshal(obj.RawClaims(), new(map[string]interface{}))
if err != nil {
b.Fatal(err)
}
}
})
}
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 1
- Comments: 18 (9 by maintainers)
Just run code from https://github.com/cristaloleg/benches/tree/master/jwt and I can confirm that cristalhq/jwt/v3 performs a bit better on my machine (MacBook Pro (13-inch, Late 2016)):
Similar results on 40$ per month Linux droplet on Digitalocean (
Linux ubuntu-s-4vcpu-8gb-ams3-01 4.15.0-66-generic #75-Ubuntu SMP Tue Oct 1 05:24:09 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
, go 1.14.6):Nice. Please share a benchmark code/repo, your OS and environment, machine setup and Go version.
Please provide where it’s slow first.
I suggest using the benchstat when doing benchmarks. This way, you can share benchmark results in a more intuitive way. Plus it tries to determine whether results are stable or it’s a noise.
Yes that is correct. A 10 % slowdown in comparison.
Hi @pascaldekloe thanks for the benchs, I haven’t added them to the repo yet. I’ve a question, am I get it right: 2212 ns/op vs 2034 ns/op ?