Correct usages of b.SetBytes

This commit is contained in:
Andy Wang 2020-04-13 19:40:23 +01:00
parent e03080d3ba
commit 97eb6aa096
7 changed files with 38 additions and 94 deletions

View File

@ -192,16 +192,9 @@ func BenchmarkBufferedPipe_RW(b *testing.B) {
pipe.Read(smallBuf) pipe.Read(smallBuf)
} }
}() }()
b.SetBytes(int64(len(testData)))
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_, err := pipe.Write(testData) pipe.Write(testData)
if err != nil {
b.Error(
"For", "pipe write",
"got", err,
)
return
}
b.SetBytes(PAYLOAD_LEN)
} }
} }

View File

@ -80,7 +80,7 @@ func BenchmarkObfs(b *testing.B) {
testPayload, testPayload,
} }
obfsBuf := make([]byte, 2048) obfsBuf := make([]byte, defaultSendRecvBufSize)
var key [32]byte var key [32]byte
rand.Read(key[:]) rand.Read(key[:])
@ -89,14 +89,10 @@ func BenchmarkObfs(b *testing.B) {
payloadCipher, _ := cipher.NewGCM(c) payloadCipher, _ := cipher.NewGCM(c)
obfs := MakeObfs(key, payloadCipher) obfs := MakeObfs(key, payloadCipher)
b.SetBytes(int64(len(testFrame.Payload)))
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
n, err := obfs(testFrame, obfsBuf) obfs(testFrame, obfsBuf)
if err != nil {
b.Error(err)
return
}
b.SetBytes(int64(n))
} }
}) })
b.Run("AES128GCM", func(b *testing.B) { b.Run("AES128GCM", func(b *testing.B) {
@ -104,40 +100,28 @@ func BenchmarkObfs(b *testing.B) {
payloadCipher, _ := cipher.NewGCM(c) payloadCipher, _ := cipher.NewGCM(c)
obfs := MakeObfs(key, payloadCipher) obfs := MakeObfs(key, payloadCipher)
b.SetBytes(int64(len(testFrame.Payload)))
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
n, err := obfs(testFrame, obfsBuf) obfs(testFrame, obfsBuf)
if err != nil {
b.Error(err)
return
}
b.SetBytes(int64(n))
} }
}) })
b.Run("plain", func(b *testing.B) { b.Run("plain", func(b *testing.B) {
obfs := MakeObfs(key, nil) obfs := MakeObfs(key, nil)
b.SetBytes(int64(len(testFrame.Payload)))
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
n, err := obfs(testFrame, obfsBuf) obfs(testFrame, obfsBuf)
if err != nil {
b.Error(err)
return
}
b.SetBytes(int64(n))
} }
}) })
b.Run("chacha20Poly1305", func(b *testing.B) { b.Run("chacha20Poly1305", func(b *testing.B) {
payloadCipher, _ := chacha20poly1305.New(key[:16]) payloadCipher, _ := chacha20poly1305.New(key[:16])
obfs := MakeObfs(key, payloadCipher) obfs := MakeObfs(key, payloadCipher)
b.SetBytes(int64(len(testFrame.Payload)))
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
n, err := obfs(testFrame, obfsBuf) obfs(testFrame, obfsBuf)
if err != nil {
b.Error(err)
return
}
b.SetBytes(int64(n))
} }
}) })
} }
@ -152,7 +136,7 @@ func BenchmarkDeobfs(b *testing.B) {
testPayload, testPayload,
} }
obfsBuf := make([]byte, 2048) obfsBuf := make([]byte, defaultSendRecvBufSize)
var key [32]byte var key [32]byte
rand.Read(key[:]) rand.Read(key[:])
@ -164,14 +148,10 @@ func BenchmarkDeobfs(b *testing.B) {
n, _ := obfs(testFrame, obfsBuf) n, _ := obfs(testFrame, obfsBuf)
deobfs := MakeDeobfs(key, payloadCipher) deobfs := MakeDeobfs(key, payloadCipher)
b.SetBytes(int64(n))
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_, err := deobfs(obfsBuf[:n]) deobfs(obfsBuf[:n])
if err != nil {
b.Error(err)
return
}
b.SetBytes(int64(n))
} }
}) })
b.Run("AES128GCM", func(b *testing.B) { b.Run("AES128GCM", func(b *testing.B) {
@ -183,13 +163,9 @@ func BenchmarkDeobfs(b *testing.B) {
deobfs := MakeDeobfs(key, payloadCipher) deobfs := MakeDeobfs(key, payloadCipher)
b.ResetTimer() b.ResetTimer()
b.SetBytes(int64(n))
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_, err := deobfs(obfsBuf[:n]) deobfs(obfsBuf[:n])
if err != nil {
b.Error(err)
return
}
b.SetBytes(int64(n))
} }
}) })
b.Run("plain", func(b *testing.B) { b.Run("plain", func(b *testing.B) {
@ -198,13 +174,9 @@ func BenchmarkDeobfs(b *testing.B) {
deobfs := MakeDeobfs(key, nil) deobfs := MakeDeobfs(key, nil)
b.ResetTimer() b.ResetTimer()
b.SetBytes(int64(n))
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_, err := deobfs(obfsBuf[:n]) deobfs(obfsBuf[:n])
if err != nil {
b.Error(err)
return
}
b.SetBytes(int64(n))
} }
}) })
b.Run("chacha20Poly1305", func(b *testing.B) { b.Run("chacha20Poly1305", func(b *testing.B) {
@ -215,13 +187,9 @@ func BenchmarkDeobfs(b *testing.B) {
deobfs := MakeDeobfs(key, payloadCipher) deobfs := MakeDeobfs(key, payloadCipher)
b.ResetTimer() b.ResetTimer()
b.SetBytes(int64(n))
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_, err := deobfs(obfsBuf[:n]) deobfs(obfsBuf[:n])
if err != nil {
b.Error(err)
return
}
b.SetBytes(int64(n))
} }
}) })
} }

View File

@ -465,10 +465,10 @@ func BenchmarkRecvDataFromRemote_Ordered(b *testing.B) {
sesh := MakeSession(0, seshConfigOrdered) sesh := MakeSession(0, seshConfigOrdered)
n, _ := sesh.Obfs(f, obfsBuf) n, _ := sesh.Obfs(f, obfsBuf)
b.SetBytes(int64(len(f.Payload)))
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
sesh.recvDataFromRemote(obfsBuf[:n]) sesh.recvDataFromRemote(obfsBuf[:n])
b.SetBytes(int64(n))
} }
}) })
@ -478,10 +478,10 @@ func BenchmarkRecvDataFromRemote_Ordered(b *testing.B) {
sesh := MakeSession(0, seshConfigOrdered) sesh := MakeSession(0, seshConfigOrdered)
n, _ := sesh.Obfs(f, obfsBuf) n, _ := sesh.Obfs(f, obfsBuf)
b.SetBytes(int64(len(f.Payload)))
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
sesh.recvDataFromRemote(obfsBuf[:n]) sesh.recvDataFromRemote(obfsBuf[:n])
b.SetBytes(int64(n))
} }
}) })
@ -491,10 +491,10 @@ func BenchmarkRecvDataFromRemote_Ordered(b *testing.B) {
sesh := MakeSession(0, seshConfigOrdered) sesh := MakeSession(0, seshConfigOrdered)
n, _ := sesh.Obfs(f, obfsBuf) n, _ := sesh.Obfs(f, obfsBuf)
b.SetBytes(int64(len(f.Payload)))
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
sesh.recvDataFromRemote(obfsBuf[:n]) sesh.recvDataFromRemote(obfsBuf[:n])
b.SetBytes(int64(n))
} }
}) })
} }

View File

@ -5,7 +5,6 @@ import (
"github.com/cbeuw/Cloak/internal/common" "github.com/cbeuw/Cloak/internal/common"
"io" "io"
"math/rand" "math/rand"
"net"
"testing" "testing"
"time" "time"
@ -35,19 +34,14 @@ func BenchmarkStream_Write_Ordered(b *testing.B) {
rand.Read(testData) rand.Read(testData)
stream, _ := sesh.OpenStream() stream, _ := sesh.OpenStream()
b.SetBytes(payloadLen)
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_, err := stream.Write(testData) stream.Write(testData)
if err != nil {
b.Error(
"For", "stream write",
"got", err,
)
}
b.SetBytes(payloadLen)
} }
} }
/*
func BenchmarkStream_Read_Ordered(b *testing.B) { func BenchmarkStream_Read_Ordered(b *testing.B) {
var sessionKey [32]byte var sessionKey [32]byte
rand.Read(sessionKey[:]) rand.Read(sessionKey[:])
@ -88,20 +82,16 @@ func BenchmarkStream_Read_Ordered(b *testing.B) {
//time.Sleep(5*time.Second) // wait for buffer to fill up //time.Sleep(5*time.Second) // wait for buffer to fill up
readBuf := make([]byte, payloadLen) readBuf := make([]byte, payloadLen)
b.SetBytes(payloadLen)
b.ResetTimer() b.ResetTimer()
for j := 0; j < b.N; j++ { for j := 0; j < b.N; j++ {
n, err := stream.Read(readBuf) stream.Read(readBuf)
if !bytes.Equal(readBuf, testPayload) {
b.Error("paylod not equal")
}
b.SetBytes(int64(n))
if err != nil {
b.Error(err)
}
} }
} }
*/
func TestStream_Write(t *testing.T) { func TestStream_Write(t *testing.T) {
hole := connutil.Discard() hole := connutil.Discard()
var sessionKey [32]byte var sessionKey [32]byte

View File

@ -76,14 +76,10 @@ func BenchmarkSwitchboard_Send(b *testing.B) {
} }
data := make([]byte, 1000) data := make([]byte, 1000)
rand.Read(data) rand.Read(data)
b.SetBytes(int64(len(data)))
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
n, err := sesh.sb.send(data, &connId) sesh.sb.send(data, &connId)
if err != nil {
b.Error(err)
return
}
b.SetBytes(int64(n))
} }
} }

View File

@ -393,10 +393,10 @@ func BenchmarkThroughput(b *testing.B) {
clientConn, _ := pxyClientD.Dial("", "") clientConn, _ := pxyClientD.Dial("", "")
readBuf := make([]byte, bufSize) readBuf := make([]byte, bufSize)
clientConn.Write([]byte{1}) // to make server accept clientConn.Write([]byte{1}) // to make server accept
b.SetBytes(bufSize)
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
n, _ := clientConn.Read(readBuf) clientConn.Read(readBuf)
b.SetBytes(int64(n))
more <- 0 more <- 0
} }
}) })

View File

@ -1,12 +1,6 @@
package util package util
import ( /*
"io"
"io/ioutil"
"math/rand"
"testing"
)
func BenchmarkPipe(b *testing.B) { func BenchmarkPipe(b *testing.B) {
reader := rand.New(rand.NewSource(42)) reader := rand.New(rand.NewSource(42))
buf := make([]byte, 16380) buf := make([]byte, 16380)
@ -24,3 +18,6 @@ func BenchmarkPipe(b *testing.B) {
b.SetBytes(int64(n)) b.SetBytes(int64(n))
} }
} }
*/