|
| 1 | +// Copyright 2021 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 | + |
| 15 | +package cmd |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "encoding/binary" |
| 20 | + "fmt" |
| 21 | + "math" |
| 22 | + "math/rand" |
| 23 | + "os" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/spf13/cobra" |
| 27 | + "golang.org/x/time/rate" |
| 28 | + "gopkg.in/cheggaaa/pb.v1" |
| 29 | + |
| 30 | + v3 "go.etcd.io/etcd/client/v3" |
| 31 | + "go.etcd.io/etcd/pkg/v3/report" |
| 32 | +) |
| 33 | + |
| 34 | +// mixeTxnCmd represents the mixedTxn command |
| 35 | +var mixedTxnCmd = &cobra.Command{ |
| 36 | + Use: "txn-mixed key [end-range]", |
| 37 | + Short: "Benchmark a mixed load of txn-put & txn-range.", |
| 38 | + |
| 39 | + Run: mixedTxnFunc, |
| 40 | +} |
| 41 | + |
| 42 | +var ( |
| 43 | + mixedTxnTotal int |
| 44 | + mixedTxnRate int |
| 45 | + mixedTxnReadWriteRatio float64 |
| 46 | + mixedTxnRangeLimit int64 |
| 47 | + mixedTxnEndKey string |
| 48 | + |
| 49 | + writeOpsTotal uint64 |
| 50 | + readOpsTotal uint64 |
| 51 | +) |
| 52 | + |
| 53 | +func init() { |
| 54 | + RootCmd.AddCommand(mixedTxnCmd) |
| 55 | + mixedTxnCmd.Flags().IntVar(&keySize, "key-size", 8, "Key size of mixed txn") |
| 56 | + mixedTxnCmd.Flags().IntVar(&valSize, "val-size", 8, "Value size of mixed txn") |
| 57 | + mixedTxnCmd.Flags().IntVar(&mixedTxnRate, "rate", 0, "Maximum txns per second (0 is no limit)") |
| 58 | + mixedTxnCmd.Flags().IntVar(&mixedTxnTotal, "total", 10000, "Total number of txn requests") |
| 59 | + mixedTxnCmd.Flags().StringVar(&mixedTxnEndKey, "end-key", "", |
| 60 | + "Read operation range end key. By default, we do full range query with the default limit of 1000.") |
| 61 | + mixedTxnCmd.Flags().Int64Var(&mixedTxnRangeLimit, "limit", 1000, "Read operation range result limit") |
| 62 | + mixedTxnCmd.Flags().IntVar(&keySpaceSize, "key-space-size", 1, "Maximum possible keys") |
| 63 | + mixedTxnCmd.Flags().StringVar(&rangeConsistency, "consistency", "l", "Linearizable(l) or Serializable(s)") |
| 64 | + mixedTxnCmd.Flags().Float64Var(&mixedTxnReadWriteRatio, "rw-ratio", 1, "Read/write ops ratio") |
| 65 | +} |
| 66 | + |
| 67 | +type request struct { |
| 68 | + isWrite bool |
| 69 | + op v3.Op |
| 70 | +} |
| 71 | + |
| 72 | +func mixedTxnFunc(cmd *cobra.Command, _ []string) { |
| 73 | + if keySpaceSize <= 0 { |
| 74 | + fmt.Fprintf(os.Stderr, "expected positive --key-space-size, got (%v)", keySpaceSize) |
| 75 | + os.Exit(1) |
| 76 | + } |
| 77 | + |
| 78 | + if rangeConsistency == "l" { |
| 79 | + fmt.Println("bench with linearizable range") |
| 80 | + } else if rangeConsistency == "s" { |
| 81 | + fmt.Println("bench with serializable range") |
| 82 | + } else { |
| 83 | + fmt.Fprintln(os.Stderr, cmd.Usage()) |
| 84 | + os.Exit(1) |
| 85 | + } |
| 86 | + |
| 87 | + requests := make(chan request, totalClients) |
| 88 | + if mixedTxnRate == 0 { |
| 89 | + mixedTxnRate = math.MaxInt32 |
| 90 | + } |
| 91 | + limit := rate.NewLimiter(rate.Limit(mixedTxnRate), 1) |
| 92 | + clients := mustCreateClients(totalClients, totalConns) |
| 93 | + k, v := make([]byte, keySize), string(mustRandBytes(valSize)) |
| 94 | + |
| 95 | + bar = pb.New(mixedTxnTotal) |
| 96 | + bar.Start() |
| 97 | + |
| 98 | + reportRead := newReport() |
| 99 | + reportWrite := newReport() |
| 100 | + for i := range clients { |
| 101 | + wg.Add(1) |
| 102 | + go func(c *v3.Client) { |
| 103 | + defer wg.Done() |
| 104 | + for req := range requests { |
| 105 | + limit.Wait(context.Background()) |
| 106 | + st := time.Now() |
| 107 | + _, err := c.Txn(context.TODO()).Then(req.op).Commit() |
| 108 | + if req.isWrite { |
| 109 | + reportWrite.Results() <- report.Result{Err: err, Start: st, End: time.Now()} |
| 110 | + } else { |
| 111 | + reportRead.Results() <- report.Result{Err: err, Start: st, End: time.Now()} |
| 112 | + } |
| 113 | + bar.Increment() |
| 114 | + } |
| 115 | + }(clients[i]) |
| 116 | + } |
| 117 | + |
| 118 | + go func() { |
| 119 | + for i := 0; i < mixedTxnTotal; i++ { |
| 120 | + var req request |
| 121 | + if rand.Float64() < mixedTxnReadWriteRatio/(1+mixedTxnReadWriteRatio) { |
| 122 | + opts := []v3.OpOption{v3.WithRange(mixedTxnEndKey)} |
| 123 | + if rangeConsistency == "s" { |
| 124 | + opts = append(opts, v3.WithSerializable()) |
| 125 | + } |
| 126 | + opts = append(opts, v3.WithPrefix(), v3.WithLimit(mixedTxnRangeLimit)) |
| 127 | + req.op = v3.OpGet("", opts...) |
| 128 | + req.isWrite = false |
| 129 | + readOpsTotal++ |
| 130 | + } else { |
| 131 | + binary.PutVarint(k, int64(i%keySpaceSize)) |
| 132 | + req.op = v3.OpPut(string(k), v) |
| 133 | + req.isWrite = true |
| 134 | + writeOpsTotal++ |
| 135 | + } |
| 136 | + requests <- req |
| 137 | + } |
| 138 | + close(requests) |
| 139 | + }() |
| 140 | + |
| 141 | + rcRead := reportRead.Run() |
| 142 | + rcWrite := reportWrite.Run() |
| 143 | + wg.Wait() |
| 144 | + close(reportRead.Results()) |
| 145 | + close(reportWrite.Results()) |
| 146 | + bar.Finish() |
| 147 | + fmt.Printf("Total Read Ops: %d\nDetails:", readOpsTotal) |
| 148 | + fmt.Println(<-rcRead) |
| 149 | + fmt.Printf("Total Write Ops: %d\nDetails:", writeOpsTotal) |
| 150 | + fmt.Println(<-rcWrite) |
| 151 | +} |
0 commit comments