echomtf.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package main
  2. import "lmaxapi"
  3. import "log"
  4. import "time"
  5. import "runtime"
  6. import "runtime/pprof"
  7. import "os"
  8. import "sync/atomic"
  9. func main() {
  10. runtime.GOMAXPROCS(4)
  11. w, err := os.Create("me.pprof")
  12. if err != nil {
  13. panic(err)
  14. }
  15. pprof.StartCPUProfile(w)
  16. defer pprof.StopCPUProfile()
  17. testMtfEchoSync(1000000)
  18. //testMtfEcho(1000000)
  19. }
  20. func testMtfEcho(N int) {
  21. now := time.Now()
  22. mtf := lmaxapi.NewMtf(nil, nil)
  23. go mtf.Start()
  24. client := lmaxapi.NewMtfClient(mtf, time.Second, false)
  25. k := int64(N)
  26. done := make(chan int, 1)
  27. go client.Recv(func(msg *lmaxapi.Message) {
  28. v := atomic.AddInt64(&k, -1)
  29. if v == 0 {
  30. log.Println("recv message end")
  31. done <- 1
  32. }
  33. })
  34. err := client.ConnectPrivate()
  35. if err != nil {
  36. panic(err)
  37. }
  38. // log.Println("ready")
  39. i := 0
  40. for i = 0; i < N; i++ {
  41. client.SendMessageAsyn(lmaxapi.MsgEcho, "echo")
  42. }
  43. <-done
  44. err = client.Close()
  45. if err != nil {
  46. log.Println(err)
  47. return
  48. }
  49. mtf.Close()
  50. // log.Println(time.Now().Sub(now), i)
  51. }
  52. func testMtfEchoSync(N int) {
  53. now := time.Now()
  54. mtf := lmaxapi.NewMtf(nil, nil)
  55. go mtf.Start()
  56. client := lmaxapi.NewMtfClient(mtf, 100*time.Millisecond, false)
  57. go client.Recv(func(msg *lmaxapi.Message) {
  58. log.Println("err message", msg)
  59. })
  60. err := client.ConnectPrivate()
  61. if err != nil {
  62. panic(err)
  63. }
  64. // log.Println("ready")
  65. i := 0
  66. for i = 0; i < N; i++ {
  67. client.SendMessage(lmaxapi.MsgEcho, "echo")
  68. }
  69. err = client.Close()
  70. if err != nil {
  71. log.Println(err)
  72. return
  73. }
  74. mtf.Close()
  75. // log.Println(time.Now().Sub(now), i)
  76. }