node_bench_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 raft
  15. import (
  16. "testing"
  17. "time"
  18. "golang.org/x/net/context"
  19. )
  20. func BenchmarkOneNode(b *testing.B) {
  21. ctx, cancel := context.WithCancel(context.Background())
  22. defer cancel()
  23. n := newNode()
  24. s := NewMemoryStorage()
  25. r := newTestRaft(1, []uint64{1}, 10, 1, s)
  26. go n.run(r)
  27. defer n.Stop()
  28. n.Campaign(ctx)
  29. go func() {
  30. for i := 0; i < b.N; i++ {
  31. n.Propose(ctx, []byte("foo"))
  32. }
  33. }()
  34. for {
  35. rd := <-n.Ready()
  36. s.Append(rd.Entries)
  37. // a reasonable disk sync latency
  38. time.Sleep(1 * time.Millisecond)
  39. n.Advance()
  40. if rd.HardState.Commit == uint64(b.N+1) {
  41. return
  42. }
  43. }
  44. }