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
This commit is contained in:
Jakob Unterwurzacher 2023-03-08 16:54:56 +01:00
parent 77a0410e2e
commit d74cf7c723
2 changed files with 20 additions and 4 deletions

View File

@ -23,7 +23,7 @@ import (
const adLen = 24 const adLen = 24
// gocryptfs uses fixed-size 4 kiB blocks // gocryptfs uses fixed-size 4 kiB blocks
const blockSize = 4096 const gocryptfsBlockSize = 4096
// Run - run the speed the test and print the results. // Run - run the speed the test and print the results.
func Run() { func Run() {
@ -83,6 +83,11 @@ func randBytes(n int) []byte {
// bEncrypt benchmarks the encryption speed of cipher "c" // bEncrypt benchmarks the encryption speed of cipher "c"
func bEncrypt(b *testing.B, c cipher.AEAD) { func bEncrypt(b *testing.B, c cipher.AEAD) {
bEncryptBlockSize(b, c, gocryptfsBlockSize)
}
// bEncryptBlockSize benchmarks the encryption speed of cipher "c" at block size "blockSize"
func bEncryptBlockSize(b *testing.B, c cipher.AEAD, blockSize int) {
authData := randBytes(adLen) authData := randBytes(adLen)
iv := randBytes(c.NonceSize()) iv := randBytes(c.NonceSize())
in := make([]byte, blockSize) in := make([]byte, blockSize)
@ -97,13 +102,12 @@ func bEncrypt(b *testing.B, c cipher.AEAD) {
// Encrypt and append to nonce // Encrypt and append to nonce
c.Seal(dst, iv, in, authData) c.Seal(dst, iv, in, authData)
} }
} }
func bDecrypt(b *testing.B, c cipher.AEAD) { func bDecrypt(b *testing.B, c cipher.AEAD) {
authData := randBytes(adLen) authData := randBytes(adLen)
iv := randBytes(c.NonceSize()) iv := randBytes(c.NonceSize())
plain := randBytes(blockSize) plain := randBytes(gocryptfsBlockSize)
ciphertext := c.Seal(iv, iv, plain, authData) ciphertext := c.Seal(iv, iv, plain, authData)
b.SetBytes(int64(len(plain))) b.SetBytes(int64(len(plain)))
@ -129,6 +133,10 @@ func bStupidGCM(b *testing.B) {
// bGoGCM benchmarks Go stdlib GCM // bGoGCM benchmarks Go stdlib GCM
func bGoGCM(b *testing.B) { func bGoGCM(b *testing.B) {
bGoGCMBlockSize(b, gocryptfsBlockSize)
}
func bGoGCMBlockSize(b *testing.B, blockSize int) {
gAES, err := aes.NewCipher(randBytes(32)) gAES, err := aes.NewCipher(randBytes(32))
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
@ -137,7 +145,7 @@ func bGoGCM(b *testing.B) {
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }
bEncrypt(b, gGCM) bEncryptBlockSize(b, gGCM, blockSize)
} }
// bAESSIV benchmarks AES-SIV from github.com/aperturerobotics/jacobsa-crypto/siv // bAESSIV benchmarks AES-SIV from github.com/aperturerobotics/jacobsa-crypto/siv

View File

@ -3,6 +3,7 @@ package speed
import ( import (
"crypto/aes" "crypto/aes"
"crypto/cipher" "crypto/cipher"
"fmt"
"testing" "testing"
"golang.org/x/crypto/chacha20poly1305" "golang.org/x/crypto/chacha20poly1305"
@ -38,6 +39,13 @@ func BenchmarkGoGCM(b *testing.B) {
bGoGCM(b) bGoGCM(b)
} }
func BenchmarkGoGCMBlockSize(b *testing.B) {
for blockSize := 1024; blockSize <= 1024*1024; blockSize *= 2 {
name := fmt.Sprintf("%d", blockSize)
b.Run(name, func(b *testing.B) { bGoGCMBlockSize(b, blockSize) })
}
}
func BenchmarkGoGCMDecrypt(b *testing.B) { func BenchmarkGoGCMDecrypt(b *testing.B) {
gAES, err := aes.NewCipher(randBytes(32)) gAES, err := aes.NewCipher(randBytes(32))
if err != nil { if err != nil {