github.com/MeteorsLiu/rand@v0.0.0-20230523094032-b55484ce1d5d/rand_test.go (about)

     1  package rand
     2  
     3  import (
     4  	r "math/rand"
     5  	"sync"
     6  	"sync/atomic"
     7  	"testing"
     8  
     9  	"github.com/MeteorsLiu/wyhash"
    10  )
    11  
    12  func TestAll(t *testing.T) {
    13  	t.Log(ExpFloat64())
    14  	t.Log(Float32())
    15  	t.Log(Float64())
    16  	t.Log(Int())
    17  	t.Log(Int31())
    18  	t.Log(Int31n(123))
    19  	t.Log(Int63())
    20  	t.Log(Int63n(123))
    21  	t.Log(Intn(123))
    22  	t.Log(NormFloat64())
    23  	t.Log(Perm(5))
    24  	t.Log(Uint32())
    25  	t.Log(Uint64())
    26  	t.Log(Int31range(10, 20))
    27  	t.Log(Intrange(100, 200))
    28  	t.Log(Int63range(10522, 20453))
    29  	t.Log(Uniform32(30.5, 55.5))
    30  	t.Log(Uniform64(30.5, 55.5))
    31  
    32  	Do(func(rd *r.Rand) {
    33  		t.Log(rd.Int())
    34  		t.Log(rd.Intn(50))
    35  		t.Log(rd.Uint64())
    36  	})
    37  
    38  	buf := make([]byte, 64)
    39  	Read(buf)
    40  	t.Log(buf)
    41  
    42  	ReadN(buf, 32, 126)
    43  	t.Log(buf)
    44  }
    45  
    46  func TestConcurrent(t *testing.T) {
    47  	var wg sync.WaitGroup
    48  	wg.Add(5)
    49  	for i := 0; i < 5; i++ {
    50  		go func(id int) {
    51  			defer wg.Done()
    52  			t.Log(id, Intn(16))
    53  		}(i)
    54  	}
    55  	wg.Wait()
    56  
    57  	wg.Add(5)
    58  	for i := 0; i < 5; i++ {
    59  		go func(id int) {
    60  			defer wg.Done()
    61  			t.Log(id, Int())
    62  		}(i)
    63  	}
    64  	wg.Wait()
    65  }
    66  
    67  func BenchmarkInt(b *testing.B) {
    68  	b.ResetTimer()
    69  	for i := 0; i < b.N; i++ {
    70  		_ = Int()
    71  	}
    72  }
    73  
    74  func BenchmarkInt31N(b *testing.B) {
    75  	b.ResetTimer()
    76  	for i := 0; i < b.N; i++ {
    77  		_ = Intn(123456)
    78  	}
    79  }
    80  
    81  func BenchmarkInt63N(b *testing.B) {
    82  	b.ResetTimer()
    83  	for i := 0; i < b.N; i++ {
    84  		_ = Intn(1<<31 + 10000)
    85  	}
    86  }
    87  
    88  func BenchmarkGoInt63N(b *testing.B) {
    89  	b.ResetTimer()
    90  	for i := 0; i < b.N; i++ {
    91  		_ = r.Intn(1<<31 + 10000)
    92  	}
    93  }
    94  
    95  func BenchmarkGoInt31N(b *testing.B) {
    96  	b.ResetTimer()
    97  	for i := 0; i < b.N; i++ {
    98  		_ = r.Intn(123456)
    99  	}
   100  }
   101  
   102  func BenchmarkGoInt(b *testing.B) {
   103  	b.ResetTimer()
   104  	for i := 0; i < b.N; i++ {
   105  		_ = r.Int()
   106  	}
   107  }
   108  
   109  func BenchmarkReadNPowerOfTwo(b *testing.B) {
   110  	buf := make([]byte, 32)
   111  	b.ResetTimer()
   112  	for i := 0; i < b.N; i++ {
   113  		ReadN(buf, 32, 48)
   114  	}
   115  }
   116  
   117  func BenchmarkRead(b *testing.B) {
   118  	buf := make([]byte, 32)
   119  	b.ResetTimer()
   120  	for i := 0; i < b.N; i++ {
   121  		Read(buf)
   122  	}
   123  }
   124  
   125  func BenchmarkGoRead(b *testing.B) {
   126  	buf := make([]byte, 32)
   127  	b.ResetTimer()
   128  	for i := 0; i < b.N; i++ {
   129  		r.Read(buf)
   130  	}
   131  }
   132  
   133  func BenchmarkReadN(b *testing.B) {
   134  	buf := make([]byte, 32)
   135  	b.ResetTimer()
   136  	for i := 0; i < b.N; i++ {
   137  		ReadN(buf, 32, 126)
   138  	}
   139  }
   140  
   141  func BenchmarkReadNU(b *testing.B) {
   142  	buf := make([]byte, 32)
   143  	b.ResetTimer()
   144  	for i := 0; i < b.N; i++ {
   145  		ReadNU(buf, 32, 126)
   146  	}
   147  }
   148  
   149  func BenchmarkReadSmall(b *testing.B) {
   150  	// 1 KB
   151  	buf := make([]byte, 1024)
   152  	b.ResetTimer()
   153  	for i := 0; i < b.N; i++ {
   154  		Read(buf)
   155  	}
   156  }
   157  
   158  func BenchmarkGoReadSmall(b *testing.B) {
   159  	// 32 KB
   160  	buf := make([]byte, 1024)
   161  	b.ResetTimer()
   162  	for i := 0; i < b.N; i++ {
   163  		r.Read(buf)
   164  	}
   165  }
   166  
   167  func BenchmarkReadMedium(b *testing.B) {
   168  	buf := make([]byte, 32*1024)
   169  	b.ResetTimer()
   170  	for i := 0; i < b.N; i++ {
   171  		Read(buf)
   172  	}
   173  }
   174  
   175  func BenchmarkGoReadMedium(b *testing.B) {
   176  	buf := make([]byte, 32*1024)
   177  	b.ResetTimer()
   178  	for i := 0; i < b.N; i++ {
   179  		r.Read(buf)
   180  	}
   181  }
   182  
   183  func BenchmarkReadLarge(b *testing.B) {
   184  	// 128 KB
   185  	buf := make([]byte, 128*1024)
   186  	b.ResetTimer()
   187  	for i := 0; i < b.N; i++ {
   188  		Read(buf)
   189  	}
   190  }
   191  
   192  func BenchmarkGoReadLarge(b *testing.B) {
   193  	buf := make([]byte, 128*1024)
   194  	b.ResetTimer()
   195  	for i := 0; i < b.N; i++ {
   196  		r.Read(buf)
   197  	}
   198  }
   199  
   200  func BenchmarkParallel(b *testing.B) {
   201  	var wg sync.WaitGroup
   202  	wg.Add(b.N)
   203  	b.ResetTimer()
   204  	for i := 0; i < b.N; i++ {
   205  		go func() {
   206  			defer wg.Done()
   207  			_ = Int()
   208  		}()
   209  	}
   210  	wg.Wait()
   211  }
   212  
   213  func BenchmarkGoParallel(b *testing.B) {
   214  	var wg sync.WaitGroup
   215  	wg.Add(b.N)
   216  	b.ResetTimer()
   217  	for i := 0; i < b.N; i++ {
   218  		go func() {
   219  			defer wg.Done()
   220  			_ = r.Int()
   221  		}()
   222  	}
   223  	wg.Wait()
   224  }
   225  
   226  func BenchmarkParallelRead(b *testing.B) {
   227  	var wg sync.WaitGroup
   228  	wg.Add(b.N)
   229  	b.ResetTimer()
   230  	for i := 0; i < b.N; i++ {
   231  		go func() {
   232  			defer wg.Done()
   233  			buf := make([]byte, 64)
   234  			Read(buf)
   235  		}()
   236  	}
   237  	wg.Wait()
   238  }
   239  
   240  func BenchmarkGoParallelRead(b *testing.B) {
   241  	var wg sync.WaitGroup
   242  	wg.Add(b.N)
   243  	b.ResetTimer()
   244  	for i := 0; i < b.N; i++ {
   245  		go func() {
   246  			defer wg.Done()
   247  			buf := make([]byte, 64)
   248  			r.Read(buf)
   249  		}()
   250  	}
   251  	wg.Wait()
   252  }
   253  
   254  func BenchmarkParallelReadN(b *testing.B) {
   255  	var wg sync.WaitGroup
   256  	wg.Add(b.N)
   257  	b.ResetTimer()
   258  	for i := 0; i < b.N; i++ {
   259  		go func() {
   260  			defer wg.Done()
   261  			buf := make([]byte, 64)
   262  			ReadN(buf, 32, 48)
   263  		}()
   264  	}
   265  	wg.Wait()
   266  }
   267  
   268  func BenchmarkParallelReadNU(b *testing.B) {
   269  	var wg sync.WaitGroup
   270  	wg.Add(b.N)
   271  	b.ResetTimer()
   272  	for i := 0; i < b.N; i++ {
   273  		go func() {
   274  			defer wg.Done()
   275  			buf := make([]byte, 64)
   276  			ReadNU(buf, 32, 48)
   277  		}()
   278  	}
   279  	wg.Wait()
   280  }
   281  
   282  func BenchmarkWyhashParallelRead(b *testing.B) {
   283  	var rng wyhash.SRNG
   284  	var wg sync.WaitGroup
   285  	wg.Add(b.N)
   286  	b.ResetTimer()
   287  	for i := 0; i < b.N; i++ {
   288  		go func() {
   289  			defer wg.Done()
   290  			buf := make([]byte, 64)
   291  			rng.Read(buf)
   292  		}()
   293  	}
   294  	wg.Wait()
   295  }
   296  
   297  func BenchmarkWyhashPoolParallelRead(b *testing.B) {
   298  	var lock int32
   299  	var rng wyhash.RNG
   300  	var srng wyhash.SRNG
   301  	var wg sync.WaitGroup
   302  	wg.Add(b.N)
   303  	b.ResetTimer()
   304  	for i := 0; i < b.N; i++ {
   305  		go func() {
   306  			defer wg.Done()
   307  			buf := make([]byte, 64)
   308  			if lock == 0 {
   309  				if atomic.CompareAndSwapInt32(&lock, 0, 1) {
   310  					rng.Read(buf)
   311  					atomic.StoreInt32(&lock, 0)
   312  					return
   313  				}
   314  			}
   315  			srng.Read(buf)
   316  		}()
   317  	}
   318  	wg.Wait()
   319  }
   320  
   321  func BenchmarkGoMultipleDo(b *testing.B) {
   322  	var wg sync.WaitGroup
   323  	wg.Add(b.N)
   324  	b.ResetTimer()
   325  	for i := 0; i < b.N; i++ {
   326  		go func() {
   327  			defer wg.Done()
   328  			buf := make([]byte, 64)
   329  			r.Read(buf)
   330  			_ = r.Int()
   331  			_ = r.Intn(50)
   332  			_ = r.Uint64()
   333  		}()
   334  	}
   335  	wg.Wait()
   336  }
   337  
   338  func BenchmarkMultipleDo(b *testing.B) {
   339  	var wg sync.WaitGroup
   340  	wg.Add(b.N)
   341  	b.ResetTimer()
   342  	for i := 0; i < b.N; i++ {
   343  		go func() {
   344  			defer wg.Done()
   345  			buf := make([]byte, 64)
   346  			Do(func(rd *r.Rand) {
   347  				rd.Read(buf)
   348  				_ = rd.Int()
   349  				_ = rd.Intn(50)
   350  				_ = rd.Uint64()
   351  			})
   352  		}()
   353  	}
   354  	wg.Wait()
   355  }