mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00

It has always bugged me that any new feature essentially needed to be tested twice due to the two ways in which apps can use raft (`*node` and `*RawNode`). Due to upcoming testing work for joint consensus, now is a good time to rectify this somewhat. This commit removes most logic from `(*node).run` and uses `*RawNode` internally. This simplifies the logic and also lead (via debugging) to some insight on how the semantics of the approaches differ, which is now documented in the comments.
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
// Copyright 2015 The etcd Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package raft
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func BenchmarkOneNode(b *testing.B) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
n := newNode()
|
|
s := NewMemoryStorage()
|
|
rn := newTestRawNode(1, []uint64{1}, 10, 1, s)
|
|
go n.run(rn)
|
|
|
|
defer n.Stop()
|
|
|
|
n.Campaign(ctx)
|
|
go func() {
|
|
for i := 0; i < b.N; i++ {
|
|
n.Propose(ctx, []byte("foo"))
|
|
}
|
|
}()
|
|
|
|
for {
|
|
rd := <-n.Ready()
|
|
s.Append(rd.Entries)
|
|
// a reasonable disk sync latency
|
|
time.Sleep(1 * time.Millisecond)
|
|
n.Advance()
|
|
if rd.HardState.Commit == uint64(b.N+1) {
|
|
return
|
|
}
|
|
}
|
|
}
|