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

RFC: faster implementation of KObj #261

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
77 changes: 77 additions & 0 deletions benchmark/kobj_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright 2021 The Kubernetes Authors.

SPDX-License-Identifier: Apache-2.0
*/

package example

import (
"testing"

"github.com/go-logr/logr"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
)

type kmeta struct {
name, namespace string
}

func (k kmeta) GetName() string {
return k.name
}

func (k kmeta) GetNamespace() string {
return k.namespace
}

var _ klog.KMetadata = kmeta{}

// var obj = kmeta{name: "some-fake-name", namespace: "kube-system"}
var obj = v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "some-fake-name", Namespace: "kube-system"}}

var result string

func BenchmarkKObjByPointer(b *testing.B) {
var s string
for n := 0; n < b.N; n++ {
s = klog.KObj(&obj).String()
}
result = s
}

func BenchmarkKObj2ByPointer(b *testing.B) {
var s string
for n := 0; n < b.N; n++ {
s = klog.KObj2(&obj).String()
}
result = s
}

func BenchmarkSkipObjByPointer(b *testing.B) {
for n := 0; n < b.N; n++ {
klog.V(10).InfoS("skipped", "obj", klog.KObj(&obj))
}
}

func BenchmarkSkipObj2ByPointer(b *testing.B) {
for n := 0; n < b.N; n++ {
klog.V(10).InfoS("skipped", "obj", klog.KObj2(&obj))
}
}

func BenchmarkDiscardObjByPointer(b *testing.B) {
log := logr.Discard()
for n := 0; n < b.N; n++ {
log.Info("skipped", klog.KObj(&obj))
}
}

func BenchmarkDiscardObj2ByPointer(b *testing.B) {
log := logr.Discard()
for n := 0; n < b.N; n++ {
log.Info("skipped", klog.KObj2(&obj))
}
}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module k8s.io/klog/v2

go 1.13

require github.com/go-logr/logr v1.0.0
require (
github.com/go-logr/logr v1.0.0
k8s.io/api v0.22.1 // indirect
k8s.io/apimachinery v0.22.1 // indirect
)