github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/benchmarks/tools/ab_test.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 "testing"
    18  
    19  // TestApacheBench checks the ApacheBench parsers on sample output.
    20  func TestApacheBench(t *testing.T) {
    21  	// Sample output from apachebench.
    22  	sampleData := `This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
    23  Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
    24  Licensed to The Apache Software Foundation, http://www.apache.org/
    25  
    26  Benchmarking 10.10.10.10 (be patient).....done
    27  
    28  
    29  Server Software:        Apache/2.4.38
    30  Server Hostname:        10.10.10.10
    31  Server Port:            80
    32  
    33  Document Path:          /latin10k.txt
    34  Document Length:        210 bytes
    35  
    36  Concurrency Level:      1
    37  Time taken for tests:   0.180 seconds
    38  Complete requests:      100
    39  Failed requests:        0
    40  Non-2xx responses:      100
    41  Total transferred:      38800 bytes
    42  HTML transferred:       21000 bytes
    43  Requests per second:    556.44 [#/sec] (mean)
    44  Time per request:       1.797 [ms] (mean)
    45  Time per request:       1.797 [ms] (mean, across all concurrent requests)
    46  Transfer rate:          210.84 [Kbytes/sec] received
    47  
    48  Connection Times (ms)
    49                min  mean[+/-sd] median   max
    50  Connect:        0    0   0.2      0       2
    51  Processing:     1    2   1.0      1       8
    52  Waiting:        1    1   1.0      1       7
    53  Total:          1    2   1.2      1      10
    54  
    55  Percentage of the requests served within a certain time (ms)
    56    50%      1
    57    66%      2
    58    75%      2
    59    80%      2
    60    90%      2
    61    95%      3
    62    98%      7
    63    99%     10
    64   100%     10 (longest request)`
    65  
    66  	ab := ApacheBench{}
    67  	want := 210.84
    68  	got, err := ab.parseTransferRate(sampleData)
    69  	if err != nil {
    70  		t.Fatalf("failed to parse transfer rate with error: %v", err)
    71  	} else if got != want {
    72  		t.Fatalf("parseTransferRate got: %f, want: %f", got, want)
    73  	}
    74  
    75  	want = 2.0
    76  	got, err = ab.parseLatency(sampleData)
    77  	if err != nil {
    78  		t.Fatalf("failed to parse transfer rate with error: %v", err)
    79  	} else if got != want {
    80  		t.Fatalf("parseLatency got: %f, want: %f", got, want)
    81  	}
    82  
    83  	want = 556.44
    84  	got, err = ab.parseRequestsPerSecond(sampleData)
    85  	if err != nil {
    86  		t.Fatalf("failed to parse transfer rate with error: %v", err)
    87  	} else if got != want {
    88  		t.Fatalf("parseRequestsPerSecond got: %f, want: %f", got, want)
    89  	}
    90  }