Skip to content

Commit d74cf7c

Browse files
committed
speed: add per-blocksize GoGCM benchmarks
Only visible when you run "go test -bench" like this: $ cd gocryptfs/internal/speed $ go test -bench . goos: linux goarch: amd64 pkg: github.com/rfjakob/gocryptfs/v2/internal/speed cpu: Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz BenchmarkStupidGCM-4 202352 5937 ns/op 689.96 MB/s BenchmarkStupidGCMDecrypt-4 206023 5782 ns/op 708.38 MB/s BenchmarkGoGCM-4 291878 4098 ns/op 999.45 MB/s BenchmarkGoGCMBlockSize/1024-4 1000000 1151 ns/op 889.88 MB/s BenchmarkGoGCMBlockSize/2048-4 561182 2134 ns/op 959.60 MB/s BenchmarkGoGCMBlockSize/4096-4 292057 4101 ns/op 998.87 MB/s BenchmarkGoGCMBlockSize/8192-4 149216 8031 ns/op 1020.09 MB/s BenchmarkGoGCMBlockSize/16384-4 75361 15917 ns/op 1029.34 MB/s BenchmarkGoGCMBlockSize/32768-4 37916 31649 ns/op 1035.35 MB/s BenchmarkGoGCMBlockSize/65536-4 19005 63117 ns/op 1038.33 MB/s BenchmarkGoGCMBlockSize/131072-4 9498 126166 ns/op 1038.89 MB/s BenchmarkGoGCMBlockSize/262144-4 4755 252149 ns/op 1039.64 MB/s BenchmarkGoGCMBlockSize/524288-4 2377 504108 ns/op 1040.03 MB/s BenchmarkGoGCMBlockSize/1048576-4 1188 1008675 ns/op 1039.56 MB/s BenchmarkGoGCMDecrypt-4 294664 4059 ns/op 1009.02 MB/s BenchmarkAESSIV-4 46498 25432 ns/op 161.05 MB/s BenchmarkAESSIVDecrypt-4 46908 25509 ns/op 160.57 MB/s BenchmarkXchacha-4 244473 4894 ns/op 836.97 MB/s BenchmarkXchachaDecrypt-4 249710 4798 ns/op 853.75 MB/s BenchmarkStupidXchacha-4 166988 7101 ns/op 576.79 MB/s BenchmarkStupidXchachaDecrypt-4 163093 7240 ns/op 565.72 MB/s BenchmarkStupidChacha-4 184172 6527 ns/op 627.58 MB/s BenchmarkStupidChachaDecrypt-4 179796 6659 ns/op 615.11 MB/s PASS ok github.com/rfjakob/gocryptfs/v2/internal/speed 30.068s
1 parent 77a0410 commit d74cf7c

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

internal/speed/speed.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
const adLen = 24
2424

2525
// gocryptfs uses fixed-size 4 kiB blocks
26-
const blockSize = 4096
26+
const gocryptfsBlockSize = 4096
2727

2828
// Run - run the speed the test and print the results.
2929
func Run() {
@@ -83,6 +83,11 @@ func randBytes(n int) []byte {
8383

8484
// bEncrypt benchmarks the encryption speed of cipher "c"
8585
func bEncrypt(b *testing.B, c cipher.AEAD) {
86+
bEncryptBlockSize(b, c, gocryptfsBlockSize)
87+
}
88+
89+
// bEncryptBlockSize benchmarks the encryption speed of cipher "c" at block size "blockSize"
90+
func bEncryptBlockSize(b *testing.B, c cipher.AEAD, blockSize int) {
8691
authData := randBytes(adLen)
8792
iv := randBytes(c.NonceSize())
8893
in := make([]byte, blockSize)
@@ -97,13 +102,12 @@ func bEncrypt(b *testing.B, c cipher.AEAD) {
97102
// Encrypt and append to nonce
98103
c.Seal(dst, iv, in, authData)
99104
}
100-
101105
}
102106

103107
func bDecrypt(b *testing.B, c cipher.AEAD) {
104108
authData := randBytes(adLen)
105109
iv := randBytes(c.NonceSize())
106-
plain := randBytes(blockSize)
110+
plain := randBytes(gocryptfsBlockSize)
107111
ciphertext := c.Seal(iv, iv, plain, authData)
108112

109113
b.SetBytes(int64(len(plain)))
@@ -129,6 +133,10 @@ func bStupidGCM(b *testing.B) {
129133

130134
// bGoGCM benchmarks Go stdlib GCM
131135
func bGoGCM(b *testing.B) {
136+
bGoGCMBlockSize(b, gocryptfsBlockSize)
137+
}
138+
139+
func bGoGCMBlockSize(b *testing.B, blockSize int) {
132140
gAES, err := aes.NewCipher(randBytes(32))
133141
if err != nil {
134142
b.Fatal(err)
@@ -137,7 +145,7 @@ func bGoGCM(b *testing.B) {
137145
if err != nil {
138146
b.Fatal(err)
139147
}
140-
bEncrypt(b, gGCM)
148+
bEncryptBlockSize(b, gGCM, blockSize)
141149
}
142150

143151
// bAESSIV benchmarks AES-SIV from github.com/aperturerobotics/jacobsa-crypto/siv

internal/speed/speed_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package speed
33
import (
44
"crypto/aes"
55
"crypto/cipher"
6+
"fmt"
67
"testing"
78

89
"golang.org/x/crypto/chacha20poly1305"
@@ -38,6 +39,13 @@ func BenchmarkGoGCM(b *testing.B) {
3839
bGoGCM(b)
3940
}
4041

42+
func BenchmarkGoGCMBlockSize(b *testing.B) {
43+
for blockSize := 1024; blockSize <= 1024*1024; blockSize *= 2 {
44+
name := fmt.Sprintf("%d", blockSize)
45+
b.Run(name, func(b *testing.B) { bGoGCMBlockSize(b, blockSize) })
46+
}
47+
}
48+
4149
func BenchmarkGoGCMDecrypt(b *testing.B) {
4250
gAES, err := aes.NewCipher(randBytes(32))
4351
if err != nil {

0 commit comments

Comments
 (0)