node_bench_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 raft
  15. import (
  16. "testing"
  17. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  18. )
  19. func BenchmarkOneNode(b *testing.B) {
  20. ctx, cancel := context.WithCancel(context.Background())
  21. defer cancel()
  22. n := newNode()
  23. s := NewMemoryStorage()
  24. r := newTestRaft(1, []uint64{1}, 10, 1, s)
  25. go n.run(r)
  26. defer n.Stop()
  27. n.Campaign(ctx)
  28. for i := 0; i < b.N; i++ {
  29. rd := <-n.Ready()
  30. s.Append(rd.Entries)
  31. n.Advance()
  32. n.Propose(ctx, []byte("foo"))
  33. }
  34. rd := <-n.Ready()
  35. if rd.HardState.Commit != uint64(b.N+1) {
  36. b.Errorf("commit = %d, want %d", rd.HardState.Commit, b.N+1)
  37. }
  38. }