client.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "sync"
  8. "time"
  9. "github.com/tal-tech/go-zero/core/lang"
  10. "github.com/tal-tech/go-zero/core/logx"
  11. "github.com/tal-tech/go-zero/core/threading"
  12. "gopkg.in/cheggaaa/pb.v1"
  13. )
  14. var (
  15. freq = flag.Int("freq", 100, "frequency")
  16. duration = flag.String("duration", "10s", "duration")
  17. )
  18. type (
  19. counting struct {
  20. ok int
  21. fail int
  22. reject int
  23. errs int
  24. unknown int
  25. }
  26. metric struct {
  27. counting
  28. lock sync.Mutex
  29. }
  30. )
  31. func (m *metric) addOk() {
  32. m.lock.Lock()
  33. m.ok++
  34. m.lock.Unlock()
  35. }
  36. func (m *metric) addFail() {
  37. m.lock.Lock()
  38. m.ok++
  39. m.lock.Unlock()
  40. }
  41. func (m *metric) addReject() {
  42. m.lock.Lock()
  43. m.ok++
  44. m.lock.Unlock()
  45. }
  46. func (m *metric) addErrs() {
  47. m.lock.Lock()
  48. m.errs++
  49. m.lock.Unlock()
  50. }
  51. func (m *metric) addUnknown() {
  52. m.lock.Lock()
  53. m.unknown++
  54. m.lock.Unlock()
  55. }
  56. func (m *metric) reset() counting {
  57. m.lock.Lock()
  58. result := counting{
  59. ok: m.ok,
  60. fail: m.fail,
  61. reject: m.reject,
  62. errs: m.errs,
  63. unknown: m.unknown,
  64. }
  65. m.ok = 0
  66. m.fail = 0
  67. m.reject = 0
  68. m.errs = 0
  69. m.unknown = 0
  70. m.lock.Unlock()
  71. return result
  72. }
  73. func runRequests(url string, frequency int, metrics *metric, done <-chan lang.PlaceholderType) {
  74. ticker := time.NewTicker(time.Second / time.Duration(frequency))
  75. defer ticker.Stop()
  76. for {
  77. select {
  78. case <-ticker.C:
  79. go func() {
  80. resp, err := http.Get(url)
  81. if err != nil {
  82. metrics.addErrs()
  83. return
  84. }
  85. defer resp.Body.Close()
  86. switch resp.StatusCode {
  87. case http.StatusOK:
  88. metrics.addOk()
  89. case http.StatusInternalServerError:
  90. metrics.addFail()
  91. case http.StatusServiceUnavailable:
  92. metrics.addReject()
  93. default:
  94. metrics.addUnknown()
  95. }
  96. }()
  97. case <-done:
  98. return
  99. }
  100. }
  101. }
  102. func main() {
  103. flag.Parse()
  104. fp, err := os.Create("result.csv")
  105. logx.Must(err)
  106. defer fp.Close()
  107. fmt.Fprintln(fp, "seconds,goodOk,goodFail,goodReject,goodErrs,goodUnknowns,goodDropRatio,"+
  108. "heavyOk,heavyFail,heavyReject,heavyErrs,heavyUnknowns,heavyDropRatio")
  109. var gm, hm metric
  110. dur, err := time.ParseDuration(*duration)
  111. logx.Must(err)
  112. done := make(chan lang.PlaceholderType)
  113. group := threading.NewRoutineGroup()
  114. group.RunSafe(func() {
  115. runRequests("http://localhost:8080/heavy", *freq, &hm, done)
  116. })
  117. group.RunSafe(func() {
  118. runRequests("http://localhost:8080/good", *freq, &gm, done)
  119. })
  120. go func() {
  121. ticker := time.NewTicker(time.Second)
  122. defer ticker.Stop()
  123. var seconds int
  124. for range ticker.C {
  125. seconds++
  126. g := gm.reset()
  127. h := hm.reset()
  128. fmt.Fprintf(fp, "%d,%d,%d,%d,%d,%d,%.1f,%d,%d,%d,%d,%d,%.1f\n",
  129. seconds, g.ok, g.fail, g.reject, g.errs, g.unknown,
  130. float32(g.reject)/float32(g.ok+g.fail+g.reject+g.unknown),
  131. h.ok, h.fail, h.reject, h.errs, h.unknown,
  132. float32(h.reject)/float32(h.ok+h.fail+h.reject+h.unknown))
  133. }
  134. }()
  135. go func() {
  136. bar := pb.New(int(dur / time.Second)).Start()
  137. ticker := time.NewTicker(time.Second)
  138. defer ticker.Stop()
  139. for range ticker.C {
  140. bar.Increment()
  141. }
  142. bar.Finish()
  143. }()
  144. <-time.After(dur)
  145. close(done)
  146. group.Wait()
  147. time.Sleep(time.Millisecond * 900)
  148. }