guid_test.go 728 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package nsqd
  2. import (
  3. "testing"
  4. "unsafe"
  5. )
  6. func BenchmarkGUIDCopy(b *testing.B) {
  7. source := make([]byte, 16)
  8. var dest MessageID
  9. for i := 0; i < b.N; i++ {
  10. copy(dest[:], source)
  11. }
  12. }
  13. func BenchmarkGUIDUnsafe(b *testing.B) {
  14. source := make([]byte, 16)
  15. var dest MessageID
  16. for i := 0; i < b.N; i++ {
  17. dest = *(*MessageID)(unsafe.Pointer(&source[0]))
  18. }
  19. _ = dest
  20. }
  21. func BenchmarkGUID(b *testing.B) {
  22. var okays, errors, fails int64
  23. var previd guid
  24. factory := &guidFactory{}
  25. for i := 0; i < b.N; i++ {
  26. id, err := factory.NewGUID()
  27. if err != nil {
  28. errors++
  29. } else if id == previd {
  30. fails++
  31. b.Fail()
  32. } else {
  33. okays++
  34. }
  35. id.Hex()
  36. }
  37. b.Logf("okays=%d errors=%d bads=%d", okays, errors, fails)
  38. }