github.com/zhongdalu/gf@v1.0.0/g/container/gqueue/gqueue_bench_test.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/zhongdalu/gf.
     6  
     7  // go test *.go -bench=".*" -benchmem
     8  
     9  package gqueue_test
    10  
    11  import (
    12  	"github.com/zhongdalu/gf/g/container/gqueue"
    13  	"testing"
    14  )
    15  
    16  var bn = 20000000
    17  var length = 1000000
    18  var qstatic = gqueue.New(length)
    19  var qdynamic = gqueue.New()
    20  var cany = make(chan interface{}, length)
    21  
    22  func Benchmark_Gqueue_StaticPushAndPop(b *testing.B) {
    23  	b.N = bn
    24  	for i := 0; i < b.N; i++ {
    25  		qstatic.Push(i)
    26  		qstatic.Pop()
    27  	}
    28  }
    29  
    30  func Benchmark_Gqueue_DynamicPush(b *testing.B) {
    31  	b.N = bn
    32  	for i := 0; i < b.N; i++ {
    33  		qdynamic.Push(i)
    34  	}
    35  }
    36  
    37  func Benchmark_Gqueue_DynamicPop(b *testing.B) {
    38  	b.N = bn
    39  	for i := 0; i < b.N; i++ {
    40  		qdynamic.Pop()
    41  	}
    42  }
    43  
    44  func Benchmark_Channel_PushAndPop(b *testing.B) {
    45  	b.N = bn
    46  	for i := 0; i < b.N; i++ {
    47  		cany <- i
    48  		<-cany
    49  	}
    50  }