github.com/ii64/gouring@v0.4.1/uring_bench_test.go (about)

     1  package gouring
     2  
     3  import (
     4  	"context"
     5  	"runtime"
     6  	"syscall"
     7  	"testing"
     8  )
     9  
    10  func BenchmarkQueueNop(b *testing.B) {
    11  	type opt struct {
    12  		name    string
    13  		entries uint32
    14  		p       IoUringParams
    15  	}
    16  
    17  	ts := []opt{
    18  		{"def-256", 256, IoUringParams{Flags: 0}},
    19  		{"sqpoll-256-4-10000", 256, IoUringParams{Flags: IORING_SETUP_SQPOLL, SqThreadCpu: 16, SqThreadIdle: 10_000}},
    20  	}
    21  
    22  	consumer := func(h *IoUring, ctx context.Context, count int) {
    23  		var cqe *IoUringCqe
    24  		var err error
    25  		for i := 0; i < count; i++ {
    26  			if ctx.Err() != nil {
    27  				return
    28  			}
    29  			err = h.WaitCqe(&cqe)
    30  			if err == syscall.EINTR {
    31  				continue // ignore INTR
    32  			} else if err != nil {
    33  				panic(err)
    34  			}
    35  			if cqe.Res < 0 {
    36  				panic(syscall.Errno(-cqe.Res))
    37  			}
    38  
    39  			h.SeenCqe(cqe)
    40  		}
    41  	}
    42  
    43  	for _, tc := range ts {
    44  		b.Run(tc.name, func(b *testing.B) {
    45  			h := testNewIoUringWithParams(b, tc.entries, &tc.p)
    46  			defer h.Close()
    47  			var (
    48  				j         uint32
    49  				sqe       *IoUringSqe
    50  				err       error
    51  				submitted int
    52  			)
    53  
    54  			ctx, cancel := context.WithCancel(context.Background())
    55  			defer cancel()
    56  
    57  			b.ResetTimer()
    58  			for i := 0; i < b.N; i++ {
    59  				for j = 0; j < tc.entries; j++ {
    60  					for {
    61  						// sqe could be nil if SQ is already full so we spin until we got one
    62  						sqe = h.GetSqe()
    63  						if sqe != nil {
    64  							break
    65  						}
    66  						runtime.Gosched()
    67  					}
    68  					PrepNop(sqe)
    69  					sqe.UserData.SetUint64(uint64(i + int(j)))
    70  				}
    71  				submitted, err = h.Submit()
    72  				if err != nil {
    73  					panic(err)
    74  				}
    75  				consumer(h, ctx, submitted)
    76  			}
    77  			b.StopTimer()
    78  		})
    79  
    80  	}
    81  }