wal_bench_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2015 CoreOS, Inc.
  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 < len(data); i++ {
  43. data[i] = byte(i)
  44. }
  45. e := &raftpb.Entry{Data: data}
  46. b.ResetTimer()
  47. n := 0
  48. for i := 0; i < b.N; i++ {
  49. err := w.saveEntry(e)
  50. if err != nil {
  51. b.Fatal(err)
  52. }
  53. n++
  54. if n > batch {
  55. w.sync()
  56. n = 0
  57. }
  58. }
  59. }