工作5年C++服务器,转GO的感触,GO服务器业余时间整理的,用时2个月,框架到细节

bobohume · · 21549 次点击
package main import ( "time" "fmt" "sync" "runtime" "github.com/smartystreets/go-disruptor" ) const ( RingBufferSize = 1024 * 64 RingBufferMask = RingBufferSize - 1 ReserveOne = 1 ReserveMany = 16 ReserveManyDelta = ReserveMany - 1 DisruptorCleanup = time.Millisecond * 10 ) var ringBuffer = [RingBufferSize]int64{} var num = 5 func main() { NumPublishers := num //runtime.NumCPU() totalIterations := int64(1000 * 1000 * 20) iterations := totalIterations / int64(NumPublishers) totalIterations = iterations * int64(NumPublishers) fmt.Printf("Total: %d, Iterations: %d, Publisher: %d, Consumer: 1\n", totalIterations, iterations, NumPublishers) runtime.GOMAXPROCS(NumPublishers) var consumer = &countConsumer{TotalIterations: totalIterations, Count: 0} consumer.WG.Add(1) controller := disruptor.Configure(RingBufferSize).WithConsumerGroup(consumer).BuildShared() controller.Start() defer controller.Stop() var wg sync.WaitGroup wg.Add(NumPublishers + 1) var sendWG sync.WaitGroup sendWG.Add(NumPublishers) for i := 0; i < NumPublishers; i++ { go func() { writer := controller.Writer() wg.Done() wg.Wait() current1 := disruptor.InitialSequenceValue for current1 < iterations { current := writer.Reserve(1) ringBuffer[current&RingBufferMask] = current writer.Commit(current, current) current1++ } sendWG.Done() }() } wg.Done() t := time.Now().UnixNano() wg.Wait() //waiting for ready as a barrier fmt.Println("start to publish") sendWG.Wait() fmt.Println("Finished to publish") consumer.WG.Wait() fmt.Println("Finished to consume") //waiting for consumer t = (time.Now().UnixNano() - t) / 1000000 //ms fmt.Printf("opsPerSecond: %d\n", totalIterations*1000/t) main1() } type countConsumer struct { Count int64 TotalIterations int64 WG sync.WaitGroup } func (cc *countConsumer) Consume(lower, upper int64) { for lower <= upper { message := ringBuffer[lower&RingBufferMask] if message != lower { warning := fmt.Sprintf("\nRace condition--Sequence: %d, Message: %d\n", lower, message) fmt.Printf(warning) panic(warning) } lower++ cc.Count++ //fmt.Printf("count: %d, message: %d\n", cc.Count-1, message) if cc.Count == cc.TotalIterations { cc.WG.Done() return } } } func main1() { NumPublishers := num //runtime.NumCPU() totalIterations := int64(1000 * 1000 * 20) iterations := totalIterations / int64(NumPublishers) totalIterations = iterations * int64(NumPublishers) channel := make(chan int64, 1024*64) var wg sync.WaitGroup wg.Add(NumPublishers + 1) var readerWG sync.WaitGroup readerWG.Add(1) for i := 0; i < NumPublishers; i++ { go func() { wg.Done() wg.Wait() for i := int64(0); i < iterations; { channel <- i i++ } }() } go func() { for i := int64(0); i < totalIterations; i++ { select { case msg := <-channel: if NumPublishers == 1 && msg != i { //panic("Out of sequence") } } } readerWG.Done() }() wg.Done() t := time.Now().UnixNano() wg.Wait() readerWG.Wait() t = (time.Now().UnixNano() - t) / 1000000 //ms fmt.Printf("opsPerSecond: %d\n", totalIterations*1000/t) }
#21
更多评论
haoyupei
大道至简,始终如一
c++ 和lua这是个好模式啊
#2