Black Lives Matter. Support the Equal Justice Initiative.

Source file src/runtime/histogram_test.go

Documentation: runtime

     1  // Copyright 2020 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 runtime_test
     6  
     7  import (
     8  	"math"
     9  	. "runtime"
    10  	"testing"
    11  )
    12  
    13  var dummyTimeHistogram TimeHistogram
    14  
    15  func TestTimeHistogram(t *testing.T) {
    16  	// We need to use a global dummy because this
    17  	// could get stack-allocated with a non-8-byte alignment.
    18  	// The result of this bad alignment is a segfault on
    19  	// 32-bit platforms when calling Record.
    20  	h := &dummyTimeHistogram
    21  
    22  	// Record exactly one sample in each bucket.
    23  	for i := 0; i < TimeHistNumSuperBuckets; i++ {
    24  		var base int64
    25  		if i > 0 {
    26  			base = int64(1) << (i + TimeHistSubBucketBits - 1)
    27  		}
    28  		for j := 0; j < TimeHistNumSubBuckets; j++ {
    29  			v := int64(j)
    30  			if i > 0 {
    31  				v <<= i - 1
    32  			}
    33  			h.Record(base + v)
    34  		}
    35  	}
    36  	// Hit the underflow bucket.
    37  	h.Record(int64(-1))
    38  
    39  	// Check to make sure there's exactly one count in each
    40  	// bucket.
    41  	for i := uint(0); i < TimeHistNumSuperBuckets; i++ {
    42  		for j := uint(0); j < TimeHistNumSubBuckets; j++ {
    43  			c, ok := h.Count(i, j)
    44  			if !ok {
    45  				t.Errorf("hit underflow bucket unexpectedly: (%d, %d)", i, j)
    46  			} else if c != 1 {
    47  				t.Errorf("bucket (%d, %d) has count that is not 1: %d", i, j, c)
    48  			}
    49  		}
    50  	}
    51  	c, ok := h.Count(TimeHistNumSuperBuckets, 0)
    52  	if ok {
    53  		t.Errorf("expected to hit underflow bucket: (%d, %d)", TimeHistNumSuperBuckets, 0)
    54  	}
    55  	if c != 1 {
    56  		t.Errorf("underflow bucket has count that is not 1: %d", c)
    57  	}
    58  
    59  	// Check overflow behavior.
    60  	// By hitting a high value, we should just be adding into the highest bucket.
    61  	h.Record(math.MaxInt64)
    62  	c, ok = h.Count(TimeHistNumSuperBuckets-1, TimeHistNumSubBuckets-1)
    63  	if !ok {
    64  		t.Error("hit underflow bucket in highest bucket unexpectedly")
    65  	} else if c != 2 {
    66  		t.Errorf("highest has count that is not 2: %d", c)
    67  	}
    68  
    69  	dummyTimeHistogram = TimeHistogram{}
    70  }
    71  

View as plain text