Last active
April 12, 2016 23:35
-
-
Save cmars/01371c4ccdc70620e17457580c0d76da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bench | |
import ( | |
"crypto/tls" | |
"crypto/x509" | |
"testing" | |
"time" | |
"github.com/juju/juju/cert" | |
gc "gopkg.in/check.v1" | |
) | |
func Test(t *testing.T) { gc.TestingT(t) } | |
type suite struct { | |
caCert tls.Certificate | |
serverCert tls.Certificate | |
certPool *x509.CertPool | |
} | |
var _ = gc.Suite(&suite{}) | |
func (s *suite) BenchmarkRSA1024(c *gc.C) { | |
s.runBench(c, 1024) | |
} | |
func (s *suite) BenchmarkRSA2048(c *gc.C) { | |
s.runBench(c, 2048) | |
} | |
func (s *suite) BenchmarkRSA4096(c *gc.C) { | |
s.runBench(c, 4096) | |
} | |
func (s *suite) runBench(c *gc.C, bits int) { | |
caCert, caKey, err := cert.NewCA("bobs", "bits", time.Now().Add(10*365*86400*time.Second)) | |
c.Assert(err, gc.IsNil) | |
serverCert, serverKey, err := cert.NewDefaultServer(caCert, caKey, []string{"localhost", "127.0.0.1"}) | |
c.Assert(err, gc.IsNil) | |
s.caCert, err = tls.X509KeyPair([]byte(caCert), []byte(caKey)) | |
c.Assert(err, gc.IsNil) | |
s.serverCert, err = tls.X509KeyPair([]byte(serverCert), []byte(serverKey)) | |
c.Assert(err, gc.IsNil) | |
caCertAsn1, err := x509.ParseCertificate(s.caCert.Certificate[0]) | |
c.Assert(err, gc.IsNil) | |
s.certPool = x509.NewCertPool() | |
s.certPool.AddCert(caCertAsn1) | |
cert.KeyBits = bits | |
l, err := tls.Listen("tcp", "localhost:0", &tls.Config{ | |
Certificates: []tls.Certificate{ | |
s.serverCert, | |
s.caCert, | |
}}) | |
defer l.Close() | |
go func() { | |
for { | |
conn, err := l.Accept() | |
if err != nil { | |
return | |
} | |
conn.Read(nil) | |
conn.Close() | |
} | |
}() | |
c.Assert(err, gc.IsNil) | |
clientConf := &tls.Config{ | |
RootCAs: s.certPool, | |
} | |
for i := 0; i < c.N; i++ { | |
conn, err := tls.Dial("tcp", l.Addr().String(), clientConf) | |
c.Assert(err, gc.IsNil) | |
err = conn.Close() | |
c.Assert(err, gc.IsNil) | |
} | |
} |
Author
cmars
commented
Apr 12, 2016
Actually, I had a mistake in my benchmark... These are the right numbers:
PASS: bench_test.go:23: suite.BenchmarkRSA1024 500 5917138 ns/op
PASS: bench_test.go:27: suite.BenchmarkRSA2048 50 26014933 ns/op
PASS: bench_test.go:31: suite.BenchmarkRSA4096 5 1882141855 ns/op
OK: 3 passed
PASS
ok rsa-bench 14.781s
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment