transport_bench_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. r := &countRaft{}
  29. ss := &stats.ServerStats{}
  30. ss.Initialize()
  31. tr := NewTransporter(&http.Transport{}, types.ID(1), types.ID(1), r, nil, ss, stats.NewLeaderStats("1"))
  32. srv := httptest.NewServer(tr.Handler())
  33. defer srv.Close()
  34. tr.AddPeer(types.ID(1), []string{srv.URL})
  35. defer tr.Stop()
  36. // wait for underlying stream created
  37. time.Sleep(time.Second)
  38. b.ReportAllocs()
  39. b.SetBytes(64)
  40. b.ResetTimer()
  41. data := make([]byte, 64)
  42. for i := 0; i < b.N; i++ {
  43. tr.Send([]raftpb.Message{{Type: raftpb.MsgApp, To: 1, Entries: []raftpb.Entry{{Data: data}}}})
  44. }
  45. // wait until all messages are received by the target raft
  46. for r.count() != b.N {
  47. time.Sleep(time.Millisecond)
  48. }
  49. b.StopTimer()
  50. }
  51. type countRaft struct {
  52. mu sync.Mutex
  53. cnt int
  54. }
  55. func (r *countRaft) Process(ctx context.Context, m raftpb.Message) error {
  56. r.mu.Lock()
  57. defer r.mu.Unlock()
  58. r.cnt++
  59. return nil
  60. }
  61. func (r *countRaft) ReportUnreachable(id uint64) {}
  62. func (r *countRaft) ReportSnapshot(id uint64, status raft.SnapshotStatus) {}
  63. func (r *countRaft) count() int {
  64. r.mu.Lock()
  65. defer r.mu.Unlock()
  66. return r.cnt
  67. }