wal_bench_test.go 2.2 KB

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