SHARE
TWEET

Untitled

a guest Apr 20th, 2019 114 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "math/rand"
  6.     "sort"
  7.     "sync"
  8.     "time"
  9.  
  10.     "github.com/RussellLuo/timingwheel"
  11. )
  12.  
  13. func main() {
  14.     rand.Seed(time.Now().UTC().UnixNano())
  15.  
  16.     profileTimerWithFixQPS(8000)
  17.     // counts := []int{8000, 10000}
  18.     // for _, count := range counts {
  19.     //  profileTimers(count)
  20.     // }
  21. }
  22.  
  23. func profileTimers(count int) {
  24.     buffers := make(chan time.Duration, count)
  25.     var waitGroup sync.WaitGroup
  26.     waitGroup.Add(count)
  27.  
  28.     tws := []*timingwheel.TimingWheel{}
  29.     twCount := 1
  30.     for i := 0; i < twCount; i++ {
  31.         tw := timingwheel.NewTimingWheel(3000*time.Microsecond, 4)
  32.         tw.Start()
  33.         tws = append(tws, tw)
  34.     }
  35.  
  36.     for i := 0; i < count; i++ {
  37.         twIndex := rand.Intn(len(tws))
  38.         go func() {
  39.             defer waitGroup.Done()
  40.             start := time.Now()
  41.             ch := make(chan int)
  42.             tws[twIndex].AfterFunc(10*time.Millisecond, func() {
  43.                 ch <- 1
  44.             })
  45.             select {
  46.             case <-ch:
  47.                 end := time.Now()
  48.                 buffers <- end.Sub(start)
  49.             }
  50.         }()
  51.     }
  52.     waitGroup.Wait()
  53.     close(buffers)
  54.     for _, tw := range tws {
  55.         tw.Stop()
  56.     }
  57.  
  58.     sum := time.Duration(0)
  59.     durations := []time.Duration{}
  60.     for time := range buffers {
  61.         sum += time
  62.         durations = append(durations, time)
  63.     }
  64.     sort.Slice(durations, func(i, j int) bool {
  65.         return durations[i] < durations[j]
  66.     })
  67.  
  68.     fmt.Printf("with %d timers\n", count)
  69.     fmt.Println("max\t", durations[count-1])
  70.     fmt.Println("pct99\t", durations[int(float64(count)*0.99)])
  71.     fmt.Println("pct95\t", durations[int(float64(count)*0.95)])
  72.     fmt.Println("avg\t", sum/time.Duration(count))
  73.     fmt.Println("pct5\t", durations[int(float64(count)*0.05)])
  74.     fmt.Println("pct1\t", durations[int(float64(count)*0.01)])
  75.     fmt.Println("min\t", durations[0])
  76. }
  77.  
  78. func profileTimerWithFixQPS(qps int) {
  79.     buffer := make(chan struct{}, qps)
  80.     tw := timingwheel.NewTimingWheel(3000*time.Microsecond, 10)
  81.     tw.Start()
  82.  
  83.     count := 0
  84.     fatal := 0
  85.  
  86.     var lock sync.Mutex
  87.     for {
  88.         count++
  89.         buffer <- struct{}{}
  90.  
  91.         go func() {
  92.             start := time.Now()
  93.             ch := make(chan int)
  94.             tw.AfterFunc(10*time.Millisecond, func() {
  95.                 ch <- 1
  96.             })
  97.             select {
  98.             case <-ch:
  99.                 timeouts := time.Since(start)
  100.                 if timeouts > 20*time.Millisecond {
  101.                     lock.Lock()
  102.                     fatal++
  103.                     lock.Unlock()
  104.                 }
  105.             }
  106.             fmt.Printf("%d/%d\n", fatal, count)
  107.             <-buffer
  108.         }()
  109.     }
  110. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top