github.com/MerlinKodo/sing-tun@v0.1.15/internal/clashtcpip/tcpip_amd64_test.go (about)

     1  package clashtcpip
     2  
     3  import (
     4  	"crypto/rand"
     5  	"testing"
     6  
     7  	"golang.org/x/sys/cpu"
     8  )
     9  
    10  func Test_SumAVX2(t *testing.T) {
    11  	if !cpu.X86.HasAVX2 {
    12  		t.Skipf("AVX2 unavailable")
    13  	}
    14  
    15  	bytes := make([]byte, chunkSize)
    16  
    17  	for size := 0; size <= chunkSize; size++ {
    18  		for count := 0; count < chunkCount; count++ {
    19  			_, err := rand.Reader.Read(bytes[:size])
    20  			if err != nil {
    21  				t.Skipf("Rand read failed: %v", err)
    22  			}
    23  
    24  			compat := SumCompat(bytes[:size])
    25  			avx := SumAVX2(bytes[:size])
    26  
    27  			if compat != avx {
    28  				t.Errorf("Sum of length=%d mismatched", size)
    29  			}
    30  		}
    31  	}
    32  }
    33  
    34  func Benchmark_SumAVX2(b *testing.B) {
    35  	if !cpu.X86.HasAVX2 {
    36  		b.Skipf("AVX2 unavailable")
    37  	}
    38  
    39  	bytes := make([]byte, chunkSize)
    40  
    41  	_, err := rand.Reader.Read(bytes)
    42  	if err != nil {
    43  		b.Skipf("Rand read failed: %v", err)
    44  	}
    45  
    46  	b.ResetTimer()
    47  
    48  	for i := 0; i < b.N; i++ {
    49  		SumAVX2(bytes)
    50  	}
    51  }