github.com/wangyougui/gf/v2@v2.6.5/util/grand/grand_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 grand_test
    10  
    11  import (
    12  	cryptoRand "crypto/rand"
    13  	"encoding/binary"
    14  	mathRand "math/rand"
    15  	"testing"
    16  
    17  	"github.com/wangyougui/gf/v2/util/grand"
    18  )
    19  
    20  var (
    21  	buffer         = make([]byte, 8)
    22  	randBuffer4    = make([]byte, 4)
    23  	randBuffer1024 = make([]byte, 1024)
    24  	strForStr      = "我爱GoFrame"
    25  )
    26  
    27  func Benchmark_Math_Rand_Int(b *testing.B) {
    28  	for i := 0; i < b.N; i++ {
    29  		mathRand.Int()
    30  	}
    31  }
    32  
    33  func Benchmark_CryptoRand_Buffer4(b *testing.B) {
    34  	for i := 0; i < b.N; i++ {
    35  		cryptoRand.Read(randBuffer4)
    36  	}
    37  }
    38  
    39  func Benchmark_CryptoRand_Buffer1024(b *testing.B) {
    40  	for i := 0; i < b.N; i++ {
    41  		cryptoRand.Read(randBuffer1024)
    42  	}
    43  }
    44  
    45  func Benchmark_GRand_Intn(b *testing.B) {
    46  	for i := 0; i < b.N; i++ {
    47  		grand.N(0, 99)
    48  	}
    49  }
    50  
    51  func Benchmark_Perm10(b *testing.B) {
    52  	for i := 0; i < b.N; i++ {
    53  		grand.Perm(10)
    54  	}
    55  }
    56  
    57  func Benchmark_Perm100(b *testing.B) {
    58  	for i := 0; i < b.N; i++ {
    59  		grand.Perm(100)
    60  	}
    61  }
    62  
    63  func Benchmark_Rand_N1(b *testing.B) {
    64  	for i := 0; i < b.N; i++ {
    65  		grand.N(0, 99)
    66  	}
    67  }
    68  
    69  func Benchmark_Rand_N2(b *testing.B) {
    70  	for i := 0; i < b.N; i++ {
    71  		grand.N(0, 999999999)
    72  	}
    73  }
    74  
    75  func Benchmark_B(b *testing.B) {
    76  	for i := 0; i < b.N; i++ {
    77  		grand.B(16)
    78  	}
    79  }
    80  
    81  func Benchmark_S(b *testing.B) {
    82  	for i := 0; i < b.N; i++ {
    83  		grand.S(16)
    84  	}
    85  }
    86  
    87  func Benchmark_S_Symbols(b *testing.B) {
    88  	for i := 0; i < b.N; i++ {
    89  		grand.S(16, true)
    90  	}
    91  }
    92  
    93  func Benchmark_Str(b *testing.B) {
    94  	for i := 0; i < b.N; i++ {
    95  		grand.Str(strForStr, 16)
    96  	}
    97  }
    98  
    99  func Benchmark_Symbols(b *testing.B) {
   100  	for i := 0; i < b.N; i++ {
   101  		grand.Symbols(16)
   102  	}
   103  }
   104  
   105  func Benchmark_Uint32Converting(b *testing.B) {
   106  	for i := 0; i < b.N; i++ {
   107  		binary.LittleEndian.Uint32([]byte{1, 1, 1, 1})
   108  	}
   109  }
   110  
   111  func Benchmark_CryptoRand_Buffer(b *testing.B) {
   112  	for i := 0; i < b.N; i++ {
   113  		if _, err := cryptoRand.Read(buffer); err == nil {
   114  			binary.LittleEndian.Uint64(buffer)
   115  		}
   116  	}
   117  }