benchmark_test.go 990 B

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