github.com/pawelgaczynski/giouring@v0.0.0-20230826085535-69588b89acb9/bench/main.go (about)

     1  // MIT License
     2  //
     3  // Copyright (c) 2023 Paweł Gaczyński
     4  //
     5  // Permission is hereby granted, free of charge, to any person obtaining a
     6  // copy of this software and associated documentation files (the
     7  // "Software"), to deal in the Software without restriction, including
     8  // without limitation the rights to use, copy, modify, merge, publish,
     9  // distribute, sublicense, and/or sell copies of the Software, and to
    10  // permit persons to whom the Software is furnished to do so, subject to
    11  // the following conditions:
    12  //
    13  // The above copyright notice and this permission notice shall be included
    14  // in all copies or substantial portions of the Software.
    15  //
    16  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    17  // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    18  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    19  // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    20  // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    21  // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    22  // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    23  
    24  package main
    25  
    26  import (
    27  	"fmt"
    28  	"log"
    29  	"runtime"
    30  	"time"
    31  
    32  	"github.com/pawelgaczynski/giouring"
    33  )
    34  
    35  const (
    36  	ringSize  = 9192
    37  	batchSize = 4096
    38  	benchTime = 10
    39  )
    40  
    41  func main() {
    42  	runtime.GOMAXPROCS(1)
    43  	runtime.LockOSThread()
    44  	defer runtime.UnlockOSThread()
    45  
    46  	ring, ringErr := giouring.CreateRing(ringSize)
    47  	if ringErr != nil {
    48  		log.Panic(ringErr)
    49  	}
    50  	defer ring.QueueExit()
    51  
    52  	cqeBuff := make([]*giouring.CompletionQueueEvent, batchSize)
    53  
    54  	benchTimeExpected := (time.Second * benchTime).Nanoseconds()
    55  	startTime := time.Now().UnixNano()
    56  	var count uint64
    57  
    58  	for {
    59  		for i := 0; i < batchSize; i++ {
    60  			entry := ring.GetSQE()
    61  			if entry == nil {
    62  				log.Panic()
    63  			}
    64  
    65  			entry.PrepareNop()
    66  		}
    67  		submitted, err := ring.SubmitAndWait(batchSize)
    68  		if err != nil {
    69  			log.Panic(err)
    70  		}
    71  
    72  		if batchSize != int(submitted) {
    73  			log.Panicf("Submitted %d, expected %d", submitted, batchSize)
    74  		}
    75  		peeked := ring.PeekBatchCQE(cqeBuff)
    76  		if batchSize != int(peeked) {
    77  			log.Panicf("Peeked %d, expected %d", peeked, batchSize)
    78  		}
    79  
    80  		count += uint64(peeked)
    81  
    82  		ring.CQAdvance(uint32(submitted))
    83  
    84  		nowTime := time.Now().UnixNano()
    85  		elapsedTime := nowTime - startTime
    86  
    87  		if elapsedTime > benchTimeExpected {
    88  			duration := time.Duration(elapsedTime * int64(time.Nanosecond))
    89  			// nolint: forbidigo
    90  			fmt.Println("Submitted ", count, " entries in ", duration, " seconds")
    91  			// nolint: forbidigo
    92  			fmt.Println(count/uint64(duration.Seconds()), " ops/s")
    93  
    94  			return
    95  		}
    96  	}
    97  }