mvcc-put.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 cmd
  15. import (
  16. "crypto/rand"
  17. "fmt"
  18. "os"
  19. "runtime/pprof"
  20. "time"
  21. "github.com/coreos/etcd/lease"
  22. "github.com/coreos/etcd/pkg/report"
  23. "github.com/spf13/cobra"
  24. )
  25. // mvccPutCmd represents a storage put performance benchmarking tool
  26. var mvccPutCmd = &cobra.Command{
  27. Use: "put",
  28. Short: "Benchmark put performance of storage",
  29. Run: mvccPutFunc,
  30. }
  31. var (
  32. mvccTotalRequests int
  33. storageKeySize int
  34. valueSize int
  35. txn bool
  36. nrTxnOps int
  37. )
  38. func init() {
  39. mvccCmd.AddCommand(mvccPutCmd)
  40. mvccPutCmd.Flags().IntVar(&mvccTotalRequests, "total", 100, "a total number of keys to put")
  41. mvccPutCmd.Flags().IntVar(&storageKeySize, "key-size", 64, "a size of key (Byte)")
  42. mvccPutCmd.Flags().IntVar(&valueSize, "value-size", 64, "a size of value (Byte)")
  43. mvccPutCmd.Flags().BoolVar(&txn, "txn", false, "put a key in transaction or not")
  44. mvccPutCmd.Flags().IntVar(&nrTxnOps, "txn-ops", 1, "a number of keys to put per transaction")
  45. // TODO: after the PR https://github.com/spf13/cobra/pull/220 is merged, the below pprof related flags should be moved to RootCmd
  46. mvccPutCmd.Flags().StringVar(&cpuProfPath, "cpuprofile", "", "the path of file for storing cpu profile result")
  47. mvccPutCmd.Flags().StringVar(&memProfPath, "memprofile", "", "the path of file for storing heap profile result")
  48. }
  49. func createBytesSlice(bytesN, sliceN int) [][]byte {
  50. rs := make([][]byte, sliceN)
  51. for i := range rs {
  52. rs[i] = make([]byte, bytesN)
  53. if _, err := rand.Read(rs[i]); err != nil {
  54. panic(err)
  55. }
  56. }
  57. return rs
  58. }
  59. func mvccPutFunc(cmd *cobra.Command, args []string) {
  60. if cpuProfPath != "" {
  61. f, err := os.Create(cpuProfPath)
  62. if err != nil {
  63. fmt.Fprintln(os.Stderr, "Failed to create a file for storing cpu profile result: ", err)
  64. os.Exit(1)
  65. }
  66. err = pprof.StartCPUProfile(f)
  67. if err != nil {
  68. fmt.Fprintln(os.Stderr, "Failed to start cpu profile: ", err)
  69. os.Exit(1)
  70. }
  71. defer pprof.StopCPUProfile()
  72. }
  73. if memProfPath != "" {
  74. f, err := os.Create(memProfPath)
  75. if err != nil {
  76. fmt.Fprintln(os.Stderr, "Failed to create a file for storing heap profile result: ", err)
  77. os.Exit(1)
  78. }
  79. defer func() {
  80. err := pprof.WriteHeapProfile(f)
  81. if err != nil {
  82. fmt.Fprintln(os.Stderr, "Failed to write heap profile result: ", err)
  83. // can do nothing for handling the error
  84. }
  85. }()
  86. }
  87. keys := createBytesSlice(storageKeySize, mvccTotalRequests*nrTxnOps)
  88. vals := createBytesSlice(valueSize, mvccTotalRequests*nrTxnOps)
  89. weight := float64(nrTxnOps)
  90. r := newWeightedReport()
  91. rrc := r.Results()
  92. rc := r.Run()
  93. if txn {
  94. for i := 0; i < mvccTotalRequests; i++ {
  95. st := time.Now()
  96. tw := s.Write()
  97. for j := i; j < i+nrTxnOps; j++ {
  98. tw.Put(keys[j], vals[j], lease.NoLease)
  99. }
  100. tw.End()
  101. rrc <- report.Result{Start: st, End: time.Now(), Weight: weight}
  102. }
  103. } else {
  104. for i := 0; i < mvccTotalRequests; i++ {
  105. st := time.Now()
  106. s.Put(keys[i], vals[i], lease.NoLease)
  107. rrc <- report.Result{Start: st, End: time.Now()}
  108. }
  109. }
  110. close(r.Results())
  111. fmt.Printf("%s", <-rc)
  112. }