main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "net"
  7. "net/http"
  8. _ "net/http/pprof"
  9. "os"
  10. "sync/atomic"
  11. "time"
  12. "go-common/library/log"
  13. xtime "go-common/library/time"
  14. )
  15. var isClient bool
  16. func init() {
  17. os.Setenv("LOG_AGENT", "tcp://127.0.0.1:8080")
  18. flag.BoolVar(&isClient, "c", false, "-c=true")
  19. }
  20. func main() {
  21. flag.Parse()
  22. if isClient {
  23. go func() {
  24. fmt.Println(http.ListenAndServe("localhost:6060", nil))
  25. }()
  26. log.Init(&log.Config{
  27. Stdout: false,
  28. Agent: &log.AgentConfig{
  29. Proto: "tcp",
  30. Addr: "127.0.0.1:8080",
  31. Chan: 2048,
  32. Buffer: 1,
  33. Timeout: xtime.Duration(time.Second),
  34. }})
  35. for i := 0; i < 3; i++ {
  36. go func() {
  37. arg := `area:"reply"
  38. message:"\345\233\236\345\244\215 @\347\231\276\350\215\211\345\221\263\346\235\245\344\274\212\344\273\275 :\345\223\210\357\274\214\344\275\240\345\260\261\345\217\252\347\234\213\345\210\260\344\272\206pick me up\346\262\241\347\234\213\345\210\260\346\222\236\357\274\237\347\262\211\344\270\235\350\247\243\351\207\212\344\272\206\346\227\240\346\225\260\351\201\215\344\273\245\345\211\215\351\202\243\344\270\252\345\245\263\345\233\242\344\270\200\344\270\252\346\234\210\345\260\261\346\262\241\345\255\246\345\225\245\350\210\236\357\274\214\347\277\273\346\235\245\350\246\206\345\216\273\350\267\263\357\274\214\345\217\202\345\212\240101\347\232\204\346\227\266\345\200\2313\345\244\251\344\270\200\344\270\252\350\210\236\350\277\230\345\234\250\350\277\231\350\257\264\343\200\202\346\234\215\344\272\206"
  39. id:1075824609 oid:31813039 mid:76839481 `
  40. for {
  41. log.Infov(context.Background(),
  42. log.KV("user", "test_user"),
  43. log.KV("ip", "127.0.0.1:8080"),
  44. log.KV("path", "127.0.0.1:8080/test_user"),
  45. log.KV("ret", 0),
  46. log.KV("args", arg),
  47. log.KV("stack", "nil"),
  48. log.KV("error", ""),
  49. log.KV("ts", time.Second.Seconds()),
  50. )
  51. }
  52. }()
  53. }
  54. time.Sleep(time.Hour)
  55. } else {
  56. startServer()
  57. }
  58. }
  59. func startServer() {
  60. go calcuQPS()
  61. lis, err := net.Listen("tcp", ":8080")
  62. if err != nil {
  63. panic(err)
  64. }
  65. for {
  66. conn, err := lis.Accept()
  67. if err != nil {
  68. return
  69. }
  70. go serve(conn)
  71. }
  72. }
  73. var total int64
  74. func calcuQPS() {
  75. var lastTotal int64
  76. var lastUpdated time.Time
  77. for {
  78. time.Sleep(time.Second * 3)
  79. t := atomic.LoadInt64(&total)
  80. n := time.Now()
  81. change := t - lastTotal
  82. gap := n.Sub(lastUpdated)
  83. fmt.Println("bw:", change/(gap.Nanoseconds()/1e7))
  84. lastTotal = t
  85. lastUpdated = n
  86. }
  87. }
  88. func serve(conn net.Conn) {
  89. p := make([]byte, 4096)
  90. for {
  91. nRead, err := conn.Read(p)
  92. if err != nil {
  93. conn.Close()
  94. break
  95. }
  96. atomic.AddInt64(&total, int64(nRead))
  97. }
  98. fmt.Println(string(p))
  99. }