go.etcd.io/etcd@v3.3.27+incompatible/wal/wal_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 wal
    16  
    17  import (
    18  	"io/ioutil"
    19  	"os"
    20  	"testing"
    21  
    22  	"github.com/coreos/etcd/raft/raftpb"
    23  )
    24  
    25  func BenchmarkWrite100EntryWithoutBatch(b *testing.B) { benchmarkWriteEntry(b, 100, 0) }
    26  func BenchmarkWrite100EntryBatch10(b *testing.B)      { benchmarkWriteEntry(b, 100, 10) }
    27  func BenchmarkWrite100EntryBatch100(b *testing.B)     { benchmarkWriteEntry(b, 100, 100) }
    28  func BenchmarkWrite100EntryBatch500(b *testing.B)     { benchmarkWriteEntry(b, 100, 500) }
    29  func BenchmarkWrite100EntryBatch1000(b *testing.B)    { benchmarkWriteEntry(b, 100, 1000) }
    30  
    31  func BenchmarkWrite1000EntryWithoutBatch(b *testing.B) { benchmarkWriteEntry(b, 1000, 0) }
    32  func BenchmarkWrite1000EntryBatch10(b *testing.B)      { benchmarkWriteEntry(b, 1000, 10) }
    33  func BenchmarkWrite1000EntryBatch100(b *testing.B)     { benchmarkWriteEntry(b, 1000, 100) }
    34  func BenchmarkWrite1000EntryBatch500(b *testing.B)     { benchmarkWriteEntry(b, 1000, 500) }
    35  func BenchmarkWrite1000EntryBatch1000(b *testing.B)    { benchmarkWriteEntry(b, 1000, 1000) }
    36  
    37  func benchmarkWriteEntry(b *testing.B, size int, batch int) {
    38  	p, err := ioutil.TempDir(os.TempDir(), "waltest")
    39  	if err != nil {
    40  		b.Fatal(err)
    41  	}
    42  	defer os.RemoveAll(p)
    43  
    44  	w, err := Create(p, []byte("somedata"))
    45  	if err != nil {
    46  		b.Fatalf("err = %v, want nil", err)
    47  	}
    48  	data := make([]byte, size)
    49  	for i := 0; i < size; i++ {
    50  		data[i] = byte(i)
    51  	}
    52  	e := &raftpb.Entry{Data: data}
    53  
    54  	b.ResetTimer()
    55  	n := 0
    56  	b.SetBytes(int64(e.Size()))
    57  	for i := 0; i < b.N; i++ {
    58  		err := w.saveEntry(e)
    59  		if err != nil {
    60  			b.Fatal(err)
    61  		}
    62  		n++
    63  		if n > batch {
    64  			w.sync()
    65  			n = 0
    66  		}
    67  	}
    68  }