transport_bench_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package rafthttp
  15. import (
  16. "net/http/httptest"
  17. "sync"
  18. "testing"
  19. "time"
  20. "github.com/coreos/etcd/etcdserver/stats"
  21. "github.com/coreos/etcd/pkg/types"
  22. "github.com/coreos/etcd/raft"
  23. "github.com/coreos/etcd/raft/raftpb"
  24. "golang.org/x/net/context"
  25. )
  26. func BenchmarkSendingMsgApp(b *testing.B) {
  27. // member 1
  28. tr := &Transport{
  29. ID: types.ID(1),
  30. ClusterID: types.ID(1),
  31. Raft: &fakeRaft{},
  32. ServerStats: newServerStats(),
  33. LeaderStats: stats.NewLeaderStats("1"),
  34. }
  35. tr.Start()
  36. srv := httptest.NewServer(tr.Handler())
  37. defer srv.Close()
  38. // member 2
  39. r := &countRaft{}
  40. tr2 := &Transport{
  41. ID: types.ID(2),
  42. ClusterID: types.ID(1),
  43. Raft: r,
  44. ServerStats: newServerStats(),
  45. LeaderStats: stats.NewLeaderStats("2"),
  46. }
  47. tr2.Start()
  48. srv2 := httptest.NewServer(tr2.Handler())
  49. defer srv2.Close()
  50. tr.AddPeer(types.ID(2), []string{srv2.URL})
  51. defer tr.Stop()
  52. tr2.AddPeer(types.ID(1), []string{srv.URL})
  53. defer tr2.Stop()
  54. if !waitStreamWorking(tr.Get(types.ID(2)).(*peer)) {
  55. b.Fatalf("stream from 1 to 2 is not in work as expected")
  56. }
  57. b.ReportAllocs()
  58. b.SetBytes(64)
  59. b.ResetTimer()
  60. data := make([]byte, 64)
  61. for i := 0; i < b.N; i++ {
  62. tr.Send([]raftpb.Message{
  63. {
  64. Type: raftpb.MsgApp,
  65. From: 1,
  66. To: 2,
  67. Index: uint64(i),
  68. Entries: []raftpb.Entry{
  69. {
  70. Index: uint64(i + 1),
  71. Data: data,
  72. },
  73. },
  74. },
  75. })
  76. }
  77. // wait until all messages are received by the target raft
  78. for r.count() != b.N {
  79. time.Sleep(time.Millisecond)
  80. }
  81. b.StopTimer()
  82. }
  83. type countRaft struct {
  84. mu sync.Mutex
  85. cnt int
  86. }
  87. func (r *countRaft) Process(ctx context.Context, m raftpb.Message) error {
  88. r.mu.Lock()
  89. defer r.mu.Unlock()
  90. r.cnt++
  91. return nil
  92. }
  93. func (r *countRaft) IsIDRemoved(id uint64) bool { return false }
  94. func (r *countRaft) ReportUnreachable(id uint64) {}
  95. func (r *countRaft) ReportSnapshot(id uint64, status raft.SnapshotStatus) {}
  96. func (r *countRaft) count() int {
  97. r.mu.Lock()
  98. defer r.mu.Unlock()
  99. return r.cnt
  100. }