txn_put.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2017 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. "context"
  17. "encoding/binary"
  18. "fmt"
  19. "math"
  20. "os"
  21. "time"
  22. v3 "go.etcd.io/etcd/clientv3"
  23. "go.etcd.io/etcd/pkg/report"
  24. "github.com/spf13/cobra"
  25. "golang.org/x/time/rate"
  26. "gopkg.in/cheggaaa/pb.v1"
  27. )
  28. // txnPutCmd represents the txnPut command
  29. var txnPutCmd = &cobra.Command{
  30. Use: "txn-put",
  31. Short: "Benchmark txn-put",
  32. Run: txnPutFunc,
  33. }
  34. var (
  35. txnPutTotal int
  36. txnPutRate int
  37. txnPutOpsPerTxn int
  38. )
  39. func init() {
  40. RootCmd.AddCommand(txnPutCmd)
  41. txnPutCmd.Flags().IntVar(&keySize, "key-size", 8, "Key size of txn put")
  42. txnPutCmd.Flags().IntVar(&valSize, "val-size", 8, "Value size of txn put")
  43. txnPutCmd.Flags().IntVar(&txnPutOpsPerTxn, "txn-ops", 1, "Number of puts per txn")
  44. txnPutCmd.Flags().IntVar(&txnPutRate, "rate", 0, "Maximum txns per second (0 is no limit)")
  45. txnPutCmd.Flags().IntVar(&txnPutTotal, "total", 10000, "Total number of txn requests")
  46. txnPutCmd.Flags().IntVar(&keySpaceSize, "key-space-size", 1, "Maximum possible keys")
  47. }
  48. func txnPutFunc(cmd *cobra.Command, args []string) {
  49. if keySpaceSize <= 0 {
  50. fmt.Fprintf(os.Stderr, "expected positive --key-space-size, got (%v)", keySpaceSize)
  51. os.Exit(1)
  52. }
  53. requests := make(chan []v3.Op, totalClients)
  54. if txnPutRate == 0 {
  55. txnPutRate = math.MaxInt32
  56. }
  57. limit := rate.NewLimiter(rate.Limit(txnPutRate), 1)
  58. clients := mustCreateClients(totalClients, totalConns)
  59. k, v := make([]byte, keySize), string(mustRandBytes(valSize))
  60. bar = pb.New(txnPutTotal)
  61. bar.Format("Bom !")
  62. bar.Start()
  63. r := newReport()
  64. for i := range clients {
  65. wg.Add(1)
  66. go func(c *v3.Client) {
  67. defer wg.Done()
  68. for ops := range requests {
  69. limit.Wait(context.Background())
  70. st := time.Now()
  71. _, err := c.Txn(context.TODO()).Then(ops...).Commit()
  72. r.Results() <- report.Result{Err: err, Start: st, End: time.Now()}
  73. bar.Increment()
  74. }
  75. }(clients[i])
  76. }
  77. go func() {
  78. for i := 0; i < txnPutTotal; i++ {
  79. ops := make([]v3.Op, txnPutOpsPerTxn)
  80. for j := 0; j < txnPutOpsPerTxn; j++ {
  81. binary.PutVarint(k, int64(((i*txnPutOpsPerTxn)+j)%keySpaceSize))
  82. ops[j] = v3.OpPut(string(k), v)
  83. }
  84. requests <- ops
  85. }
  86. close(requests)
  87. }()
  88. rc := r.Run()
  89. wg.Wait()
  90. close(r.Results())
  91. bar.Finish()
  92. fmt.Println(<-rc)
  93. }