github.com/lestrrat-go/jwx/v2@v2.0.21/jwe/speed_test.go (about)

     1  package jwe
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  var s = []byte(`eyJhbGciOiJSU0EtT0FFUCIsImVuYyI6IkEyNTZHQ00ifQ.OKOawDo13gRp2ojaHV7LFpZcgV7T6DVZKTyKOMTYUmKoTCVJRgckCL9kiMT03JGeipsEdY3mx_etLbbWSrFr05kLzcSr4qKAq7YN7e9jwQRb23nfa6c9d-StnImGyFDbSv04uVuxIp5Zms1gNxKKK2Da14B8S4rzVRltdYwam_lDp5XnZAYpQdb76FdIKLaVmqgfwX7XWRxv2322i-vDxRfqNzo_tETKzpVLzfiwQyeyPGLBIO56YJ7eObdv0je81860ppamavo35UgoRdbYaBcoh9QcfylQr66oc6vFWXRcZ_ZT2LawVCWTIy3brGPi6UklfCpIMfIjf7iGdXKHzg.48V1_ALb6US04U3b.5eym8TW_c8SuK0ltJ3rpYIzOeDQz7TALvtu6UG9oMo4vpzs9tX_EFShS8iB7j6jiSdiwkIr3ajwQzaBtQD_A.XFBoMYUZodetZdvTiFvSkQ`)
     9  
    10  func BenchmarkSplitLib(b *testing.B) {
    11  	for i := 0; i < b.N; i++ {
    12  		SplitLib(s)
    13  	}
    14  }
    15  
    16  func BenchmarkSplitManual(b *testing.B) {
    17  	ret := make([][]byte, 5)
    18  	for i := 0; i < b.N; i++ {
    19  		SplitManual(ret, s)
    20  	}
    21  }
    22  
    23  func SplitLib(buf []byte) [][]byte {
    24  	return bytes.Split(buf, []byte{'.'})
    25  }
    26  
    27  func SplitManual(parts [][]byte, buf []byte) {
    28  	bufi := 0
    29  	for len(buf) > 0 {
    30  		i := bytes.IndexByte(buf, '.')
    31  		if i == -1 {
    32  			return
    33  		}
    34  
    35  		parts[bufi] = buf[:i]
    36  		bufi++
    37  		if len(buf) > i {
    38  			buf = buf[i+1:]
    39  		}
    40  		if bufi == 4 {
    41  			break
    42  		}
    43  	}
    44  
    45  	if i := bytes.IndexByte(buf, '.'); i != -1 {
    46  		return
    47  	}
    48  
    49  	parts[4] = buf
    50  }