{"action":"created","issue":{"url":"https://api.github.com/repos/dgrijalva/jwt-go/issues/61","labels_url":"https://api.github.com/repos/dgrijalva/jwt-go/issues/61/labels{/name}","comments_url":"https://api.github.com/repos/dgrijalva/jwt-go/issues/61/comments","events_url":"https://api.github.com/repos/dgrijalva/jwt-go/issues/61/events","html_url":"https://github.com/dgrijalva/jwt-go/issues/61","id":67725199,"number":61,"title":"Running several SignedString(privateKey) in goroutines is almost the same as running in serial.","user":{"login":"wari","id":525422,"avatar_url":"https://avatars.githubusercontent.com/u/525422?v=3","gravatar_id":"","url":"https://api.github.com/users/wari","html_url":"https://github.com/wari","followers_url":"https://api.github.com/users/wari/followers","following_url":"https://api.github.com/users/wari/following{/other_user}","gists_url":"https://api.github.com/users/wari/gists{/gist_id}","starred_url":"https://api.github.com/users/wari/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wari/subscriptions","organizations_url":"https://api.github.com/users/wari/orgs","repos_url":"https://api.github.com/users/wari/repos","events_url":"https://api.github.com/users/wari/events{/privacy}","received_events_url":"https://api.github.com/users/wari/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-04-11T03:05:07Z","updated_at":"2015-04-11T20:53:23Z","closed_at":null,"body":"Hi there, just a note, not sure if this would be a bug, or just a normal behaviour that's to be expected with signing tokens via RSxxx.\r\n\r\nOn my system, where I need to sign one or many token at once. When I sign a token, it may take roughly 200-250ms for `jwt.SignedString()`. Of course, depending on what machine I use, it may be faster or slower due to CPU.\r\n\r\nBut when I tried signing 3 different tokens at the same time, using the same privateKey, the results are almost 3x longer.\r\n\r\nThe source for my implementation in psuedocode-ish looks like this:\r\n\r\n``` go\r\nfor item := range stuff {\r\n  i := doSomethingWith(item)\r\n  go func() {\r\n    doBasicClaims()\r\n    AddPayloadClaim(i)\r\n    s := jwt.SignedString(privateKey)\r\n    sendOffToken(s)\r\n    getBackData()\r\n  }\r\n}\r\n```\r\n\r\nSo for this, all the other stuff took only a few ms, but the goroutines will take a total of 600 to 900 ms on a request that produces 3 iterations.\r\n\r\nHere's what the verbose log looks like\r\n\r\n```\r\nStarted POST /settings for localhost:10000\r\nProcessing all stuff 915.559µs\r\nIterating through 3 settings request 933.57µs\r\nFinished processing all stuff, and waiting for return 954.803µs (for range:)\r\nProcessing AAAA 989.179µs\r\nBefore token signing with RSA256 1.175348ms\r\nProcessing BBBB 11.132295ms\r\nBefore token signing with RSA256 11.341492ms\r\nProcessing CCCC 22.01571ms\r\nBefore token signing with RSA256 22.350366ms\r\nToken signing for BBBB took 613.113249ms current time @ 624.455326ms\r\nSent Request for BBBB 624.532368ms\r\nToken signing for AAAA took 658.879584ms current time @ 660.055904ms\r\nSent Request for AAAA 660.175747ms\r\nToken signing for CCCC took 656.992226ms current time @ 679.343004ms\r\nSent Request for CCCC 679.402761ms\r\nGot back reply from AAAA 684.077767ms travel time took 23.902779ms\r\nFinished Processing AAAA 684.188976ms\r\nGot back reply from CCCC 686.740498ms travel time took 7.338188ms\r\nFinished Processing CCCC 686.815321ms\r\nGot back reply from BBBB 767.306613ms travel time took 142.774982ms\r\nFinished Processing BBBB 767.355212ms\r\nCompleted 200 OK in 767.547319ms: served 1511 bytes\r\n```\r\n\r\nSo, the question is, is this expected behaviour that I'm observing? I would be afraid to do this on hundreds of simultaneous token creations with RSA.\r\n\r\nJust a note, the `getBackData()` parts are actually tokens signed with HS256 over a network, and the slowest one that took 142ms (BBBB), is actually from a Raspberry Pi. Which means, it took less time to verify an RSA signature, and generate a SignedString() with HS256 than to actually sign with RSA on a faster machine."},"comment":{"url":"https://api.github.com/repos/dgrijalva/jwt-go/issues/comments/91924321","html_url":"https://github.com/dgrijalva/jwt-go/issues/61#issuecomment-91924321","issue_url":"https://api.github.com/repos/dgrijalva/jwt-go/issues/61","id":91924321,"user":{"login":"dgrijalva","id":23713,"avatar_url":"https://avatars.githubusercontent.com/u/23713?v=3","gravatar_id":"","url":"https://api.github.com/users/dgrijalva","html_url":"https://github.com/dgrijalva","followers_url":"https://api.github.com/users/dgrijalva/followers","following_url":"https://api.github.com/users/dgrijalva/following{/other_user}","gists_url":"https://api.github.com/users/dgrijalva/gists{/gist_id}","starred_url":"https://api.github.com/users/dgrijalva/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dgrijalva/subscriptions","organizations_url":"https://api.github.com/users/dgrijalva/orgs","repos_url":"https://api.github.com/users/dgrijalva/repos","events_url":"https://api.github.com/users/dgrijalva/events{/privacy}","received_events_url":"https://api.github.com/users/dgrijalva/received_events","type":"User","site_admin":false},"created_at":"2015-04-11T20:53:23Z","updated_at":"2015-04-11T20:53:23Z","body":"```\r\nArtax:jwt-go dgrijalva$ go test -bench . -cpu 1 -benchtime 5s\r\nPASS\r\nBenchmarkHS256Signing\t 1000000\t      6329 ns/op\r\nBenchmarkHS384Signing\t 1000000\t      7121 ns/op\r\nBenchmarkHS512Signing\t 1000000\t      7725 ns/op\r\nBenchmarkRS256Signing\t 5000000\t      3339 ns/op\r\nBenchmarkRS384Signing\t 5000000\t      3331 ns/op\r\nBenchmarkRS512Signing\t 5000000\t      3327 ns/op\r\nok  \tgithub.com/dgrijalva/jwt-go\t82.747s\r\nArtax:jwt-go dgrijalva$ go test -bench . -cpu 2 -benchtime 5s\r\nPASS\r\nBenchmarkHS256Signing-2\t 5000000\t      3692 ns/op\r\nBenchmarkHS384Signing-2\t 2000000\t      4287 ns/op\r\nBenchmarkHS512Signing-2\t 2000000\t      4494 ns/op\r\nBenchmarkRS256Signing-2\t 5000000\t      2019 ns/op\r\nBenchmarkRS384Signing-2\t 5000000\t      1975 ns/op\r\nBenchmarkRS512Signing-2\t 5000000\t      1967 ns/op\r\nok  \tgithub.com/dgrijalva/jwt-go\t85.383s\r\nArtax:jwt-go dgrijalva$ go test -bench . -cpu 4 -benchtime 5s\r\nPASS\r\nBenchmarkHS256Signing-4\t 5000000\t      2716 ns/op\r\nBenchmarkHS384Signing-4\t 5000000\t      3127 ns/op\r\nBenchmarkHS512Signing-4\t 5000000\t      3150 ns/op\r\nBenchmarkRS256Signing-4\t10000000\t      1390 ns/op\r\nBenchmarkRS384Signing-4\t10000000\t      1399 ns/op\r\nBenchmarkRS512Signing-4\t10000000\t      1420 ns/op\r\nok  \tgithub.com/dgrijalva/jwt-go\t101.527s\r\n```"}}