kengen_test.go 808 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package utils
  2. import (
  3. "fmt"
  4. "sync"
  5. "testing"
  6. "time"
  7. )
  8. func TestNextID(t *testing.T) {
  9. idSize := 100000
  10. var wg sync.WaitGroup
  11. wg.Add(idSize)
  12. m := make(map[Long]int)
  13. startTime := time.Now().UnixNano()
  14. q := make(chan Long, 0)
  15. go func(m map[Long]int) {
  16. for {
  17. select {
  18. case id := <-q:
  19. if _, ok := m[id]; ok {
  20. m[id]++
  21. } else {
  22. m[id] = 1
  23. }
  24. wg.Done()
  25. }
  26. }
  27. }(m)
  28. for i := 0; i < idSize; i++ {
  29. go func() {
  30. q <- NextId()
  31. }()
  32. }
  33. wg.Wait()
  34. endTime := time.Now().UnixNano()
  35. usedTime := time.Duration(endTime - startTime)
  36. fmt.Printf("Id size: %d\nTotal seconds:%d\nAvg nanos per id:%d\n", len(m), usedTime/time.Second, int64(usedTime)/int64(idSize))
  37. badSize := 0
  38. for _, v := range m {
  39. if v > 1 {
  40. badSize++
  41. }
  42. }
  43. if badSize > 0 {
  44. t.Fail()
  45. }
  46. }