storage-put.go 3.0 KB

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