github.com/EagleQL/Xray-core@v1.4.3/common/crypto/benchmark_test.go (about)

     1  package crypto_test
     2  
     3  import (
     4  	"crypto/cipher"
     5  	"testing"
     6  
     7  	. "github.com/xtls/xray-core/common/crypto"
     8  )
     9  
    10  const benchSize = 1024 * 1024
    11  
    12  func benchmarkStream(b *testing.B, c cipher.Stream) {
    13  	b.SetBytes(benchSize)
    14  	input := make([]byte, benchSize)
    15  	output := make([]byte, benchSize)
    16  	b.ResetTimer()
    17  	for i := 0; i < b.N; i++ {
    18  		c.XORKeyStream(output, input)
    19  	}
    20  }
    21  
    22  func BenchmarkChaCha20(b *testing.B) {
    23  	key := make([]byte, 32)
    24  	nonce := make([]byte, 8)
    25  	c := NewChaCha20Stream(key, nonce)
    26  	benchmarkStream(b, c)
    27  }
    28  
    29  func BenchmarkChaCha20IETF(b *testing.B) {
    30  	key := make([]byte, 32)
    31  	nonce := make([]byte, 12)
    32  	c := NewChaCha20Stream(key, nonce)
    33  	benchmarkStream(b, c)
    34  }
    35  
    36  func BenchmarkAESEncryption(b *testing.B) {
    37  	key := make([]byte, 32)
    38  	iv := make([]byte, 16)
    39  	c := NewAesEncryptionStream(key, iv)
    40  
    41  	benchmarkStream(b, c)
    42  }
    43  
    44  func BenchmarkAESDecryption(b *testing.B) {
    45  	key := make([]byte, 32)
    46  	iv := make([]byte, 16)
    47  	c := NewAesDecryptionStream(key, iv)
    48  
    49  	benchmarkStream(b, c)
    50  }