mirror of
https://github.com/ii64/gouring.git
synced 2025-04-01 03:41:44 +02:00
all: use constant for sizeof, add bench test
Signed-off-by: Nugraha <richiisei@gmail.com>
This commit is contained in:
parent
254fa28928
commit
25e599231f
3 changed files with 97 additions and 1 deletions
|
@ -2,7 +2,7 @@ package gouring
|
|||
|
||||
import "unsafe"
|
||||
|
||||
var (
|
||||
const (
|
||||
SizeofUnsigned = unsafe.Sizeof(uint32(0))
|
||||
SizeofUint32 = unsafe.Sizeof(uint32(0))
|
||||
SizeofIoUringSqe = unsafe.Sizeof(IoUringSqe{})
|
||||
|
|
77
uring_bench_test.go
Normal file
77
uring_bench_test.go
Normal file
|
@ -0,0 +1,77 @@
|
|||
package gouring
|
||||
|
||||
import (
|
||||
"context"
|
||||
"syscall"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func BenchmarkQueueNop(b *testing.B) {
|
||||
type opt struct {
|
||||
name string
|
||||
entries uint32
|
||||
p IoUringParams
|
||||
}
|
||||
|
||||
ts := []opt{
|
||||
{"def-256", 256, IoUringParams{Flags: 0}},
|
||||
{"sqpoll-256-4-10000", 256, IoUringParams{Flags: IORING_SETUP_SQPOLL, SqThreadCpu: 8, SqThreadIdle: 10_000}},
|
||||
}
|
||||
|
||||
consumer := func(h *IoUring, ctx context.Context, count int) {
|
||||
var cqe *IoUringCqe
|
||||
var err error
|
||||
for i := 0; i < count; i++ {
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
err = h.WaitCQE(&cqe)
|
||||
if err == syscall.EINTR {
|
||||
continue // ignore INTR
|
||||
} else if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if cqe.Res < 0 {
|
||||
panic(syscall.Errno(-cqe.Res))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, tc := range ts {
|
||||
b.Run(tc.name, func(b *testing.B) {
|
||||
h := testNewIoUringWithParam(b, tc.entries, &tc.p)
|
||||
defer h.Close()
|
||||
var (
|
||||
j uint32
|
||||
sqe *IoUringSqe
|
||||
err error
|
||||
submitted int
|
||||
)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
for j = 0; j < tc.entries; j++ {
|
||||
for {
|
||||
// sqe could be nil if SQ is already full so we spin until we got one
|
||||
sqe = h.GetSQE()
|
||||
if sqe != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
PrepNop(sqe)
|
||||
sqe.UserData = uint64(i + int(j))
|
||||
}
|
||||
submitted, err = h.Submit()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
consumer(h, ctx, submitted)
|
||||
}
|
||||
b.StopTimer()
|
||||
})
|
||||
|
||||
}
|
||||
}
|
|
@ -9,6 +9,25 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type genericTestingT interface {
|
||||
assert.TestingT
|
||||
require.TestingT
|
||||
}
|
||||
|
||||
func testNewIoUring(t genericTestingT, entries uint32, flags uint32) *IoUring {
|
||||
h, err := New(entries, flags)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, h)
|
||||
return h
|
||||
}
|
||||
|
||||
func testNewIoUringWithParam(t genericTestingT, entries uint32, p *IoUringParams) *IoUring {
|
||||
h, err := NewWithParams(entries, p)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, h)
|
||||
return h
|
||||
}
|
||||
|
||||
func TestRingWrapper(t *testing.T) {
|
||||
h := testNewIoUring(t, 256, 0)
|
||||
defer h.Close()
|
||||
|
|
Loading…
Add table
Reference in a new issue