github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/benchmarks/tools/hey.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 // Hey is for the client application 'hey'. 26 type Hey struct { 27 Requests int // Note: requests cannot be less than concurrency. 28 Concurrency int 29 Doc string 30 } 31 32 // MakeCmd returns a 'hey' command. 33 func (h *Hey) MakeCmd(ip net.IP, port int) []string { 34 c := h.Concurrency 35 if c > h.Requests { 36 c = h.Requests 37 } 38 return []string{ 39 "hey", 40 "-n", fmt.Sprintf("%d", h.Requests), 41 "-c", fmt.Sprintf("%d", c), 42 fmt.Sprintf("http://%s:%d/%s", ip.String(), port, h.Doc), 43 } 44 } 45 46 // Report parses output from 'hey' and reports metrics. 47 func (h *Hey) Report(b *testing.B, output string) { 48 b.Helper() 49 requests, err := h.parseRequestsPerSecond(output) 50 if err != nil { 51 b.Fatalf("failed to parse requests per second: %v", err) 52 } 53 ReportCustomMetric(b, requests, "requests_per_second" /*metric name*/, "QPS" /*unit*/) 54 55 ave, err := h.parseAverageLatency(output) 56 if err != nil { 57 b.Fatalf("failed to parse average latency: %v", err) 58 } 59 ReportCustomMetric(b, ave, "average_latency" /*metric name*/, "s" /*unit*/) 60 } 61 62 var heyReqPerSecondRE = regexp.MustCompile(`Requests/sec:\s*(\d+\.?\d+?)\s+`) 63 64 // parseRequestsPerSecond finds requests per second from 'hey' output. 65 func (h *Hey) parseRequestsPerSecond(data string) (float64, error) { 66 match := heyReqPerSecondRE.FindStringSubmatch(data) 67 if len(match) < 2 { 68 return 0, fmt.Errorf("failed get bandwidth: %s", data) 69 } 70 return strconv.ParseFloat(match[1], 64) 71 } 72 73 var heyAverageLatencyRE = regexp.MustCompile(`Average:\s*(\d+\.?\d+?)\s+secs`) 74 75 // parseHeyAverageLatency finds Average Latency in seconds form 'hey' output. 76 func (h *Hey) parseAverageLatency(data string) (float64, error) { 77 match := heyAverageLatencyRE.FindStringSubmatch(data) 78 if len(match) < 2 { 79 return 0, fmt.Errorf("failed get average latency match%d : %s", len(match), data) 80 } 81 return strconv.ParseFloat(match[1], 64) 82 }