storage-put.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "time"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
  21. )
  22. // storagePutCmd represents a storage put performance benchmarking tool
  23. var storagePutCmd = &cobra.Command{
  24. Use: "put",
  25. Short: "Benchmark put performance of storage",
  26. Run: storagePutFunc,
  27. }
  28. var (
  29. totalNrKeys int
  30. storageKeySize int
  31. valueSize int
  32. txn bool
  33. )
  34. func init() {
  35. storageCmd.AddCommand(storagePutCmd)
  36. storagePutCmd.Flags().IntVar(&totalNrKeys, "total", 100, "a total number of keys to put")
  37. storagePutCmd.Flags().IntVar(&storageKeySize, "key-size", 64, "a size of key (Byte)")
  38. storagePutCmd.Flags().IntVar(&valueSize, "value-size", 64, "a size of value (Byte)")
  39. storagePutCmd.Flags().BoolVar(&txn, "txn", false, "put a key in transaction or not")
  40. }
  41. func createBytesSlice(bytesN, sliceN int) [][]byte {
  42. rs := make([][]byte, sliceN)
  43. for i := range rs {
  44. rs[i] = make([]byte, bytesN)
  45. if _, err := rand.Read(rs[i]); err != nil {
  46. panic(err)
  47. }
  48. }
  49. return rs
  50. }
  51. func storagePutFunc(cmd *cobra.Command, args []string) {
  52. keys := createBytesSlice(storageKeySize, totalNrKeys)
  53. vals := createBytesSlice(valueSize, totalNrKeys)
  54. latencies := make([]time.Duration, totalNrKeys)
  55. minLat := time.Duration(1<<63 - 1)
  56. maxLat := time.Duration(0)
  57. for i := 0; i < totalNrKeys; i++ {
  58. begin := time.Now()
  59. if txn {
  60. id := s.TxnBegin()
  61. if _, err := s.TxnPut(id, keys[i], vals[i]); err != nil {
  62. fmt.Errorf("txn put error: %v", err)
  63. os.Exit(1)
  64. }
  65. s.TxnEnd(id)
  66. } else {
  67. s.Put(keys[i], vals[i])
  68. }
  69. end := time.Now()
  70. lat := end.Sub(begin)
  71. latencies[i] = lat
  72. if maxLat < lat {
  73. maxLat = lat
  74. }
  75. if lat < minLat {
  76. minLat = lat
  77. }
  78. }
  79. total := time.Duration(0)
  80. for _, lat := range latencies {
  81. total += lat
  82. }
  83. fmt.Printf("total: %v\n", total)
  84. fmt.Printf("average: %v\n", total/time.Duration(totalNrKeys))
  85. fmt.Printf("rate: %4.4f\n", float64(totalNrKeys)/total.Seconds())
  86. fmt.Printf("minimum latency: %v\n", minLat)
  87. fmt.Printf("maximum latency: %v\n", maxLat)
  88. // TODO: Currently this benchmark doesn't use the common histogram infrastructure.
  89. // This is because an accuracy of the infrastructure isn't suitable for measuring
  90. // performance of kv storage:
  91. // https://github.com/coreos/etcd/pull/4070#issuecomment-167954149
  92. }