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