github.com/jgbaldwinbrown/perf@v0.1.1/benchmath/aexact.go (about)

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package benchmath
     6  
     7  import "fmt"
     8  
     9  // AssumeExact is an assumption that a value can be measured exactly
    10  // and thus has no distribution and does not require repeated sampling.
    11  // It reports a warning if not all values in a sample are equal.
    12  var AssumeExact = assumeExact{}
    13  
    14  type assumeExact struct{}
    15  
    16  var _ Assumption = assumeExact{}
    17  
    18  func (assumeExact) SummaryLabel() string {
    19  	// Really the summary is the mode, but the point of this
    20  	// assumption is that the summary is the exact value.
    21  	return "exact"
    22  }
    23  
    24  func (assumeExact) Summary(s *Sample, confidence float64) Summary {
    25  	// Find the sample's mode. This checks if all samples are the
    26  	// same, and lets us return a reasonable summary even if they
    27  	// aren't all the same.
    28  	val, count := s.Values[0], 1
    29  	modeVal, modeCount := val, count
    30  	for _, v := range s.Values[1:] {
    31  		if v == val {
    32  			count++
    33  			if count > modeCount {
    34  				modeVal, modeCount = val, count
    35  			}
    36  		} else {
    37  			val, count = v, 1
    38  		}
    39  	}
    40  	summary := Summary{Center: modeVal, Lo: s.Values[0], Hi: s.Values[len(s.Values)-1], Confidence: 1}
    41  
    42  	if modeCount != len(s.Values) {
    43  		// They're not all the same. Report a warning.
    44  		summary.Warnings = []error{fmt.Errorf("exact distribution expected, but values range from %v to %v", s.Values[0], s.Values[len(s.Values)-1])}
    45  	}
    46  	return summary
    47  }
    48  
    49  func (assumeExact) Compare(s1, s2 *Sample) Comparison {
    50  	return Comparison{P: 0, N1: len(s1.Values), N2: len(s2.Values)}
    51  }