transport_bench_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2015 CoreOS, Inc.
  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"
  17. "net/http/httptest"
  18. "sync"
  19. "testing"
  20. "time"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  22. "github.com/coreos/etcd/etcdserver/stats"
  23. "github.com/coreos/etcd/pkg/types"
  24. "github.com/coreos/etcd/raft"
  25. "github.com/coreos/etcd/raft/raftpb"
  26. )
  27. func BenchmarkSendingMsgApp(b *testing.B) {
  28. // member 1
  29. tr := NewTransporter(&http.Transport{}, types.ID(1), types.ID(1), &fakeRaft{}, nil, newServerStats(), stats.NewLeaderStats("1"))
  30. srv := httptest.NewServer(tr.Handler())
  31. defer srv.Close()
  32. // member 2
  33. r := &countRaft{}
  34. tr2 := NewTransporter(&http.Transport{}, types.ID(2), types.ID(1), r, nil, newServerStats(), stats.NewLeaderStats("2"))
  35. srv2 := httptest.NewServer(tr2.Handler())
  36. defer srv2.Close()
  37. tr.AddPeer(types.ID(2), []string{srv2.URL})
  38. defer tr.Stop()
  39. tr2.AddPeer(types.ID(1), []string{srv.URL})
  40. defer tr2.Stop()
  41. if !waitStreamWorking(tr.(*transport).Get(types.ID(2)).(*peer)) {
  42. b.Fatalf("stream from 1 to 2 is not in work as expected")
  43. }
  44. b.ReportAllocs()
  45. b.SetBytes(64)
  46. b.ResetTimer()
  47. data := make([]byte, 64)
  48. for i := 0; i < b.N; i++ {
  49. tr.Send([]raftpb.Message{
  50. {
  51. Type: raftpb.MsgApp,
  52. From: 1,
  53. To: 2,
  54. Index: uint64(i),
  55. Entries: []raftpb.Entry{
  56. {
  57. Index: uint64(i + 1),
  58. Data: data,
  59. },
  60. },
  61. },
  62. })
  63. }
  64. // wait until all messages are received by the target raft
  65. for r.count() != b.N {
  66. time.Sleep(time.Millisecond)
  67. }
  68. b.StopTimer()
  69. }
  70. type countRaft struct {
  71. mu sync.Mutex
  72. cnt int
  73. }
  74. func (r *countRaft) Process(ctx context.Context, m raftpb.Message) error {
  75. r.mu.Lock()
  76. defer r.mu.Unlock()
  77. r.cnt++
  78. return nil
  79. }
  80. func (r *countRaft) IsIDRemoved(id uint64) bool { return false }
  81. func (r *countRaft) ReportUnreachable(id uint64) {}
  82. func (r *countRaft) ReportSnapshot(id uint64, status raft.SnapshotStatus) {}
  83. func (r *countRaft) count() int {
  84. r.mu.Lock()
  85. defer r.mu.Unlock()
  86. return r.cnt
  87. }