
The following investigation occurred during development. Add TimingHistogram impl that shares lock with WeightedHistogram Benchmarking and profiling shows that two layers of locking is noticeably more expensive than one. After adding this new alternative, I now get the following benchmark results. ``` (base) mspreitz@mjs12 kubernetes % go test -benchmem -run=^$ -bench ^BenchmarkTimingHistogram$ k8s.io/component-base/metrics/prometheusextension goos: darwin goarch: amd64 pkg: k8s.io/component-base/metrics/prometheusextension cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz BenchmarkTimingHistogram-16 22232037 52.79 ns/op 0 B/op 0 allocs/op PASS ok k8s.io/component-base/metrics/prometheusextension 1.404s (base) mspreitz@mjs12 kubernetes % go test -benchmem -run=^$ -bench ^BenchmarkTimingHistogram$ k8s.io/component-base/metrics/prometheusextension goos: darwin goarch: amd64 pkg: k8s.io/component-base/metrics/prometheusextension cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz BenchmarkTimingHistogram-16 22190997 54.50 ns/op 0 B/op 0 allocs/op PASS ok k8s.io/component-base/metrics/prometheusextension 1.435s ``` and ``` (base) mspreitz@mjs12 kubernetes % go test -benchmem -run=^$ -bench ^BenchmarkTimingHistogramDirect$ k8s.io/component-base/metrics/prometheusextension goos: darwin goarch: amd64 pkg: k8s.io/component-base/metrics/prometheusextension cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz BenchmarkTimingHistogramDirect-16 28863244 40.99 ns/op 0 B/op 0 allocs/op PASS ok k8s.io/component-base/metrics/prometheusextension 1.890s (base) mspreitz@mjs12 kubernetes % (base) mspreitz@mjs12 kubernetes % (base) mspreitz@mjs12 kubernetes % go test -benchmem -run=^$ -bench ^BenchmarkTimingHistogramDirect$ k8s.io/component-base/metrics/prometheusextension goos: darwin goarch: amd64 pkg: k8s.io/component-base/metrics/prometheusextension cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz BenchmarkTimingHistogramDirect-16 27994173 40.37 ns/op 0 B/op 0 allocs/op PASS ok k8s.io/component-base/metrics/prometheusextension 1.384s ``` So the new implementation is roughly 20% faster than the original. Add overlooked exception, rename timingHistogram to timingHistogramLayered Use the direct (one mutex) style of TimingHistogram impl This is about a 20% gain in CPU speed on my development machine, in benchmarks without lock contention. Following are two consecutive trials. (base) mspreitz@mjs12 prometheusextension % go test -benchmem -run=^$ -bench Histogram . goos: darwin goarch: amd64 pkg: k8s.io/component-base/metrics/prometheusextension cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz BenchmarkTimingHistogramLayered-16 21650905 51.91 ns/op 0 B/op 0 allocs/op BenchmarkTimingHistogramDirect-16 29876860 39.33 ns/op 0 B/op 0 allocs/op BenchmarkWeightedHistogram-16 49227044 24.13 ns/op 0 B/op 0 allocs/op BenchmarkHistogram-16 41063907 28.82 ns/op 0 B/op 0 allocs/op PASS ok k8s.io/component-base/metrics/prometheusextension 5.432s (base) mspreitz@mjs12 prometheusextension % go test -benchmem -run=^$ -bench Histogram . goos: darwin goarch: amd64 pkg: k8s.io/component-base/metrics/prometheusextension cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz BenchmarkTimingHistogramLayered-16 22483816 51.72 ns/op 0 B/op 0 allocs/op BenchmarkTimingHistogramDirect-16 29697291 39.39 ns/op 0 B/op 0 allocs/op BenchmarkWeightedHistogram-16 48919845 24.03 ns/op 0 B/op 0 allocs/op BenchmarkHistogram-16 41153044 29.26 ns/op 0 B/op 0 allocs/op PASS ok k8s.io/component-base/metrics/prometheusextension 5.044s Remove layered implementation of TimingHistogram
151 lines
6.4 KiB
Bash
Executable File
151 lines
6.4 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# 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.
|
|
|
|
# This script validates that only a restricted set of packages are importing
|
|
# github.com/prometheus/*
|
|
|
|
# NOTE: this is not the same as verify-imports which can only verify
|
|
# that within a particular package the imports made are allowed.
|
|
#
|
|
# This is also not the same thing as verify-import-boss, which is pretty
|
|
# powerful for specifying restricted imports but does not scale to checking
|
|
# the entire source tree well and is only enabled for specific packages.
|
|
#
|
|
# See: https://github.com/kubernetes/kubernetes/issues/99876
|
|
|
|
set -o errexit
|
|
set -o nounset
|
|
set -o pipefail
|
|
|
|
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
|
|
source "${KUBE_ROOT}/hack/lib/init.sh"
|
|
source "${KUBE_ROOT}/hack/lib/util.sh"
|
|
|
|
# See: https://github.com/kubernetes/kubernetes/issues/89267
|
|
allowed_prometheus_importers=(
|
|
./cluster/images/etcd-version-monitor/etcd-version-monitor.go
|
|
./pkg/volume/util/operationexecutor/operation_generator_test.go
|
|
./staging/src/k8s.io/component-base/metrics/prometheusextension/timing_histogram.go
|
|
./staging/src/k8s.io/component-base/metrics/prometheusextension/timing_histogram_test.go
|
|
./staging/src/k8s.io/component-base/metrics/prometheusextension/timing_histogram_vec.go
|
|
./staging/src/k8s.io/component-base/metrics/prometheusextension/weighted_histogram.go
|
|
./staging/src/k8s.io/component-base/metrics/prometheusextension/weighted_histogram_test.go
|
|
./staging/src/k8s.io/component-base/metrics/prometheusextension/weighted_histogram_vec.go
|
|
./staging/src/k8s.io/component-base/metrics/collector.go
|
|
./staging/src/k8s.io/component-base/metrics/collector_test.go
|
|
./staging/src/k8s.io/component-base/metrics/counter.go
|
|
./staging/src/k8s.io/component-base/metrics/counter_test.go
|
|
./staging/src/k8s.io/component-base/metrics/desc.go
|
|
./staging/src/k8s.io/component-base/metrics/gauge.go
|
|
./staging/src/k8s.io/component-base/metrics/gauge_test.go
|
|
./staging/src/k8s.io/component-base/metrics/histogram.go
|
|
./staging/src/k8s.io/component-base/metrics/histogram_test.go
|
|
./staging/src/k8s.io/component-base/metrics/http.go
|
|
./staging/src/k8s.io/component-base/metrics/labels.go
|
|
./staging/src/k8s.io/component-base/metrics/legacyregistry/registry.go
|
|
./staging/src/k8s.io/component-base/metrics/metric.go
|
|
./staging/src/k8s.io/component-base/metrics/opts.go
|
|
./staging/src/k8s.io/component-base/metrics/processstarttime_others.go
|
|
./staging/src/k8s.io/component-base/metrics/registry.go
|
|
./staging/src/k8s.io/component-base/metrics/registry_test.go
|
|
./staging/src/k8s.io/component-base/metrics/summary.go
|
|
./staging/src/k8s.io/component-base/metrics/testutil/metrics.go
|
|
./staging/src/k8s.io/component-base/metrics/testutil/metrics_test.go
|
|
./staging/src/k8s.io/component-base/metrics/testutil/promlint.go
|
|
./staging/src/k8s.io/component-base/metrics/testutil/testutil.go
|
|
./staging/src/k8s.io/component-base/metrics/value.go
|
|
./staging/src/k8s.io/component-base/metrics/wrappers.go
|
|
./test/e2e/apimachinery/flowcontrol.go
|
|
./test/e2e/node/pods.go
|
|
./test/e2e_node/resource_metrics_test.go
|
|
./test/instrumentation/main_test.go
|
|
./test/integration/apiserver/flowcontrol/concurrency_test.go
|
|
./test/integration/metrics/metrics_test.go
|
|
)
|
|
|
|
# Go imports always involve a double quoted string of the package path
|
|
# https://golang.org/ref/spec#Import_declarations
|
|
#
|
|
# If you *really* need a string literal that looks like "github.com/prometheus/.*"
|
|
# somewhere else that actually isn't an import, you can use backticks / a raw
|
|
# string literal instead (which cannot be used in imports, only double quotes).
|
|
#
|
|
# NOTE: we previously had an implementation that checked for an actual import
|
|
# as a post-processing step on the matching files, which is cheap enough and
|
|
# accurate, except that it's difficult to guarantee we check for all supported
|
|
# GOOS, GOARCH, and other build tags, and we want to prevent all imports.
|
|
# So we dropped this, in favor of only the grep call.
|
|
# See: https://github.com/kubernetes/kubernetes/pull/100552
|
|
really_failing_files=()
|
|
all_failing_files=()
|
|
while IFS='' read -r filepath; do
|
|
# convert from file to package, and only insert unique results
|
|
# we want to minimize the amount of `go list` calls we need to make
|
|
if ! kube::util::array_contains "$filepath" "${allowed_prometheus_importers[@]}"; then
|
|
# record a failure if not
|
|
really_failing_files+=("$filepath")
|
|
fi
|
|
all_failing_files+=("$filepath")
|
|
done < <(cd "${KUBE_ROOT}" && grep \
|
|
--exclude-dir={_output,vendor} \
|
|
--include='*.go' \
|
|
-R . \
|
|
-l \
|
|
-Ee '"github.com/prometheus/.*"' \
|
|
| LC_ALL=C sort -u)
|
|
|
|
# check for any files we're allowing to fail that are no longer failing, so we
|
|
# can enforce that the list shrinks
|
|
allowed_but_not_failing=()
|
|
for allowed_file in "${allowed_prometheus_importers[@]}"; do
|
|
if ! kube::util::array_contains "$allowed_file" "${all_failing_files[@]}"; then
|
|
allowed_but_not_failing+=("$allowed_file")
|
|
fi
|
|
done
|
|
|
|
# we will exit with this at the end of the script depending on the checks below
|
|
exit_code=0
|
|
|
|
# check for files we've allow-listed that no longer need to be
|
|
if [ -n "${allowed_but_not_failing[*]}" ]; then
|
|
{
|
|
echo "ERROR: Some files allow-listed to import prometheus are no longer failing and should be removed."
|
|
echo "Please remove these files from allowed_prometheus_importers in hack/verify-prometheus-imports.sh"
|
|
echo ""
|
|
echo "Non-failing but allow-listed files:"
|
|
for non_failing_file in "${allowed_but_not_failing[@]}"; do
|
|
echo " ${non_failing_file}"
|
|
done
|
|
} >&2
|
|
exit_code=1
|
|
fi
|
|
# check for files that fail but are not allow-listed
|
|
if [ -n "${really_failing_files[*]}" ]; then
|
|
{
|
|
echo "ERROR: Some files are importing packages under github.com/prometheus/* but are not allow-listed to do so."
|
|
echo ""
|
|
echo "See: https://github.com/kubernetes/kubernetes/issues/89267"
|
|
echo ""
|
|
echo "Failing files:"
|
|
for failing_file in "${really_failing_files[@]}"; do
|
|
echo " ${failing_file}"
|
|
done
|
|
} >&2
|
|
exit_code=2
|
|
fi
|
|
|
|
exit "$exit_code"
|