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