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