aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/histogram_test.go
diff options
context:
space:
mode:
authorMichael Anthony Knyszek <mknyszek@google.com>2020-08-06 20:36:49 +0000
committerMichael Knyszek <mknyszek@google.com>2020-10-26 21:47:49 +0000
commit36c5edd8d9e6c13af26733e5c820eae0598203fe (patch)
treefa6de57db82f3ee49df98b3dfddff62d0f6af536 /src/runtime/histogram_test.go
parent8e2370bf7f0c992ce1ea5dc54b43551cea71a485 (diff)
downloadgo-36c5edd8d9e6c13af26733e5c820eae0598203fe.tar.gz
go-36c5edd8d9e6c13af26733e5c820eae0598203fe.zip
runtime: add timeHistogram type
This change adds a concurrent HDR time histogram to the runtime with tests. It also adds a function to generate boundaries for use by the metrics package. For #37112. Change-Id: Ifbef8ddce8e3a965a0dcd58ccd4915c282ae2098 Reviewed-on: https://go-review.googlesource.com/c/go/+/247046 Run-TryBot: Michael Knyszek <mknyszek@google.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Pratt <mpratt@google.com>
Diffstat (limited to 'src/runtime/histogram_test.go')
-rw-r--r--src/runtime/histogram_test.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/runtime/histogram_test.go b/src/runtime/histogram_test.go
new file mode 100644
index 0000000000..5f5b28f784
--- /dev/null
+++ b/src/runtime/histogram_test.go
@@ -0,0 +1,58 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package runtime_test
+
+import (
+ . "runtime"
+ "testing"
+)
+
+var dummyTimeHistogram TimeHistogram
+
+func TestTimeHistogram(t *testing.T) {
+ // We need to use a global dummy because this
+ // could get stack-allocated with a non-8-byte alignment.
+ // The result of this bad alignment is a segfault on
+ // 32-bit platforms when calling Record.
+ h := &dummyTimeHistogram
+
+ // Record exactly one sample in each bucket.
+ for i := 0; i < TimeHistNumSuperBuckets; i++ {
+ var base int64
+ if i > 0 {
+ base = int64(1) << (i + TimeHistSubBucketBits - 1)
+ }
+ for j := 0; j < TimeHistNumSubBuckets; j++ {
+ v := int64(j)
+ if i > 0 {
+ v <<= i - 1
+ }
+ h.Record(base + v)
+ }
+ }
+ // Hit the overflow bucket.
+ h.Record(int64(^uint64(0) >> 1))
+
+ // Check to make sure there's exactly one count in each
+ // bucket.
+ for i := uint(0); i < TimeHistNumSuperBuckets; i++ {
+ for j := uint(0); j < TimeHistNumSubBuckets; j++ {
+ c, ok := h.Count(i, j)
+ if !ok {
+ t.Errorf("hit overflow bucket unexpectedly: (%d, %d)", i, j)
+ } else if c != 1 {
+ t.Errorf("bucket (%d, %d) has count that is not 1: %d", i, j, c)
+ }
+ }
+ }
+ c, ok := h.Count(TimeHistNumSuperBuckets, 0)
+ if ok {
+ t.Errorf("expected to hit overflow bucket: (%d, %d)", TimeHistNumSuperBuckets, 0)
+ }
+ if c != 1 {
+ t.Errorf("overflow bucket has count that is not 1: %d", c)
+ }
+ dummyTimeHistogram = TimeHistogram{}
+}