main.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package main
  2. // Start Commond eg: ./push_room test://test_room 10 100 127.0.0.1:7831
  3. // first parameter: room id
  4. // second parameter: routine count
  5. // third parameter: running time
  6. // fourth parameter: service server ip
  7. import (
  8. "context"
  9. "fmt"
  10. "net/url"
  11. "os"
  12. "strconv"
  13. "time"
  14. bm "go-common/library/net/http/blademaster"
  15. "go-common/library/net/netutil/breaker"
  16. xtime "go-common/library/time"
  17. )
  18. var (
  19. httpClient *bm.Client
  20. )
  21. const TestContent = "{\"test\":\"test push room\"}"
  22. func init() {
  23. httpClient = bm.NewClient(&bm.ClientConfig{
  24. App: &bm.App{
  25. Key: "6aa4286456d16b97",
  26. Secret: "test",
  27. },
  28. Dial: xtime.Duration(time.Second),
  29. Timeout: xtime.Duration(time.Second),
  30. KeepAlive: xtime.Duration(time.Second * 10),
  31. Breaker: &breaker.Config{
  32. Window: xtime.Duration(time.Second),
  33. Sleep: xtime.Duration(time.Second),
  34. Bucket: 10,
  35. Ratio: 0.8,
  36. Request: 100,
  37. SwitchOff: false,
  38. },
  39. })
  40. }
  41. func main() {
  42. rountineNum, err := strconv.Atoi(os.Args[2])
  43. if err != nil {
  44. panic(err)
  45. }
  46. t, err := strconv.Atoi(os.Args[3])
  47. if err != nil {
  48. panic(err)
  49. }
  50. addr := os.Args[4]
  51. time.AfterFunc(time.Duration(t)*time.Second, stop)
  52. gap := time.Second / time.Duration(rountineNum)
  53. delay := time.Duration(0)
  54. go run(addr, time.Duration(0)*time.Second)
  55. for i := 0; i < rountineNum-1; i++ {
  56. go run(addr, delay)
  57. delay += gap
  58. fmt.Println("delay:", delay)
  59. }
  60. time.Sleep(9999 * time.Hour)
  61. }
  62. func run(addr string, delay time.Duration) {
  63. time.Sleep(delay)
  64. i := int64(0)
  65. for {
  66. go post(addr, i)
  67. time.Sleep(time.Second)
  68. i++
  69. }
  70. }
  71. func stop() {
  72. os.Exit(-1)
  73. }
  74. func post(addr string, i int64) {
  75. params := url.Values{}
  76. params.Set("room", os.Args[1])
  77. params.Set("operation", "9")
  78. params.Set("message", TestContent)
  79. if err := httpClient.Get(context.Background(), "http://"+addr+"/x/internal/broadcast/push/room", "", params, nil); err != nil {
  80. fmt.Printf("Error: bm.post() error(%s)\n", err.Error())
  81. return
  82. }
  83. }