1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
|
package fastrand
import (
"bytes"
"compress/gzip"
"crypto/rand"
"io"
"math"
"math/big"
mrand "math/rand"
"sync"
"testing"
"time"
)
// panics returns true if the function fn panicked.
func panics(fn func()) (panicked bool) {
defer func() {
panicked = (recover() != nil)
}()
fn()
return
}
// TestUint64nPanics tests that Uint64n panics if n == 0.
func TestUint64nPanics(t *testing.T) {
// Test n = 0.
if !panics(func() { Uint64n(0) }) {
t.Error("expected panic for n == 0")
}
// Test n > 0.
if panics(func() { Uint64n(math.MaxUint64) }) {
t.Error("did not expect panic for n > 0")
}
}
// TestIntnPanics tests that Intn panics if n <= 0.
func TestIntnPanics(t *testing.T) {
// Test n < 0.
if !panics(func() { Intn(-1) }) {
t.Error("expected panic for n < 0")
}
// Test n = 0.
if !panics(func() { Intn(0) }) {
t.Error("expected panic for n == 0")
}
// Test n > 0.
if panics(func() { Intn(1) }) {
t.Error("did not expect panic for n > 0")
}
}
// TestBigIntnPanics tests that BigIntn panics if n <= 0.
func TestBigIntnPanics(t *testing.T) {
// Test n < 0.
if !panics(func() { BigIntn(big.NewInt(-1)) }) {
t.Error("expected panic for n < 0")
}
// Test n = 0.
if !panics(func() { BigIntn(big.NewInt(0)) }) {
t.Error("expected panic for n == 0")
}
// Test n > 0.
if panics(func() { BigIntn(big.NewInt(1)) }) {
t.Error("did not expect panic for n > 0")
}
}
// TestUint64n tests the Uint64n function.
func TestUint64n(t *testing.T) {
const iters = 10000
var counts [10]uint64
for i := 0; i < iters; i++ {
counts[Uint64n(uint64(len(counts)))]++
}
exp := iters / uint64(len(counts))
lower, upper := exp-(exp/10), exp+(exp/10)
for i, n := range counts {
if !(lower < n && n < upper) {
t.Errorf("Expected range of %v-%v for index %v, got %v", lower, upper, i, n)
}
}
}
// TestIntn tests the Intn function.
func TestIntn(t *testing.T) {
const iters = 10000
var counts [10]int
for i := 0; i < iters; i++ {
counts[Intn(len(counts))]++
}
exp := iters / len(counts)
lower, upper := exp-(exp/10), exp+(exp/10)
for i, n := range counts {
if !(lower < n && n < upper) {
t.Errorf("Expected range of %v-%v for index %v, got %v", lower, upper, i, n)
}
}
}
// TestRead tests that Read produces output with sufficiently high entropy.
func TestRead(t *testing.T) {
const size = 10e3
var b bytes.Buffer
zip, _ := gzip.NewWriterLevel(&b, gzip.BestCompression)
if _, err := zip.Write(Bytes(size)); err != nil {
t.Fatal(err)
}
if err := zip.Close(); err != nil {
t.Fatal(err)
}
if b.Len() < size {
t.Error("supposedly high entropy bytes have been compressed!")
}
}
// TestReadConcurrent tests that concurrent calls to 'Read' will not result
// result in identical entropy being produced. Note that for this test to work,
// the points at which 'counter' and 'innerCounter' get incremented need to be
// reduced substantially, to a value like '64'. (larger than the number of
// threads, but not by much).
//
// Note that while this test is capable of catching failures, it's not
// guaranteed to.
func TestReadConcurrent(t *testing.T) {
threads := 32
// Spin up threads which will all be collecting entropy from 'Read' in
// parallel.
closeChan := make(chan struct{})
var wg sync.WaitGroup
wg.Add(threads)
entropys := make([]map[string]struct{}, threads)
for i := 0; i < threads; i++ {
entropys[i] = make(map[string]struct{})
go func(i int) {
for {
select {
case <-closeChan:
wg.Done()
return
default:
}
// Read 32 bytes.
buf := make([]byte, 32)
Read(buf)
bufStr := string(buf)
_, exists := entropys[i][bufStr]
if exists {
t.Error("got the same entropy twice out of the reader")
}
entropys[i][bufStr] = struct{}{}
}
}(i)
}
// Let the threads spin for a bit, then shut them down.
time.Sleep(time.Millisecond * 1250)
close(closeChan)
wg.Wait()
// Compare the entropy collected and verify that no set of 32 bytes was
// output twice.
allEntropy := make(map[string]struct{})
for _, entropy := range entropys {
for str := range entropy {
_, exists := allEntropy[str]
if exists {
t.Error("got the same entropy twice out of the reader")
}
allEntropy[str] = struct{}{}
}
}
}
// TestRandConcurrent checks that there are no race conditions when using the
// rngs concurrently.
func TestRandConcurrent(t *testing.T) {
// Spin up one goroutine for each exported function. Each goroutine calls
// its function in a tight loop.
funcs := []func(){
// Read some random data into a large byte slice.
func() { Read(make([]byte, 16e3)) },
// Call io.Copy on the global reader.
func() { io.CopyN(new(bytes.Buffer), Reader, 16e3) },
// Call Intn
func() { Intn(math.MaxUint64/4 + 1) },
// Call BigIntn on a 256-bit int
func() { BigIntn(new(big.Int).SetBytes(Bytes(32))) },
// Call Perm
func() { Perm(150) },
}
closeChan := make(chan struct{})
var wg sync.WaitGroup
for i := range funcs {
wg.Add(1)
go func(i int) {
for {
select {
case <-closeChan:
wg.Done()
return
default:
}
funcs[i]()
}
}(i)
}
// Allow goroutines to run for a moment.
time.Sleep(100 * time.Millisecond)
// Close the channel and wait for everything to clean up.
close(closeChan)
wg.Wait()
}
// TestPerm tests the Perm function.
func TestPerm(t *testing.T) {
chars := "abcde" // string to be permuted
createPerm := func() string {
s := make([]byte, len(chars))
for i, j := range Perm(len(chars)) {
s[i] = chars[j]
}
return string(s)
}
// create (factorial(len(chars)) * 100) permutations
permCount := make(map[string]int)
for i := 0; i < 12000; i++ {
permCount[createPerm()]++
}
// we should have seen each permutation approx. 100 times
for p, n := range permCount {
if n < 50 || n > 150 {
t.Errorf("saw permutation %v times: %v", n, p)
}
}
}
// BenchmarkUint64n benchmarks the Uint64n function for small uint64s.
func BenchmarkUint64n(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Uint64n(4e3)
}
}
// BenchmarkUint64nLarge benchmarks the Uint64n function for large uint64s.
func BenchmarkUint64nLarge(b *testing.B) {
for i := 0; i < b.N; i++ {
// constant chosen to trigger resampling (see Uint64n)
_ = Uint64n(math.MaxUint64/2 + 1)
}
}
// BenchmarkIntn benchmarks the Intn function for small ints.
func BenchmarkIntn(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Intn(4e3)
}
}
// BenchmarkIntnLarge benchmarks the Intn function for large ints.
func BenchmarkIntnLarge(b *testing.B) {
for i := 0; i < b.N; i++ {
// constant chosen to trigger resampling (see Intn)
_ = Intn(math.MaxUint64/4 + 1)
}
}
// BenchmarkBigIntn benchmarks the BigIntn function for small ints.
func BenchmarkBigIntn(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = BigIntn(big.NewInt(4e3))
}
}
// BenchmarkBigIntnLarge benchmarks the BigIntn function for large ints.
func BenchmarkBigIntnLarge(b *testing.B) {
// (2^63)^10
huge := new(big.Int).Exp(big.NewInt(math.MaxInt64), big.NewInt(10), nil)
for i := 0; i < b.N; i++ {
_ = BigIntn(huge)
}
}
// BenchmarkBigCryptoInt benchmarks the (crypto/rand).Int function for small ints.
func BenchmarkBigCryptoInt(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = rand.Int(rand.Reader, big.NewInt(4e3))
}
}
// BenchmarkBigCryptoIntLarge benchmarks the (crypto/rand).Int function for large ints.
func BenchmarkBigCryptoIntLarge(b *testing.B) {
// (2^63)^10
huge := new(big.Int).Exp(big.NewInt(math.MaxInt64), big.NewInt(10), nil)
for i := 0; i < b.N; i++ {
_, _ = rand.Int(rand.Reader, huge)
}
}
// BenchmarkPerm benchmarks the speed of Perm for small slices.
func BenchmarkPerm32(b *testing.B) {
for i := 0; i < b.N; i++ {
Perm(32)
}
}
// BenchmarkPermLarge benchmarks the speed of Perm for large slices.
func BenchmarkPermLarge4k(b *testing.B) {
for i := 0; i < b.N; i++ {
Perm(4e3)
}
}
// BenchmarkRead benchmarks the speed of Read for small slices.
func BenchmarkRead32(b *testing.B) {
b.SetBytes(32)
buf := make([]byte, 32)
for i := 0; i < b.N; i++ {
Read(buf)
}
}
// BenchmarkRead512kb benchmarks the speed of Read for larger slices.
func BenchmarkRead512kb(b *testing.B) {
b.SetBytes(512e3)
buf := make([]byte, 512e3)
for i := 0; i < b.N; i++ {
Read(buf)
}
}
// BenchmarkRead4Threads32 benchmarks the speed of Read when it's being using
// across four threads.
func BenchmarkRead4Threads32(b *testing.B) {
start := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 4; i++ {
wg.Add(1)
go func() {
buf := make([]byte, 32)
<-start
for i := 0; i < b.N; i++ {
Read(buf)
}
wg.Done()
}()
}
b.SetBytes(4 * 32)
// Signal all threads to begin
b.ResetTimer()
close(start)
// Wait for all threads to exit
wg.Wait()
}
// BenchmarkRead4Threads512kb benchmarks the speed of Read when it's being using
// across four threads with 512kb read sizes.
func BenchmarkRead4Threads512kb(b *testing.B) {
start := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 4; i++ {
wg.Add(1)
go func() {
buf := make([]byte, 512e3)
<-start
for i := 0; i < b.N; i++ {
Read(buf)
}
wg.Done()
}()
}
b.SetBytes(4 * 512e3)
// Signal all threads to begin
b.ResetTimer()
close(start)
// Wait for all threads to exit
wg.Wait()
}
// BenchmarkRead64Threads32 benchmarks the speed of Read when it's being using
// across 64 threads.
func BenchmarkRead64Threads32(b *testing.B) {
start := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 64; i++ {
wg.Add(1)
go func() {
buf := make([]byte, 32)
<-start
for i := 0; i < b.N; i++ {
Read(buf)
}
wg.Done()
}()
}
b.SetBytes(64 * 32)
// Signal all threads to begin
b.ResetTimer()
close(start)
// Wait for all threads to exit
wg.Wait()
}
// BenchmarkRead64Threads512kb benchmarks the speed of Read when it's being using
// across 64 threads with 512kb read sizes.
func BenchmarkRead64Threads512kb(b *testing.B) {
start := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 64; i++ {
wg.Add(1)
go func() {
buf := make([]byte, 512e3)
<-start
for i := 0; i < b.N; i++ {
Read(buf)
}
wg.Done()
}()
}
b.SetBytes(64 * 512e3)
// Signal all threads to begin
b.ResetTimer()
close(start)
// Wait for all threads to exit
wg.Wait()
}
// BenchmarkReadCrypto benchmarks the speed of (crypto/rand).Read for small
// slices. This establishes a lower limit for BenchmarkRead32.
func BenchmarkReadCrypto32(b *testing.B) {
b.SetBytes(32)
buf := make([]byte, 32)
for i := 0; i < b.N; i++ {
rand.Read(buf)
}
}
// BenchmarkReadCrypto512kb benchmarks the speed of (crypto/rand).Read for larger
// slices. This establishes a lower limit for BenchmarkRead512kb.
func BenchmarkReadCrypto512kb(b *testing.B) {
b.SetBytes(512e3)
buf := make([]byte, 512e3)
for i := 0; i < b.N; i++ {
rand.Read(buf)
}
}
// BenchmarkReadCrypto4Threads32 benchmarks the speed of rand.Read when its
// being used across 4 threads with 32 byte read sizes.
func BenchmarkReadCrypto4Threads32(b *testing.B) {
start := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 4; i++ {
wg.Add(1)
go func() {
buf := make([]byte, 32)
<-start
for i := 0; i < b.N; i++ {
_, err := rand.Read(buf)
if err != nil {
b.Fatal(err)
}
}
wg.Done()
}()
}
b.SetBytes(4 * 32)
// Signal all threads to begin
b.ResetTimer()
close(start)
// Wait for all threads to exit
wg.Wait()
}
// BenchmarkReadCrypto4Threads512kb benchmarks the speed of rand.Read when its
// being used across 4 threads with 512 kb read sizes.
func BenchmarkReadCrypto4Threads512kb(b *testing.B) {
start := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 4; i++ {
wg.Add(1)
go func() {
buf := make([]byte, 512e3)
<-start
for i := 0; i < b.N; i++ {
_, err := rand.Read(buf)
if err != nil {
b.Fatal(err)
}
}
wg.Done()
}()
}
b.SetBytes(4 * 512e3)
// Signal all threads to begin
b.ResetTimer()
close(start)
// Wait for all threads to exit
wg.Wait()
}
// BenchmarkReadCrypto64Threads32 benchmarks the speed of rand.Read when its
// being used across 4 threads with 32 byte read sizes.
func BenchmarkReadCrypto64Threads32(b *testing.B) {
start := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 64; i++ {
wg.Add(1)
go func() {
buf := make([]byte, 32)
<-start
for i := 0; i < b.N; i++ {
_, err := rand.Read(buf)
if err != nil {
b.Fatal(err)
}
}
wg.Done()
}()
}
b.SetBytes(64 * 32)
// Signal all threads to begin
b.ResetTimer()
close(start)
// Wait for all threads to exit
wg.Wait()
}
// BenchmarkReadCrypto64Threads512k benchmarks the speed of rand.Read when its
// being used across 4 threads with 512 kb read sizes.
func BenchmarkReadCrypto64Threads512kb(b *testing.B) {
start := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 64; i++ {
wg.Add(1)
go func() {
buf := make([]byte, 512e3)
<-start
for i := 0; i < b.N; i++ {
_, err := rand.Read(buf)
if err != nil {
b.Fatal(err)
}
}
wg.Done()
}()
}
b.SetBytes(64 * 512e3)
// Signal all threads to begin
b.ResetTimer()
close(start)
// Wait for all threads to exit
wg.Wait()
}
// BenchmarkReadMath benchmarks the speed of (math/rand).Read for small
// slices. This establishes an upper limit for BenchmarkRead32.
func BenchmarkReadMath32(b *testing.B) {
b.SetBytes(32)
buf := make([]byte, 32)
for i := 0; i < b.N; i++ {
mrand.Read(buf)
}
}
// BenchmarkReadMath512kb benchmarks the speed of (math/rand).Read for larger
// slices. This establishes an upper limit for BenchmarkRead512kb.
func BenchmarkReadMath512kb(b *testing.B) {
b.SetBytes(512e3)
buf := make([]byte, 512e3)
for i := 0; i < b.N; i++ {
mrand.Read(buf)
}
}
// BenchmarkReadMath4Threads32 benchmarks the speed of ReadMath when it's being using
// across four threads.
func BenchmarkReadMath4Threads32(b *testing.B) {
start := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 4; i++ {
wg.Add(1)
go func() {
buf := make([]byte, 32)
<-start
for i := 0; i < b.N; i++ {
mrand.Read(buf)
}
wg.Done()
}()
}
b.SetBytes(4 * 32)
// Signal all threads to begin
b.ResetTimer()
close(start)
// Wait for all threads to exit
wg.Wait()
}
// BenchmarkReadMath4Threads512kb benchmarks the speed of ReadMath when it's being using
// across four threads with 512kb read sizes.
func BenchmarkReadMath4Threads512kb(b *testing.B) {
start := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 4; i++ {
wg.Add(1)
go func() {
buf := make([]byte, 512e3)
<-start
for i := 0; i < b.N; i++ {
mrand.Read(buf)
}
wg.Done()
}()
}
b.SetBytes(4 * 512e3)
// Signal all threads to begin
b.ResetTimer()
close(start)
// Wait for all threads to exit
wg.Wait()
}
// BenchmarkReadMath64Threads32 benchmarks the speed of ReadMath when it's being using
// across 64 threads.
func BenchmarkReadMath64Threads32(b *testing.B) {
start := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 64; i++ {
wg.Add(1)
go func() {
buf := make([]byte, 32)
<-start
for i := 0; i < b.N; i++ {
mrand.Read(buf)
}
wg.Done()
}()
}
b.SetBytes(64 * 32)
// Signal all threads to begin
b.ResetTimer()
close(start)
// Wait for all threads to exit
wg.Wait()
}
// BenchmarkReadMath64Threads512kb benchmarks the speed of ReadMath when it's being using
// across 64 threads with 512kb read sizes.
func BenchmarkReadMath64Threads512kb(b *testing.B) {
start := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 64; i++ {
wg.Add(1)
go func() {
buf := make([]byte, 512e3)
<-start
for i := 0; i < b.N; i++ {
mrand.Read(buf)
}
wg.Done()
}()
}
b.SetBytes(64 * 512e3)
// Signal all threads to begin
b.ResetTimer()
close(start)
// Wait for all threads to exit
wg.Wait()
}
|