main.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package main
  2. import (
  3. "log"
  4. "rocommon"
  5. "rocommon/socket"
  6. _ "rocommon/socket/tcp"
  7. "roserver/serverproto"
  8. _ "roserver/test/model"
  9. "sync"
  10. "time"
  11. )
  12. var (
  13. lock sync.Mutex
  14. totalConnNum int = 0
  15. )
  16. const maxConnNum = 1
  17. func client() {
  18. queue := rocommon.NewEventQueue()
  19. queue.StartQueue()
  20. node := socket.NewServerNode("tcpConnector", "client", "127.0.0.1:21001", queue)
  21. rv := rocommon.NewNetSyncRecv(node)
  22. socket.SetProcessorRPC(node, "tcp.demo", rv.EventCB())
  23. node.(rocommon.TCPConnector).SetReconnectTime(5 * time.Second)
  24. node.Start()
  25. rv.WaitMsg(rocommon.SessionConnected{})
  26. lock.Lock()
  27. totalConnNum++
  28. if totalConnNum >= maxConnNum {
  29. log.Print("totalConnNum:", totalConnNum)
  30. }
  31. lock.Unlock()
  32. node.(rocommon.TCPConnector).Session().Send(&serverproto.CSPingReq{
  33. OpenId: "hello",
  34. })
  35. begin := time.Now()
  36. lastCheck := begin
  37. totalCount := 0
  38. respTime := time.Now()
  39. var avgTime float64 = 0
  40. const total = 100000 * time.Second
  41. for {
  42. now := time.Now()
  43. if now.Sub(begin) >= total {
  44. break
  45. }
  46. if now.Sub(lastCheck) >= 20*time.Second {
  47. //log.Printf("progress count pid[%v]: %v %v",syscall.Getpid(), totalCount, now.Sub(begin) * 100 / total)
  48. lastCheck = now
  49. log.Printf("count:%v, avtTime:%v\n", totalCount, avgTime/float64(totalCount)/1000.0/1000.0)
  50. totalCount = 0
  51. avgTime = 0
  52. }
  53. rv.Recv(func(ev rocommon.ProcEvent) {
  54. totalCount++
  55. if totalCount > 1 {
  56. recvTime := time.Now()
  57. avgTime += float64(recvTime.Sub(respTime).Nanoseconds())
  58. //log.Println("respTime:", recvTime.Sub(respTime))
  59. respTime = recvTime
  60. }
  61. time.Sleep(200 * time.Millisecond)
  62. respTime = time.Now()
  63. ev.Session().Send(&serverproto.CSPingReq{
  64. OpenId: "hello",
  65. })
  66. })
  67. }
  68. }
  69. func main() {
  70. for i := 0; i < maxConnNum-1; i++ {
  71. go client()
  72. }
  73. client()
  74. }