mvcc-put.go 3.6 KB

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