Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for sample-and-watermark histograms #97753

Merged
merged 4 commits into from Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion staging/src/k8s.io/apiserver/pkg/util/flowcontrol/metrics/BUILD
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
Expand Down Expand Up @@ -32,3 +32,15 @@ filegroup(
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

go_test(
name = "go_default_test",
srcs = ["sample_and_watermark_test.go"],
embed = [":go_default_library"],
deps = [
"//staging/src/k8s.io/apimachinery/pkg/util/clock:go_default_library",
"//staging/src/k8s.io/component-base/metrics:go_default_library",
"//staging/src/k8s.io/component-base/metrics/legacyregistry:go_default_library",
"//vendor/k8s.io/klog/v2:go_default_library",
],
)
@@ -0,0 +1,100 @@
/*
Copyright 2021 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package metrics

import (
"fmt"
"math/rand"
"testing"
"time"

"k8s.io/apimachinery/pkg/util/clock"
compbasemetrics "k8s.io/component-base/metrics"
"k8s.io/component-base/metrics/legacyregistry"
"k8s.io/klog/v2"
)

const (
samplesHistName = "sawtestsamples"
ddtRange = 3000
ddtOffset = 500
numIterations = 100
)

func TestSampler(t *testing.T) {
t0 := time.Now()
clk := clock.NewFakePassiveClock(t0)
buckets := []float64{0, 1}
const samplingPeriod = time.Millisecond
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put samplingPeriod into above const block?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

gen := NewSampleAndWaterMarkHistogramsGenerator(clk, samplingPeriod,
&compbasemetrics.HistogramOpts{Name: samplesHistName, Buckets: buckets},
&compbasemetrics.HistogramOpts{Name: "marks", Buckets: buckets},
[]string{})
saw := gen.Generate(0, 1, []string{})
regs := gen.metrics()
for _, reg := range regs {
legacyregistry.MustRegister(reg)
}
dt := 2 * samplingPeriod
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is dt? Why it is 2 samplingPeriod?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comment explaining what dt is. Changed my mind about initialization; the original was to avoid the case of a net negative time difference, and I decided to not try to avoid that.

t1 := t0.Add(dt)
klog.Infof("Expect about %v warnings about time going backwards; this is fake time deliberately misbehaving.", (numIterations*ddtOffset)/ddtRange)
t.Logf("t0=%s", t0)
for i := 0; i < numIterations; i++ {
ddt := time.Microsecond * time.Duration(rand.Intn(ddtRange)-ddtOffset)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, I've been staring at this for a while now and I don't really get what exactly is being tested or why it's being tested like this :( I'm really sorry to give feedback this vague but I'm not sure what would help here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment explaining the new test func.
I prefer behavior tests to checksum tests. This behavioral test exercises the quantize function, so should cover the original ask.

t1 = t1.Add(ddt)
diff := t1.Sub(t0)
if diff > dt {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am so sorry I don't really get the logic behind this code. Can you explain more about the relationship between clock and the histogram count? Also how do you calculate the expectedCount?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added/expanded comments, hopefully it is clearer now.

dt = diff
}
clk.SetTime(t1)
saw.Set(1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to test other timeObserver operation like Add or SetX1?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional tests could be created for those. This one is explicitly and exclusively focused on the question of when samples are taken.

expectedCount := int64(dt / time.Millisecond)
actualCount, err := getHistogramCount(regs, samplesHistName)
if err != nil {
t.Fatalf("For t0=%s, t1=%s, failed to getHistogramCount: %#+v", t0, t1, err)
}
t.Logf("For i=%d, ddt=%s, t1=%s, diff=%s, dt=%s, count=%d", i, ddt, t1, diff, dt, actualCount)
if expectedCount != actualCount {
t.Errorf("For i=%d, t0=%s, ddt=%s, t1=%s, expectedCount=%d, actualCount=%d", i, t0, ddt, t1, expectedCount, actualCount)
}
}
}

func getHistogramCount(regs Registerables, metricName string) (int64, error) {
considered := []string{}
mfs, err := legacyregistry.DefaultGatherer.Gather()
if err != nil {
return 0, fmt.Errorf("failed to gather metrics: %s", err)
}
for _, mf := range mfs {
thisName := mf.GetName()
if thisName != metricName {
considered = append(considered, thisName)
continue
}
metric := mf.GetMetric()[0]
hist := metric.GetHistogram()
if hist == nil {
return 0, fmt.Errorf("dto.Metric has nil Histogram")
}
if hist.SampleCount == nil {
return 0, fmt.Errorf("dto.Histogram has nil SampleCount")
}
return int64(*hist.SampleCount), nil
}
return 0, fmt.Errorf("not found, considered=%#+v", considered)
}