gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/benchmarks/tools/ab.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  // ApacheBench is for the client application ApacheBench.
    25  type ApacheBench struct {
    26  	Requests    int
    27  	Concurrency int
    28  	Doc         string
    29  	// TODO(zkoopmans): support KeepAlive and pass option to enable.
    30  }
    31  
    32  // MakeCmd makes an ApacheBench command.
    33  func (a *ApacheBench) MakeCmd(host string, port int) []string {
    34  	path := fmt.Sprintf("http://%s:%d/%s", host, port, a.Doc)
    35  	// See apachebench (ab) for flags.
    36  	cmd := fmt.Sprintf("ab -n %d -c %d %s", a.Requests, a.Concurrency, path)
    37  	return []string{"sh", "-c", cmd}
    38  }
    39  
    40  // Report parses and reports metrics from ApacheBench output.
    41  func (a *ApacheBench) Report(b *testing.B, output string) {
    42  	// Parse and report custom metrics.
    43  	transferRate, err := a.parseTransferRate(output)
    44  	if err != nil {
    45  		b.Logf("failed to parse transferrate: %v", err)
    46  	}
    47  	b.ReportMetric(transferRate*1024, "transfer_rate_b/s") // Convert from Kb/s to b/s.
    48  	ReportCustomMetric(b, transferRate*1024, "transfer_rate" /*metric name*/, "bytes_per_second" /*unit*/)
    49  
    50  	latency, err := a.parseLatency(output)
    51  	if err != nil {
    52  		b.Logf("failed to parse latency: %v", err)
    53  	}
    54  	b.ReportMetric(latency/1000, "mean_latency_secs") // Convert from ms to s.
    55  	ReportCustomMetric(b, latency/1000, "mean_latency" /*metric name*/, "s" /*unit*/)
    56  
    57  	reqPerSecond, err := a.parseRequestsPerSecond(output)
    58  	if err != nil {
    59  		b.Logf("failed to parse requests per second: %v", err)
    60  	}
    61  	b.ReportMetric(reqPerSecond, "requests_per_second")
    62  	ReportCustomMetric(b, reqPerSecond, "requests_per_second" /*metric name*/, "QPS" /*unit*/)
    63  }
    64  
    65  var transferRateRE = regexp.MustCompile(`Transfer rate:\s+(\d+\.?\d+?)\s+\[Kbytes/sec\]\s+received`)
    66  
    67  // parseTransferRate parses transfer rate from ApacheBench output.
    68  func (a *ApacheBench) parseTransferRate(data string) (float64, error) {
    69  	match := transferRateRE.FindStringSubmatch(data)
    70  	if len(match) < 2 {
    71  		return 0, fmt.Errorf("failed get bandwidth: %s", data)
    72  	}
    73  	return strconv.ParseFloat(match[1], 64)
    74  }
    75  
    76  var latencyRE = regexp.MustCompile(`Total:\s+\d+\s+(\d+)\s+(\d+\.?\d+?)\s+\d+\s+\d+\s`)
    77  
    78  // parseLatency parses latency from ApacheBench output.
    79  func (a *ApacheBench) parseLatency(data string) (float64, error) {
    80  	match := latencyRE.FindStringSubmatch(data)
    81  	if len(match) < 2 {
    82  		return 0, fmt.Errorf("failed get bandwidth: %s", data)
    83  	}
    84  	return strconv.ParseFloat(match[1], 64)
    85  }
    86  
    87  var requestsPerSecondRE = regexp.MustCompile(`Requests per second:\s+(\d+\.?\d+?)\s+`)
    88  
    89  // parseRequestsPerSecond parses requests per second from ApacheBench output.
    90  func (a *ApacheBench) parseRequestsPerSecond(data string) (float64, error) {
    91  	match := requestsPerSecondRE.FindStringSubmatch(data)
    92  	if len(match) < 2 {
    93  		return 0, fmt.Errorf("failed get bandwidth: %s", data)
    94  	}
    95  	return strconv.ParseFloat(match[1], 64)
    96  }