github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/benchmarks/tools/hey_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  // TestHey checks the Hey parsers on sample output.
    20  func TestHey(t *testing.T) {
    21  	sampleData := `
    22  	Summary:
    23            Total:	2.2391 secs
    24            Slowest:	1.6292 secs
    25            Fastest:	0.0066 secs
    26            Average:	0.5351 secs
    27            Requests/sec:	89.3202
    28  
    29            Total data:	841200 bytes
    30            Size/request:	4206 bytes
    31  
    32          Response time histogram:
    33            0.007 [1]	|
    34            0.169 [0]	|
    35            0.331 [149]	|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
    36            0.493 [0]	|
    37            0.656 [0]	|
    38            0.818 [0]	|
    39            0.980 [0]	|
    40            1.142 [0]	|
    41            1.305 [0]	|
    42            1.467 [49]	|■■■■■■■■■■■■■
    43            1.629 [1]	|
    44  
    45  
    46          Latency distribution:
    47            10% in 0.2149 secs
    48            25% in 0.2449 secs
    49            50% in 0.2703 secs
    50            75% in 1.3315 secs
    51            90% in 1.4045 secs
    52            95% in 1.4232 secs
    53            99% in 1.4362 secs
    54  
    55          Details (average, fastest, slowest):
    56            DNS+dialup:	0.0002 secs, 0.0066 secs, 1.6292 secs
    57            DNS-lookup:	0.0000 secs, 0.0000 secs, 0.0000 secs
    58            req write:	0.0000 secs, 0.0000 secs, 0.0012 secs
    59            resp wait:	0.5225 secs, 0.0064 secs, 1.4346 secs
    60            resp read:	0.0122 secs, 0.0001 secs, 0.2006 secs
    61  
    62          Status code distribution:
    63            [200]	200 responses
    64  	`
    65  	hey := Hey{}
    66  	want := 89.3202
    67  	got, err := hey.parseRequestsPerSecond(sampleData)
    68  	if err != nil {
    69  		t.Fatalf("failed to parse request per second with: %v", err)
    70  	} else if got != want {
    71  		t.Fatalf("got: %f, want: %f", got, want)
    72  	}
    73  
    74  	want = 0.5351
    75  	got, err = hey.parseAverageLatency(sampleData)
    76  	if err != nil {
    77  		t.Fatalf("failed to parse average latency with: %v", err)
    78  	} else if got != want {
    79  		t.Fatalf("got: %f, want: %f", got, want)
    80  	}
    81  }