github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/benchmarks/tools/redis.go (about) 1 // Copyright 2020 The gVisor 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 tools 16 17 import ( 18 "fmt" 19 "net" 20 "regexp" 21 "strconv" 22 "testing" 23 ) 24 25 // Redis is for the client 'redis-benchmark'. 26 type Redis struct { 27 Operation string 28 } 29 30 // MakeCmd returns a redis-benchmark client command. 31 func (r *Redis) MakeCmd(ip net.IP, port, requests int) []string { 32 // There is no -t PING_BULK for redis-benchmark, so adjust the command in that case. 33 // Note that "ping" will run both PING_INLINE and PING_BULK. 34 if r.Operation == "PING_BULK" { 35 return []string{ 36 "redis-benchmark", 37 "--csv", 38 "-t", "ping", 39 "-h", ip.String(), 40 "-p", fmt.Sprintf("%d", port), 41 "-n", fmt.Sprintf("%d", requests), 42 } 43 } 44 45 // runs redis-benchmark -t operation for 100K requests against server. 46 return []string{ 47 "redis-benchmark", 48 "--csv", 49 "-t", r.Operation, 50 "-h", ip.String(), 51 "-p", fmt.Sprintf("%d", port), 52 "-n", fmt.Sprintf("%d", requests), 53 } 54 } 55 56 // Report parses output from redis-benchmark client and reports metrics. 57 func (r *Redis) Report(b *testing.B, output string) { 58 b.Helper() 59 result, err := r.parseOperation(output) 60 if err != nil { 61 b.Fatalf("parsing result %s failed with err: %v", output, err) 62 } 63 ReportCustomMetric(b, result, r.Operation /*metric_name*/, "QPS" /*unit*/) 64 } 65 66 // parseOperation grabs the metric operations per second from redis-benchmark output. 67 func (r *Redis) parseOperation(data string) (float64, error) { 68 re := regexp.MustCompile(fmt.Sprintf(`"%s( .*)?","(\d*\.\d*)"`, r.Operation)) 69 match := re.FindStringSubmatch(data) 70 if len(match) < 3 { 71 return 0.0, fmt.Errorf("could not find %s in %s", r.Operation, data) 72 } 73 return strconv.ParseFloat(match[2], 64) 74 }