wal_bench_test.go 2.1 KB

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