Skip to content

Instantly share code, notes, and snippets.

@mauriciopoppe
Last active October 30, 2026 18:34
Show Gist options
  • Select an option

  • Save mauriciopoppe/508635e973bb8795cab2241c9a520edc to your computer and use it in GitHub Desktop.

Select an option

Save mauriciopoppe/508635e973bb8795cab2241c9a520edc to your computer and use it in GitHub Desktop.
Integration Memory Benchmark of kube-apiserver

In-Process kube-apiserver Memory Integration Benchmark (TestAPIServerMemoryIntegration)

Purpose and Motivation

Profiling kube-apiserver memory consumption and allocation churn in isolation is challenging: microbenchmarks only exercise a single function at a time, while full cluster e2e tests introduce external network jitter, node-agent background noise, and non-deterministic garbage collection timing.

TestAPIServerMemoryIntegration (cmd/kube-apiserver/app/testing/apiserver_mem_bench_test.go) is a self-contained, deterministic in-process kube-apiserver + embedded etcd3 integration benchmark designed to measure both steady-state retained heap memory (pprof inuse_space, HeapInuse, HeapObjects, process RSS) and cumulative request allocation churn (pprof alloc_space, TotalAlloc, Mallocs, NumGC) with zero external dependencies.

In-Process Benchmark Architecture and Request Flow

Key Design Properties

  • Full In-Process Control-Plane Stack: Boots a real kube-apiserver via StartTestServer backed by an embedded etcd3 server (etcd3testing.NewUnsecuredEtcd3TestClientServer), registering all built-in Kubernetes API groups, watch caches (cacher), admission plugins, and OpenAPI V2/V3 discovery services.
  • Byte-Exact Allocation Profiling (runtime.MemProfileRate = 1 and GOGC = 50): Setting runtime.MemProfileRate = 1 before server startup instructs the Go runtime profiler to record every heap allocation (100% byte-exact sampling instead of the default 512 KB statistical sampling), while debug.SetGCPercent(50) enforces a consistent garbage collection target.
  • Repeated-Measures Aggregation (N = 5 Iterations, Trimmed Mean of Middle 3): Running 5 independent full-server iterations and computing the trimmed mean of the middle 3 iterations (dropping the highest and lowest run) filters out transient scheduling variance.

Workload Phases Per Iteration

Each benchmark iteration executes five sequential phases inside the kube-apiserver process:

  • Control-Plane Bootstrap and CEL Admission Seeding: Starts kube-apiserver and creates 10 ValidatingAdmissionPolicy objects and 10 ValidatingAdmissionPolicyBinding objects targeting core/v1 ConfigMaps. Each policy compiles and evaluates a Common Expression Language (CEL) expression (has(object.metadata.name) && !object.metadata.name.startsWith('forbidden-i-') && object.metadata.name.matches('^[a-z0-9-]+$')), exercising CEL environment extension and regular expression evaluation on every write.
  • System ConfigMap Seeding (60 x 20 KB Objects): Seeds 60 representative ConfigMap objects in kube-system, each carrying a 20 KB configuration payload (~1.25 MB per full LIST response), standard control-plane labels and annotations, and ManagedFields (FieldsV1) metadata.
  • Concurrent Multi-Worker Read/Write Load (8 Workers x 8 Rounds = 64 Rounds): Spawns 8 concurrent client goroutines. In each of the 8 rounds, every worker executes:
    • 1 Protobuf ConfigMap LIST (application/vnd.kubernetes.protobuf, ~1.25 MB response, exercising runtime.Allocator and Protobuf marshaling/unmarshaling).
    • 1 JSON gzip-compressed ConfigMap LIST (Accept: application/json, Accept-Encoding: gzip, exercising JSON serialization and responsewriters).
    • 3 Protobuf ConfigMap GET requests and 3 JSON ConfigMap GET requests (exercising cacher, etcd3 decoding, and (*ConfigMap).Unmarshal).
    • 1 ConfigMap UPDATE request with a unique per-worker FieldManager (triggering all 10 bound CEL ValidatingAdmissionPolicies and ManagedFields updates).
  • Concurrent OpenAPI V3 GroupVersion Discovery (8 Workers x 2 Rounds x 3 Paths = 48 Requests): Exercises per-GroupVersion OpenAPI V3 discovery across /openapi/v3/api/v1, /openapi/v3/apis/apps/v1, and /openapi/v3/apis/admissionregistration.k8s.io/v1 with Accept-Encoding: gzip.
  • Single Synchronous GC Cycle and In-Process Profile Capture: Runs a single synchronous runtime.GC() and debug.FreeOSMemory() cycle before dumping apiserver_heap.pb.gz (pprof.WriteHeapProfile) and recording runtime.MemStats and /proc/self/status (VmRSS) into apiserver_telemetry.json. Performing one GC cycle collects unreachable request garbage while preserving warm sync.Pool caches, matching steady-state production behavior under continuous traffic.

Quick Start and Reproduction Commands

From the root of a kubernetes/kubernetes checkout:

# 1. Download the benchmark test into cmd/kube-apiserver/app/testing/
curl -sL https://gist.githubusercontent.com/mauriciopoppe/508635e973bb8795cab2241c9a520edc/raw/apiserver_mem_bench_test.go \
  -o cmd/kube-apiserver/app/testing/apiserver_mem_bench_test.go

# 2. Run on baseline (upstream/master)
RUN_APISERVER_MEM_BENCH=1 BENCH_OUT_DIR=/tmp/bench-before \
  go test -v -count=1 -timeout=10m \
  ./cmd/kube-apiserver/app/testing \
  -run=^TestAPIServerMemoryIntegration$

# 3. Switch to the target PR branch and run again
RUN_APISERVER_MEM_BENCH=1 BENCH_OUT_DIR=/tmp/bench-after \
  go test -v -count=1 -timeout=10m \
  ./cmd/kube-apiserver/app/testing \
  -run=^TestAPIServerMemoryIntegration$

# 4. Compare retained live heap (inuse_space) or cumulative allocations (alloc_space)
go tool pprof -text -inuse_space \
  -base=/tmp/bench-before/apiserver_heap.pb.gz \
  /tmp/bench-after/apiserver_heap.pb.gz

go tool pprof -text -alloc_space \
  -base=/tmp/bench-before/apiserver_heap.pb.gz \
  /tmp/bench-after/apiserver_heap.pb.gz

What We Achieved Across the Optimization Campaign

Using TestAPIServerMemoryIntegration as the evaluation harness across 95 candidate trials (v000 baseline through v095), 16 source-level optimizations reduced kube-apiserver memory consumption and allocation churn without degrading request latency:

Hillclimbing Progression Across 95 Trials

Metric (5-Iteration Trimmed Mean) Baseline (v000) Optimized (v095) Delta
Live Retained Heap (pprof inuse_space) 79.95 MB 42.57 MB -37.38 MB (-46.75%)
Active Go Runtime Spans (HeapInuse) 115.64 MB 66.19 MB -49.45 MB (-42.76%)
Span Fragmentation Overhead (HeapInuse - HeapAlloc) 31.38 MB 18.62 MB -12.76 MB (-40.66%)
Live Heap Objects (HeapObjects) 418,034 273,042 -144,992 (-34.68%)
Cumulative Allocation Churn (TotalAlloc) 1,026.70 MB 696.67 MB -330.03 MB (-32.14%)
Process Resident Set Size (VmRSS, Median) 411.63 MB 304.90 MB -106.73 MB (-25.93%)
Mean Per-Round API Latency 159.87 ms 135.27 ms -24.60 ms (-15.39%)

Upstream Pull Requests Measured with This Harness

Each pull request below was measured both in TestAPIServerMemoryIntegration and in isolation against upstream BenchmarkRequestCost (test/integration/apiserver/flowcontrol/cost/cost_test.go, n=6, with the memory lifecycle metrics added in kubernetes/kubernetes#143469):

Pull Request Component Optimization Summary Measured Impact in TestAPIServerMemoryIntegration Isolated Impact in BenchmarkRequestCost (cost_test.go, n=6) Status
kubernetes/kubernetes#142807 k8s.io/apiserver (mux, endpoints) Share read-only pretty QueryParameter, cache AddObjectParams per reflect.Type, and drop PathRecorderMux.pathStacks map -2.17 MB retained heap (inuse_space), -2.51 MB HeapInuse, -12,941 live objects -3.04% heap-inuse-B (97.43 -> 94.47 MiB, -2.96 MiB), -2.81% heap-alloc-B, -2.23% runtime-inuse-B, -1.36% setup-alloc-B Open
kubernetes/kubernetes#142808 pkg/printers/internalversion Hoist 68 []metav1.TableColumnDefinition slices in AddHandlers to package-level variables -1.44 MB retained heap in AddHandlers (-98.3%), -1.82 MB HeapInuse, -4,289 live objects -1.37% setup-alloc-B (161.9 -> 159.7 MiB, -2.2 MiB), -2.80% heap-alloc-B (-3.0 MiB), -2.81% heap-inuse-B (-4.9 MiB), -2.96% runtime-inuse-B Merged (2026-10-13)
kubernetes/kubernetes#142811 k8s.io/apiserver/pkg/endpoints/handlers/responsewriters Pool deferredResponseWriter and its bytes.Buffer (512 KiB cap) via sync.Pool and remove dead req.WithContext(ctx) -23.66 MB cumulative TotalAlloc in deferredResponseWriter.Write, -80.27% to -81.71% Gzip Protobuf B/op (BenchmarkSerializeObject) -0.02% LIST N=100 rv=0 (json) B/op (-0.04% allocs/op), -7.75% DELETECOLLECTION N=100 sec/op (-1.51% suite sec/op geomean) Open
kubernetes/kubernetes#142814 k8s.io/apimachinery/pkg/runtime Add (*Unknown).UnmarshalRawZeroCopy in types_proto.go so protobuf.Serializer.Decode avoids copying the inner Unknown.Raw envelope slice -113.66 MB cumulative TotalAlloc (-11.11%), -50.85% BenchmarkSerializerDecode B/op (-47.15% latency) -13.26% DELETECOLLECTION N=100 (protobuf) B/op (70.61 -> 61.25 MiB/op, -12.47% verified live on Prow PerfDash), -2.76% suite B/op geomean Merged (2026-10-30)
kubernetes/kube-openapi#655 k8s.io/kube-openapi (builder, builder3) Replace reflect.DeepEqual boxing in WrapRefs with hasSiblingElements + copy-on-write walking and defer non-error json.Marshal in findCommonParameters 4.27x faster BenchmarkWrapRefs (-63.5% B/op, -69.7% allocs/op), 1.44x faster builder3 spec build -7.22% setup-alloc-B (159.6 -> 148.1 MiB, -11.5 MiB), -6.24% heap-inuse-B (169.6 -> 159.0 MiB, -10.6 MiB), -5.21% runtime-inuse-B Open
kubernetes/kube-openapi#656 k8s.io/kube-openapi (handler3) Gzip-compress cached OpenAPI V3 JSON specs in openAPIV3Group.jsonCache and serve pre-compressed bytes on Accept-Encoding: gzip -97.3% cached spec memory (604 KB -> 16 KB), 15.0x faster gzip serve latency, -5.51 MB net kube-apiserver live heap (3 GroupVersions) N/A (cost_test.go only queries /api/v1/.../pods and does not scrape /openapi/v3/...) Open
emicklei/go-restful#599 & kubernetes/kubernetes#142849 github.com/emicklei/go-restful/v3 Cache compiled route path *regexp.Regexp state machines in newPathExpression and embed ParameterData by value inside Parameter (v3.14.0 bump in #142849) 17.1x faster route registration (-95.4% B/op), -50% parameter allocs, -3.54 MB kube-apiserver live heap -6.26% setup-alloc-B (170.5 -> 159.8 MiB, -10.7 MiB), -6.43% heap-alloc-B (112.6 -> 105.4 MiB, -7.2 MiB), -4.78% heap-inuse-B (-8.5 MiB) Merged (2026-10-08 & 2026-10-16)
/*
Copyright 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.
*/
// Reproduction commands (from the root of a kubernetes/kubernetes checkout):
//
// curl -sL <RAW_GIST_URL> -o cmd/kube-apiserver/app/testing/apiserver_mem_bench_test.go
// RUN_APISERVER_MEM_BENCH=1 BENCH_OUT_DIR=/tmp/bench-after \
// go test -v -count=5 -timeout=10m ./cmd/kube-apiserver/app/testing -run=^TestAPIServerMemoryIntegration$
// go tool pprof -text -alloc_space -base=/tmp/bench-before/apiserver_heap.pb.gz /tmp/bench-after/apiserver_heap.pb.gz
package testing
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"runtime/pprof"
"runtime/trace"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
etcd3testing "k8s.io/apiserver/pkg/storage/etcd3/testing"
"k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
"k8s.io/kubernetes/test/utils/ktesting"
)
type MemoryTelemetry struct {
HeapInuseMB float64 `json:"heap_inuse_mb"`
HeapAllocMB float64 `json:"heap_alloc_mb"`
HeapSysMB float64 `json:"heap_sys_mb"`
HeapIdleMB float64 `json:"heap_idle_mb"`
HeapReleasedMB float64 `json:"heap_released_mb"`
SpanFragOverheadMB float64 `json:"span_frag_overhead_mb"`
TotalAllocMB float64 `json:"total_alloc_mb"`
WorkloadAllocMB float64 `json:"workload_alloc_mb"`
Mallocs uint64 `json:"mallocs"`
Frees uint64 `json:"frees"`
LiveObjects uint64 `json:"live_objects"`
NumGC uint32 `json:"num_gc"`
GCPauseTotalMs float64 `json:"gc_pause_total_ms"`
Goroutines int `json:"goroutines"`
SysMB float64 `json:"sys_mb"`
ProcessRSSMB float64 `json:"process_rss_mb"`
CgroupWorkingSetMB float64 `json:"cgroup_working_set_mb"`
APILatencyMeanMs float64 `json:"api_latency_mean_ms"`
EndpointLatenciesMs map[string]float64 `json:"endpoint_latencies_ms"`
GOGC int `json:"gogc"`
}
func readProcessRSSMB() float64 {
data, err := os.ReadFile("/proc/self/status")
if err != nil {
return 0
}
for _, line := range strings.Split(string(data), "\n") {
if strings.HasPrefix(line, "VmRSS:") {
fields := strings.Fields(line)
if len(fields) >= 2 {
kb, err := strconv.ParseFloat(fields[1], 64)
if err == nil {
return kb / 1024.0
}
}
}
}
return 0
}
func readCgroupWorkingSetMB() float64 {
curBytes, err := os.ReadFile("/sys/fs/cgroup/memory.current")
if err != nil {
return 0
}
cur, err := strconv.ParseFloat(strings.TrimSpace(string(curBytes)), 64)
if err != nil {
return 0
}
var inactiveFile float64
if statBytes, err := os.ReadFile("/sys/fs/cgroup/memory.stat"); err == nil {
for _, line := range strings.Split(string(statBytes), "\n") {
if strings.HasPrefix(line, "inactive_file ") {
fields := strings.Fields(line)
if len(fields) >= 2 {
inactiveFile, _ = strconv.ParseFloat(fields[1], 64)
}
}
}
}
ws := cur - inactiveFile
if ws < 0 {
ws = cur
}
return ws / (1024.0 * 1024.0)
}
// TestAPIServerMemoryIntegration boots an in-process kube-apiserver with embedded etcd,
// seeds 10 bound CEL ValidatingAdmissionPolicies + 60 system ConfigMaps (20 KB each,
// ~1.25 MB per LIST), drives concurrent 8-worker load across ConfigMap Protobuf LIST,
// JSON gzip LIST, GET, UPDATE (triggering CEL admission), and Kubernetes 1.27+ OpenAPI V3
// GroupVersion chunk discovery, runs a single synchronous GC cycle (collecting dead
// request objects while preserving warm sync.Pool victim caches as in steady-state
// production), dumps a live pprof heap profile, and records exact memory telemetry.
func TestAPIServerMemoryIntegration(t *testing.T) {
if os.Getenv("RUN_APISERVER_MEM_BENCH") != "1" {
t.Skip("Skipping kube-apiserver memory integration benchmark unless RUN_APISERVER_MEM_BENCH=1")
}
gogc := 50
if v := os.Getenv("GOGC_PERCENTAGE"); v != "" {
if parsed, err := strconv.Atoi(v); err == nil && parsed > 0 {
gogc = parsed
}
}
debug.SetGCPercent(gogc)
runtime.MemProfileRate = 1
outDir := os.Getenv("BENCH_OUT_DIR")
if outDir == "" {
outDir = t.TempDir()
}
if err := os.MkdirAll(outDir, 0755); err != nil {
t.Fatalf("mkdir %s: %v", outDir, err)
}
tCtx := ktesting.Init(t).WithoutCancel()
_, storageConfig := etcd3testing.NewUnsecuredEtcd3TestClientServer(t)
instanceOptions := NewDefaultTestServerOptions()
instanceOptions.DisableInvariantChecks = true
if pPath, err := pkgPath(t); err == nil && pPath != "" {
_ = os.MkdirAll(filepath.Join(pPath, "testdata"), 0755)
}
server, err := StartTestServer(tCtx, instanceOptions, nil, storageConfig)
if err != nil {
t.Fatalf("failed to start in-process kube-apiserver: %v", err)
}
defer server.TearDownFn()
jsonConfig := restclient.CopyConfig(server.ClientConfig)
jsonConfig.QPS = -1
jsonConfig.Burst = 1000
client, err := kubernetes.NewForConfig(jsonConfig)
if err != nil {
t.Fatalf("failed to create kubernetes client: %v", err)
}
protoConfig := restclient.CopyConfig(server.ClientConfig)
protoConfig.AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json"
protoConfig.ContentType = "application/vnd.kubernetes.protobuf"
protoConfig.QPS = -1
protoConfig.Burst = 1000
protoClient, err := kubernetes.NewForConfig(protoConfig)
if err != nil {
t.Fatalf("failed to create protobuf client: %v", err)
}
transport, err := restclient.TransportFor(server.ClientConfig)
if err != nil {
t.Fatalf("failed to build transport: %v", err)
}
httpClient := &http.Client{Transport: transport, Timeout: 30 * time.Second}
baseURL := server.ClientConfig.Host
ctx := context.Background()
endpointLatencies := make(map[string]float64)
// 1. Seed 10 ValidatingAdmissionPolicies + 10 ValidatingAdmissionPolicyBindings
// so CEL environment compilation, regexp compilation, and per-write CEL admission evaluation run.
failPolicy := admissionregistrationv1.Fail
matchPolicy := admissionregistrationv1.Equivalent
for i := 0; i < 10; i++ {
policyName := fmt.Sprintf("bench-cel-policy-%02d", i)
vap := &admissionregistrationv1.ValidatingAdmissionPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: policyName,
},
Spec: admissionregistrationv1.ValidatingAdmissionPolicySpec{
FailurePolicy: &failPolicy,
MatchConstraints: &admissionregistrationv1.MatchResources{
MatchPolicy: &matchPolicy,
ResourceRules: []admissionregistrationv1.NamedRuleWithOperations{
{
RuleWithOperations: admissionregistrationv1.RuleWithOperations{
Operations: []admissionregistrationv1.OperationType{
admissionregistrationv1.Create,
admissionregistrationv1.Update,
},
Rule: admissionregistrationv1.Rule{
APIGroups: []string{""},
APIVersions: []string{"v1"},
Resources: []string{"configmaps"},
},
},
},
},
},
Validations: []admissionregistrationv1.Validation{
{
Expression: fmt.Sprintf("has(object.metadata.name) && !object.metadata.name.startsWith('forbidden-%d-') && object.metadata.name.matches('^[a-z0-9-]+$')", i),
Message: "ConfigMap name must match allowed pattern and not start with forbidden prefix",
},
},
},
}
_, _ = client.AdmissionregistrationV1().ValidatingAdmissionPolicies().Create(ctx, vap, metav1.CreateOptions{})
binding := &admissionregistrationv1.ValidatingAdmissionPolicyBinding{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("bench-cel-binding-%02d", i),
},
Spec: admissionregistrationv1.ValidatingAdmissionPolicyBindingSpec{
PolicyName: policyName,
ValidationActions: []admissionregistrationv1.ValidationAction{admissionregistrationv1.Deny},
},
}
_, _ = client.AdmissionregistrationV1().ValidatingAdmissionPolicyBindings().Create(ctx, binding, metav1.CreateOptions{})
}
// 2. Seed 60 representative system ConfigMaps (20 KB each -> ~1.25 MB per LIST response)
// with labels, annotations, and managedFields.
const numConfigMaps = 60
payload := strings.Repeat("system-configuration-payload-line-0123456789abcdef\n", 370)
for i := 0; i < numConfigMaps; i++ {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("system-cm-%02d", i),
Namespace: "kube-system",
Labels: map[string]string{
"app.kubernetes.io/managed-by": "kube-apiserver-bench",
"app.kubernetes.io/component": "control-plane",
"k8s-app": "kube-apiserver",
"tier": "control-plane",
},
Annotations: map[string]string{
"bench.k8s.io/layer": "core",
},
},
Data: map[string]string{
"config.yaml": payload,
},
}
if _, err := client.CoreV1().ConfigMaps("kube-system").Create(ctx, cm, metav1.CreateOptions{
FieldManager: "apiserver-bench-manager",
}); err != nil {
t.Fatalf("failed to seed configmap %d: %v", i, err)
}
}
var msAfterBoot runtime.MemStats
runtime.ReadMemStats(&msAfterBoot)
// Start CPU profile and Go runtime execution trace across the active concurrent request phase
cpuProfFile, err := os.Create(filepath.Join(outDir, "apiserver_cpu.pb.gz"))
if err == nil {
_ = pprof.StartCPUProfile(cpuProfFile)
}
traceFile, err := os.Create(filepath.Join(outDir, "apiserver_trace.out"))
if err == nil {
_ = trace.Start(traceFile)
}
// 3. Drive concurrent multi-worker load (8 workers x 8 rounds = 64 rounds):
// - Protobuf ConfigMap LIST (~1.25 MB per response)
// - JSON gzip ConfigMap LIST (~1.25 MB JSON)
// - ConfigMap GET (Protobuf + JSON)
// - ConfigMap UPDATE (exercises bound CEL ValidatingAdmissionPolicies & ObjectMeta managedFields)
const numWorkers = 8
const roundsPerWorker = 8
var cmLatencyNs atomic.Int64
var cmErr atomic.Value
var wg sync.WaitGroup
for w := 0; w < numWorkers; w++ {
workerID := w
wg.Add(1)
go func() {
defer wg.Done()
for r := 0; r < roundsPerWorker; r++ {
rt0 := time.Now()
if _, err := protoClient.CoreV1().ConfigMaps("kube-system").List(ctx, metav1.ListOptions{}); err != nil {
cmErr.Store(fmt.Errorf("worker %d protobuf list: %w", workerID, err))
return
}
// JSON LIST with gzip compression
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"/api/v1/namespaces/kube-system/configmaps", nil)
if err != nil {
cmErr.Store(err)
return
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Accept-Encoding", "gzip")
resp, err := httpClient.Do(req)
if err != nil {
cmErr.Store(fmt.Errorf("worker %d json gzip list: %w", workerID, err))
return
}
if resp.Header.Get("Content-Encoding") == "gzip" && resp.StatusCode == http.StatusOK {
gr, gerr := gzip.NewReader(resp.Body)
if gerr == nil {
_, _ = io.Copy(io.Discard, gr)
_ = gr.Close()
}
} else {
_, _ = io.Copy(io.Discard, resp.Body)
}
_ = resp.Body.Close()
// 3 Protobuf GETs + 3 JSON GETs
for i := 0; i < 3; i++ {
cmIdx := (workerID*6 + r + i) % numConfigMaps
name := fmt.Sprintf("system-cm-%02d", cmIdx)
if _, err := protoClient.CoreV1().ConfigMaps("kube-system").Get(ctx, name, metav1.GetOptions{}); err != nil {
cmErr.Store(fmt.Errorf("worker %d proto get: %w", workerID, err))
return
}
if _, err := client.CoreV1().ConfigMaps("kube-system").Get(ctx, name, metav1.GetOptions{}); err != nil {
cmErr.Store(fmt.Errorf("worker %d json get: %w", workerID, err))
return
}
}
// 1 ConfigMap Update to exercise bound CEL ValidatingAdmissionPolicies & ManagedFields
targetName := fmt.Sprintf("system-cm-%02d", workerID)
cmObj, err := client.CoreV1().ConfigMaps("kube-system").Get(ctx, targetName, metav1.GetOptions{})
if err != nil {
cmErr.Store(fmt.Errorf("worker %d get for update: %w", workerID, err))
return
}
if cmObj.Annotations == nil {
cmObj.Annotations = make(map[string]string)
}
cmObj.Annotations["bench.k8s.io/round"] = strconv.Itoa(r)
if _, err := client.CoreV1().ConfigMaps("kube-system").Update(ctx, cmObj, metav1.UpdateOptions{
FieldManager: fmt.Sprintf("bench-worker-%02d", workerID),
}); err != nil {
cmErr.Store(fmt.Errorf("worker %d update: %w", workerID, err))
return
}
cmLatencyNs.Add(time.Since(rt0).Nanoseconds())
}
}()
}
wg.Wait()
if v := cmErr.Load(); v != nil {
t.Fatalf("concurrent configmap workload failed: %v", v)
}
totalCMRounds := float64(numWorkers * roundsPerWorker)
endpointLatencies["configmap_list_get_ms"] = (float64(cmLatencyNs.Load()) / 1e6) / totalCMRounds
// 4. Exercise Kubernetes 1.27+ OpenAPI V3 GroupVersion chunk requests concurrently across 8 workers.
v3Paths := []string{
"/openapi/v3/api/v1",
"/openapi/v3/apis/apps/v1",
"/openapi/v3/apis/admissionregistration.k8s.io/v1",
}
const v3RoundsPerWorker = 2
var v3LatencyNs atomic.Int64
var v3Err atomic.Value
for w := 0; w < numWorkers; w++ {
wg.Add(1)
go func() {
defer wg.Done()
for r := 0; r < v3RoundsPerWorker; r++ {
for _, p := range v3Paths {
reqT0 := time.Now()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+p, nil)
if err != nil {
v3Err.Store(err)
return
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Accept-Encoding", "gzip")
resp, err := httpClient.Do(req)
if err != nil {
v3Err.Store(fmt.Errorf("GET %s failed: %w", p, err))
return
}
if resp.Header.Get("Content-Encoding") == "gzip" && resp.StatusCode == http.StatusOK {
gr, gerr := gzip.NewReader(resp.Body)
if gerr == nil {
_, _ = io.Copy(io.Discard, gr)
_ = gr.Close()
}
} else {
_, _ = io.Copy(io.Discard, resp.Body)
}
_ = resp.Body.Close()
v3LatencyNs.Add(time.Since(reqT0).Nanoseconds())
}
}
}()
}
wg.Wait()
if v := v3Err.Load(); v != nil {
t.Fatalf("concurrent openapi v3 workload failed: %v", v)
}
totalV3Reqs := float64(numWorkers * v3RoundsPerWorker * len(v3Paths))
endpointLatencies["openapi_v3_ms"] = (float64(v3LatencyNs.Load()) / 1e6) / totalV3Reqs
if traceFile != nil {
trace.Stop()
_ = traceFile.Close()
}
if cpuProfFile != nil {
pprof.StopCPUProfile()
_ = cpuProfFile.Close()
}
// 5. Run a single synchronous GC + OS scavenger cycle to collect unreachable
// request allocations while preserving warm sync.Pool caches.
runtime.GC()
debug.FreeOSMemory()
time.Sleep(50 * time.Millisecond)
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
var profBuf bytes.Buffer
if err := pprof.WriteHeapProfile(&profBuf); err != nil {
t.Fatalf("WriteHeapProfile: %v", err)
}
profPath := filepath.Join(outDir, "apiserver_heap.pb.gz")
if err := os.WriteFile(profPath, profBuf.Bytes(), 0644); err != nil {
t.Fatalf("write %s: %v", profPath, err)
}
if goroutineProf := pprof.Lookup("goroutine"); goroutineProf != nil {
var gBuf bytes.Buffer
_ = goroutineProf.WriteTo(&gBuf, 0)
_ = os.WriteFile(filepath.Join(outDir, "apiserver_goroutine.pb.gz"), gBuf.Bytes(), 0644)
}
rssMB := readProcessRSSMB()
wsMB := readCgroupWorkingSetMB()
if wsMB <= 0 {
wsMB = rssMB
}
var totalLat float64
for _, v := range endpointLatencies {
totalLat += v
}
meanLat := totalLat / float64(len(endpointLatencies))
heapInuseMB := float64(ms.HeapInuse) / (1024.0 * 1024.0)
heapAllocMB := float64(ms.HeapAlloc) / (1024.0 * 1024.0)
totalAllocMB := float64(ms.TotalAlloc) / (1024.0 * 1024.0)
workloadAllocMB := float64(ms.TotalAlloc-msAfterBoot.TotalAlloc) / (1024.0 * 1024.0)
telemetry := MemoryTelemetry{
HeapInuseMB: heapInuseMB,
HeapAllocMB: heapAllocMB,
HeapSysMB: float64(ms.HeapSys) / (1024.0 * 1024.0),
HeapIdleMB: float64(ms.HeapIdle) / (1024.0 * 1024.0),
HeapReleasedMB: float64(ms.HeapReleased) / (1024.0 * 1024.0),
SpanFragOverheadMB: heapInuseMB - heapAllocMB,
TotalAllocMB: totalAllocMB,
WorkloadAllocMB: workloadAllocMB,
Mallocs: ms.Mallocs,
Frees: ms.Frees,
LiveObjects: ms.Mallocs - ms.Frees,
NumGC: ms.NumGC,
GCPauseTotalMs: float64(ms.PauseTotalNs) / 1e6,
Goroutines: runtime.NumGoroutine(),
SysMB: float64(ms.Sys) / (1024.0 * 1024.0),
ProcessRSSMB: rssMB,
CgroupWorkingSetMB: wsMB,
APILatencyMeanMs: meanLat,
EndpointLatenciesMs: endpointLatencies,
GOGC: gogc,
}
telemetryBytes, err := json.MarshalIndent(telemetry, "", " ")
if err != nil {
t.Fatalf("marshal telemetry: %v", err)
}
telemetryPath := filepath.Join(outDir, "apiserver_telemetry.json")
if err := os.WriteFile(telemetryPath, telemetryBytes, 0644); err != nil {
t.Fatalf("write %s: %v", telemetryPath, err)
}
t.Logf("APISERVER_MEM_TELEMETRY=%s", string(telemetryBytes))
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment