mvcc-put.go 3.3 KB

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