go.etcd.io/etcd@v3.3.27+incompatible/pkg/report/report_test.go (about)

     1  // Copyright 2017 The etcd 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 report
    16  
    17  import (
    18  	"fmt"
    19  	"reflect"
    20  	"strings"
    21  	"testing"
    22  	"time"
    23  )
    24  
    25  func TestPercentiles(t *testing.T) {
    26  	nums := make([]float64, 100)
    27  	nums[99] = 1 // 99-percentile (1 out of 100)
    28  	data := percentiles(nums)
    29  	if data[len(pctls)-2] != 1 {
    30  		t.Fatalf("99-percentile expected 1, got %f", data[len(pctls)-2])
    31  	}
    32  
    33  	nums = make([]float64, 1000)
    34  	nums[999] = 1 // 99.9-percentile (1 out of 1000)
    35  	data = percentiles(nums)
    36  	if data[len(pctls)-1] != 1 {
    37  		t.Fatalf("99.9-percentile expected 1, got %f", data[len(pctls)-1])
    38  	}
    39  }
    40  
    41  func TestReport(t *testing.T) {
    42  	r := NewReportSample("%f")
    43  	go func() {
    44  		start := time.Now()
    45  		for i := 0; i < 5; i++ {
    46  			end := start.Add(time.Second)
    47  			r.Results() <- Result{Start: start, End: end}
    48  			start = end
    49  		}
    50  		r.Results() <- Result{Start: start, End: start.Add(time.Second), Err: fmt.Errorf("oops")}
    51  		close(r.Results())
    52  	}()
    53  
    54  	stats := <-r.Stats()
    55  	stats.TimeSeries = nil // ignore timeseries since it uses wall clock
    56  	wStats := Stats{
    57  		AvgTotal:  5.0,
    58  		Fastest:   1.0,
    59  		Slowest:   1.0,
    60  		Average:   1.0,
    61  		Stddev:    0.0,
    62  		Total:     stats.Total,
    63  		RPS:       5.0 / stats.Total.Seconds(),
    64  		ErrorDist: map[string]int{"oops": 1},
    65  		Lats:      []float64{1.0, 1.0, 1.0, 1.0, 1.0},
    66  	}
    67  	if !reflect.DeepEqual(stats, wStats) {
    68  		t.Fatalf("got %+v, want %+v", stats, wStats)
    69  	}
    70  
    71  	wstrs := []string{
    72  		"Stddev:\t0",
    73  		"Average:\t1.0",
    74  		"Slowest:\t1.0",
    75  		"Fastest:\t1.0",
    76  	}
    77  	ss := <-r.Run()
    78  	for i, ws := range wstrs {
    79  		if !strings.Contains(ss, ws) {
    80  			t.Errorf("#%d: stats string missing %s", i, ws)
    81  		}
    82  	}
    83  }
    84  
    85  func TestWeightedReport(t *testing.T) {
    86  	r := NewWeightedReport(NewReport("%f"), "%f")
    87  	go func() {
    88  		start := time.Now()
    89  		for i := 0; i < 5; i++ {
    90  			end := start.Add(time.Second)
    91  			r.Results() <- Result{Start: start, End: end, Weight: 2.0}
    92  			start = end
    93  		}
    94  		r.Results() <- Result{Start: start, End: start.Add(time.Second), Err: fmt.Errorf("oops")}
    95  		close(r.Results())
    96  	}()
    97  
    98  	stats := <-r.Stats()
    99  	stats.TimeSeries = nil // ignore timeseries since it uses wall clock
   100  	wStats := Stats{
   101  		AvgTotal:  10.0,
   102  		Fastest:   0.5,
   103  		Slowest:   0.5,
   104  		Average:   0.5,
   105  		Stddev:    0.0,
   106  		Total:     stats.Total,
   107  		RPS:       10.0 / stats.Total.Seconds(),
   108  		ErrorDist: map[string]int{"oops": 1},
   109  		Lats:      []float64{0.5, 0.5, 0.5, 0.5, 0.5},
   110  	}
   111  	if !reflect.DeepEqual(stats, wStats) {
   112  		t.Fatalf("got %+v, want %+v", stats, wStats)
   113  	}
   114  }