github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/prepare/14_concurrent/disruptor/main2.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"sync"
     7  	"time"
     8  )
     9  
    10  func main() {
    11  	NumPublishers := runtime.NumCPU()
    12  	totalIterations := int64(1000 * 1000 * 20)
    13  	iterations := totalIterations / int64(NumPublishers)
    14  	totalIterations = iterations * int64(NumPublishers)
    15  	channel := make(chan int64, 1024*64)
    16  	var wg sync.WaitGroup
    17  	wg.Add(NumPublishers + 1)
    18  	var readerWG sync.WaitGroup
    19  	readerWG.Add(1)
    20  	for i := 0; i < NumPublishers; i++ {
    21  		go func() {
    22  			wg.Done()
    23  			wg.Wait()
    24  			for i := int64(0); i < iterations; {
    25  				select {
    26  				case channel <- i:
    27  					i++
    28  				default:
    29  					continue
    30  				}
    31  			}
    32  		}()
    33  	}
    34  	go func() {
    35  		for i := int64(0); i < totalIterations; i++ {
    36  			select {
    37  			case msg := <-channel:
    38  				if NumPublishers == 1 && msg != i {
    39  					//panic("Out of sequence")
    40  				}
    41  			default:
    42  				continue
    43  			}
    44  		}
    45  		readerWG.Done()
    46  	}()
    47  	wg.Done()
    48  	t := time.Now().UnixNano()
    49  	wg.Wait()
    50  	readerWG.Wait()
    51  	t = (time.Now().UnixNano() - t) / 1000000 //ms
    52  	fmt.Printf("opsPerSecond: %d\n", totalIterations*1000/t)
    53  }