benchmark_test.go 982 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package crypto_test
  2. import (
  3. "crypto/cipher"
  4. "testing"
  5. . "github.com/v2ray/v2ray-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. for i := 0; i < b.N; i++ {
  13. c.XORKeyStream(output, input)
  14. }
  15. }
  16. func BenchmarkChaCha20(b *testing.B) {
  17. key := make([]byte, 32)
  18. nonce := make([]byte, 8)
  19. c := NewChaCha20Stream(key, nonce)
  20. benchmarkStream(b, c)
  21. }
  22. func BenchmarkChaCha20IETF(b *testing.B) {
  23. key := make([]byte, 32)
  24. nonce := make([]byte, 12)
  25. c := NewChaCha20Stream(key, nonce)
  26. benchmarkStream(b, c)
  27. }
  28. func BenchmarkAESEncryption(b *testing.B) {
  29. key := make([]byte, 32)
  30. iv := make([]byte, 16)
  31. c, _ := NewAesEncryptionStream(key, iv)
  32. benchmarkStream(b, c)
  33. }
  34. func BenchmarkAESDecryption(b *testing.B) {
  35. key := make([]byte, 32)
  36. iv := make([]byte, 16)
  37. c, _ := NewAesDecryptionStream(key, iv)
  38. benchmarkStream(b, c)
  39. }