go.etcd.io/etcd@v3.3.27+incompatible/raft/rafttest/node_bench_test.go (about)

     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  
    15  package rafttest
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/coreos/etcd/raft"
    23  )
    24  
    25  func BenchmarkProposal3Nodes(b *testing.B) {
    26  	peers := []raft.Peer{{1, nil}, {2, nil}, {3, nil}}
    27  	nt := newRaftNetwork(1, 2, 3)
    28  
    29  	nodes := make([]*node, 0)
    30  
    31  	for i := 1; i <= 3; i++ {
    32  		n := startNode(uint64(i), peers, nt.nodeNetwork(uint64(i)))
    33  		nodes = append(nodes, n)
    34  	}
    35  	// get ready and warm up
    36  	time.Sleep(50 * time.Millisecond)
    37  
    38  	b.ResetTimer()
    39  	for i := 0; i < b.N; i++ {
    40  		nodes[0].Propose(context.TODO(), []byte("somedata"))
    41  	}
    42  
    43  	for _, n := range nodes {
    44  		if n.state.Commit != uint64(b.N+4) {
    45  			continue
    46  		}
    47  	}
    48  	b.StopTimer()
    49  
    50  	for _, n := range nodes {
    51  		n.stop()
    52  	}
    53  }