github.com/wangyougui/gf/v2@v2.6.5/os/grpool/grpool_z_bench_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). 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/wangyougui/gf.
     6  
     7  // go test *.go -bench=".*"
     8  
     9  package grpool_test
    10  
    11  import (
    12  	"context"
    13  	"testing"
    14  
    15  	"github.com/wangyougui/gf/v2/os/grpool"
    16  )
    17  
    18  var (
    19  	ctx = context.TODO()
    20  	n   = 500000
    21  )
    22  
    23  func increment(ctx context.Context) {
    24  	for i := 0; i < 1000000; i++ {
    25  	}
    26  }
    27  
    28  func BenchmarkGrpool_1(b *testing.B) {
    29  	for i := 0; i < b.N; i++ {
    30  		grpool.Add(ctx, increment)
    31  	}
    32  }
    33  
    34  func BenchmarkGoroutine_1(b *testing.B) {
    35  	for i := 0; i < b.N; i++ {
    36  		go increment(ctx)
    37  	}
    38  }
    39  
    40  func BenchmarkGrpool2(b *testing.B) {
    41  	b.N = n
    42  	for i := 0; i < b.N; i++ {
    43  		grpool.Add(ctx, increment)
    44  	}
    45  }
    46  
    47  func BenchmarkGoroutine2(b *testing.B) {
    48  	b.N = n
    49  	for i := 0; i < b.N; i++ {
    50  		go increment(ctx)
    51  	}
    52  }