storage-put.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2015 Nippon Telegraph and Telephone Corporation.
  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/Godeps/_workspace/src/github.com/spf13/cobra"
  22. "github.com/coreos/etcd/lease"
  23. )
  24. // storagePutCmd represents a storage put performance benchmarking tool
  25. var storagePutCmd = &cobra.Command{
  26. Use: "put",
  27. Short: "Benchmark put performance of storage",
  28. Run: storagePutFunc,
  29. }
  30. var (
  31. totalNrKeys int
  32. storageKeySize int
  33. valueSize int
  34. txn bool
  35. )
  36. func init() {
  37. storageCmd.AddCommand(storagePutCmd)
  38. storagePutCmd.Flags().IntVar(&totalNrKeys, "total", 100, "a total number of keys to put")
  39. storagePutCmd.Flags().IntVar(&storageKeySize, "key-size", 64, "a size of key (Byte)")
  40. storagePutCmd.Flags().IntVar(&valueSize, "value-size", 64, "a size of value (Byte)")
  41. storagePutCmd.Flags().BoolVar(&txn, "txn", false, "put a key in transaction or not")
  42. // TODO: after the PR https://github.com/spf13/cobra/pull/220 is merged, the below pprof related flags should be moved to RootCmd
  43. storagePutCmd.Flags().StringVar(&cpuProfPath, "cpuprofile", "", "the path of file for storing cpu profile result")
  44. storagePutCmd.Flags().StringVar(&memProfPath, "memprofile", "", "the path of file for storing heap profile result")
  45. }
  46. func createBytesSlice(bytesN, sliceN int) [][]byte {
  47. rs := make([][]byte, sliceN)
  48. for i := range rs {
  49. rs[i] = make([]byte, bytesN)
  50. if _, err := rand.Read(rs[i]); err != nil {
  51. panic(err)
  52. }
  53. }
  54. return rs
  55. }
  56. func storagePutFunc(cmd *cobra.Command, args []string) {
  57. if cpuProfPath != "" {
  58. f, err := os.Create(cpuProfPath)
  59. if err != nil {
  60. fmt.Fprintln(os.Stderr, "Failed to create a file for storing cpu profile result: ", err)
  61. os.Exit(1)
  62. }
  63. err = pprof.StartCPUProfile(f)
  64. if err != nil {
  65. fmt.Fprintln(os.Stderr, "Failed to start cpu profile: ", err)
  66. os.Exit(1)
  67. }
  68. defer pprof.StopCPUProfile()
  69. }
  70. if memProfPath != "" {
  71. f, err := os.Create(memProfPath)
  72. if err != nil {
  73. fmt.Fprintln(os.Stderr, "Failed to create a file for storing heap profile result: ", err)
  74. os.Exit(1)
  75. }
  76. defer func() {
  77. err := pprof.WriteHeapProfile(f)
  78. if err != nil {
  79. fmt.Fprintln(os.Stderr, "Failed to write heap profile result: ", err)
  80. // can do nothing for handling the error
  81. }
  82. }()
  83. }
  84. keys := createBytesSlice(storageKeySize, totalNrKeys)
  85. vals := createBytesSlice(valueSize, totalNrKeys)
  86. latencies := make([]time.Duration, totalNrKeys)
  87. minLat := time.Duration(1<<63 - 1)
  88. maxLat := time.Duration(0)
  89. for i := 0; i < totalNrKeys; i++ {
  90. begin := time.Now()
  91. if txn {
  92. id := s.TxnBegin()
  93. if _, err := s.TxnPut(id, keys[i], vals[i], lease.NoLease); err != nil {
  94. fmt.Fprintln(os.Stderr, "txn put error:", err)
  95. os.Exit(1)
  96. }
  97. s.TxnEnd(id)
  98. } else {
  99. s.Put(keys[i], vals[i], lease.NoLease)
  100. }
  101. end := time.Now()
  102. lat := end.Sub(begin)
  103. latencies[i] = lat
  104. if maxLat < lat {
  105. maxLat = lat
  106. }
  107. if lat < minLat {
  108. minLat = lat
  109. }
  110. }
  111. total := time.Duration(0)
  112. for _, lat := range latencies {
  113. total += lat
  114. }
  115. fmt.Printf("total: %v\n", total)
  116. fmt.Printf("average: %v\n", total/time.Duration(totalNrKeys))
  117. fmt.Printf("rate: %4.4f\n", float64(totalNrKeys)/total.Seconds())
  118. fmt.Printf("minimum latency: %v\n", minLat)
  119. fmt.Printf("maximum latency: %v\n", maxLat)
  120. // TODO: Currently this benchmark doesn't use the common histogram infrastructure.
  121. // This is because an accuracy of the infrastructure isn't suitable for measuring
  122. // performance of kv storage:
  123. // https://github.com/coreos/etcd/pull/4070#issuecomment-167954149
  124. }