node_bench_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. "context"
  17. "testing"
  18. "time"
  19. )
  20. func BenchmarkOneNode(b *testing.B) {
  21. ctx, cancel := context.WithCancel(context.Background())
  22. defer cancel()
  23. s := NewMemoryStorage()
  24. rn := newTestRawNode(1, []uint64{1}, 10, 1, s)
  25. n := newNode(rn)
  26. go n.run()
  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. }