github.com/geph-official/geph2@v0.22.6-0.20210211030601-f527cb59b0df/libs/kcp-go/fec_test.go (about)

     1  package kcp
     2  
     3  import (
     4  	"encoding/binary"
     5  	"math/rand"
     6  	"testing"
     7  )
     8  
     9  func BenchmarkFECDecode(b *testing.B) {
    10  	const dataSize = 10
    11  	const paritySize = 3
    12  	const payLoad = 1500
    13  	decoder := newFECDecoder(1024, dataSize, paritySize)
    14  	b.ReportAllocs()
    15  	b.SetBytes(payLoad)
    16  	for i := 0; i < b.N; i++ {
    17  		if rand.Int()%(dataSize+paritySize) == 0 { // random loss
    18  			continue
    19  		}
    20  		pkt := make([]byte, payLoad)
    21  		binary.LittleEndian.PutUint32(pkt, uint32(i))
    22  		if i%(dataSize+paritySize) >= dataSize {
    23  			binary.LittleEndian.PutUint16(pkt[4:], typeParity)
    24  		} else {
    25  			binary.LittleEndian.PutUint16(pkt[4:], typeData)
    26  		}
    27  		decoder.decode(pkt)
    28  	}
    29  }
    30  
    31  func BenchmarkFECEncode(b *testing.B) {
    32  	const dataSize = 10
    33  	const paritySize = 3
    34  	const payLoad = 1500
    35  
    36  	b.ReportAllocs()
    37  	b.SetBytes(payLoad)
    38  	encoder := newFECEncoder(dataSize, paritySize, 0)
    39  	for i := 0; i < b.N; i++ {
    40  		data := make([]byte, payLoad)
    41  		encoder.encode(data)
    42  	}
    43  }