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)

Most upvoted comments

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)):

$ time ./jwt-bench -test.v -test.benchmem -test.bench HMAC/sign-HS256 -test.count 5 -test.run ^$
goos: darwin
goarch: amd64
pkg: github.com/cristaloleg/benches/jwt
Benchmark_One_HMAC
Benchmark_One_HMAC/sign-HS256
Benchmark_One_HMAC/sign-HS256-4         	  481699	      2447 ns/op	       123 B/token	     656 B/op	       7 allocs/op
Benchmark_One_HMAC/sign-HS256-4         	  504902	      2813 ns/op	       123 B/token	     656 B/op	       7 allocs/op
Benchmark_One_HMAC/sign-HS256-4         	  498763	      2438 ns/op	       123 B/token	     656 B/op	       7 allocs/op
Benchmark_One_HMAC/sign-HS256-4         	  514028	      2435 ns/op	       123 B/token	     656 B/op	       7 allocs/op
Benchmark_One_HMAC/sign-HS256-4         	  491911	      2426 ns/op	       123 B/token	     656 B/op	       7 allocs/op
Benchmark_Two_HMAC
Benchmark_Two_HMAC/sign-HS256
Benchmark_Two_HMAC/sign-HS256-4         	  529950	      2314 ns/op	       129 B/token	     400 B/op	       6 allocs/op
Benchmark_Two_HMAC/sign-HS256-4         	  525048	      2327 ns/op	       129 B/token	     400 B/op	       6 allocs/op
Benchmark_Two_HMAC/sign-HS256-4         	  532951	      2320 ns/op	       129 B/token	     400 B/op	       6 allocs/op
Benchmark_Two_HMAC/sign-HS256-4         	  517843	      2301 ns/op	       129 B/token	     400 B/op	       6 allocs/op
Benchmark_Two_HMAC/sign-HS256-4         	  517129	      2315 ns/op	       129 B/token	     400 B/op	       6 allocs/op
PASS

real	0m13.551s
user	0m13.992s
sys	0m0.363s

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):

root@ubuntu-s-4vcpu-8gb-ams3-01:~/benches/jwt# time ./jwt-bench -test.v -test.benchmem -test.bench HMAC/sign-HS256 -test.count 5 -test.run ^$
goos: linux
goarch: amd64
pkg: github.com/cristaloleg/benches/jwt
Benchmark_One_HMAC
Benchmark_One_HMAC/sign-HS256
Benchmark_One_HMAC/sign-HS256-4         	  486711	      2623 ns/op	       123 B/token	     656 B/op	       7 allocs/op
Benchmark_One_HMAC/sign-HS256-4         	  452798	      2553 ns/op	       123 B/token	     656 B/op	       7 allocs/op
Benchmark_One_HMAC/sign-HS256-4         	  491455	      2544 ns/op	       123 B/token	     656 B/op	       7 allocs/op
Benchmark_One_HMAC/sign-HS256-4         	  501620	      2541 ns/op	       123 B/token	     656 B/op	       7 allocs/op
Benchmark_One_HMAC/sign-HS256-4         	  482263	      2590 ns/op	       123 B/token	     656 B/op	       7 allocs/op
Benchmark_Two_HMAC
Benchmark_Two_HMAC/sign-HS256
Benchmark_Two_HMAC/sign-HS256-4         	  517096	      2429 ns/op	       129 B/token	     400 B/op	       6 allocs/op
Benchmark_Two_HMAC/sign-HS256-4         	  499501	      2372 ns/op	       129 B/token	     400 B/op	       6 allocs/op
Benchmark_Two_HMAC/sign-HS256-4         	  513686	      2456 ns/op	       129 B/token	     400 B/op	       6 allocs/op
Benchmark_Two_HMAC/sign-HS256-4         	  514288	      2589 ns/op	       129 B/token	     400 B/op	       6 allocs/op
Benchmark_Two_HMAC/sign-HS256-4         	  512469	      2429 ns/op	       129 B/token	     400 B/op	       6 allocs/op
PASS

real	0m12.796s
user	0m13.277s
sys	0m0.290s

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 ?