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