From 8a2dc23c1fb566542ccb3cf664ee44de3ee93467 Mon Sep 17 00:00:00 2001 From: Jonathan Rudenberg Date: Thu, 3 Jul 2014 21:26:45 -0700 Subject: [PATCH] Add encrypt benchmark --- box/box_test.go | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/box/box_test.go b/box/box_test.go index a076382..5320bbb 100644 --- a/box/box_test.go +++ b/box/box_test.go @@ -14,22 +14,7 @@ type S struct{} var _ = Suite(&S{}) func (s *S) TestRoundtrip(c *C) { - recvKey, _ := Noise255.GenerateKey(rand.Reader) - sendKey, _ := Noise255.GenerateKey(rand.Reader) - - enc := &Crypter{ - Cipher: Noise255, - SenderKey: sendKey, - ReceiverKey: recvKey, - } - enc.ReceiverKey.Private = nil - - dec := &Crypter{ - Cipher: Noise255, - SenderKey: sendKey, - ReceiverKey: recvKey, - } - dec.SenderKey.Private = nil + enc, dec := newCrypters() plain := []byte("yellow submarines") padLen := 2 @@ -51,3 +36,33 @@ func (s *S) TestRoundtrip(c *C) { c.Assert(err, IsNil) c.Assert(plaintext, DeepEquals, plain) } + +func newCrypters() (*Crypter, *Crypter) { + recvKey, _ := Noise255.GenerateKey(rand.Reader) + sendKey, _ := Noise255.GenerateKey(rand.Reader) + + enc := &Crypter{ + Cipher: Noise255, + SenderKey: sendKey, + ReceiverKey: recvKey, + } + enc.ReceiverKey.Private = nil + + dec := &Crypter{ + Cipher: Noise255, + SenderKey: sendKey, + ReceiverKey: recvKey, + } + dec.SenderKey.Private = nil + + return enc, dec +} + +func BenchmarkEncrypt(b *testing.B) { + enc, _ := newCrypters() + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + enc.Encrypt(nil, nil, []byte("yellow submarine"), 0) + } +}