Last active
November 6, 2023 03:04
-
-
Save fuweid/4c99d3633a097997b1f4914e8ca34462 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"flag" | |
"fmt" | |
"strings" | |
"sync" | |
corev1 "k8s.io/api/core/v1" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/tools/clientcmd" | |
"k8s.io/client-go/util/flowcontrol" | |
// | |
// Uncomment to load all auth plugins | |
// _ "k8s.io/client-go/plugin/pkg/client/auth" | |
// | |
// Or uncomment to load specific auth plugins | |
// _ "k8s.io/client-go/plugin/pkg/client/auth/azure" | |
// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" | |
// _ "k8s.io/client-go/plugin/pkg/client/auth/oidc" | |
) | |
func main() { | |
var kubeconfig *string | |
kubeconfig = flag.String("kubeconfig", "xxxx", "absolute path to the kubeconfig file") | |
flag.Parse() | |
// use the current context in kubeconfig | |
fmt.Println(*kubeconfig) | |
if len(*kubeconfig) == 0 { | |
panic("emtpy kubeconfig") | |
} | |
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig) | |
if err != nil { | |
panic(err) | |
} | |
config.QPS = 100 | |
config.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(100, 10) | |
clientset, err := kubernetes.NewForConfig(config) | |
if err != nil { | |
panic(err) | |
} | |
data := strings.Repeat("A", 1024*12) | |
n, batch := 12000, 40 | |
for i := 0; i < n; i = i + batch { | |
ownerId := i | |
var wg sync.WaitGroup | |
for j := i; j < i+batch && j < n; j++ { | |
wg.Add(1) | |
go func(jj int) { | |
defer wg.Done() | |
cli := clientset.CoreV1().ConfigMaps("perf-test") | |
name := fmt.Sprintf("cm-%d", jj) | |
fmt.Println(name) | |
cm := &corev1.ConfigMap{} | |
cm.Name = name | |
cm.Labels = map[string]string{ | |
"ownerId": fmt.Sprintf("%d", ownerId), | |
} | |
cm.Data = map[string]string{ | |
"12k.data": data, | |
} | |
_, err := cli.Create(context.TODO(), cm, metav1.CreateOptions{}) | |
if err != nil { | |
panic(err) | |
} | |
}(j) | |
} | |
wg.Wait() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"math/rand" | |
"os" | |
"path/filepath" | |
"strconv" | |
"sync" | |
"time" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/apimachinery/pkg/labels" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/tools/clientcmd" | |
"k8s.io/client-go/util/flowcontrol" | |
// | |
// Uncomment to load all auth plugins | |
// _ "k8s.io/client-go/plugin/pkg/client/auth" | |
// | |
// Or uncomment to load specific auth plugins | |
// _ "k8s.io/client-go/plugin/pkg/client/auth/azure" | |
// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" | |
// _ "k8s.io/client-go/plugin/pkg/client/auth/oidc" | |
// _ "k8s.io/client-go/plugin/pkg/client/auth/openstack" | |
) | |
var kubeconfigInStr = ` | |
` | |
func main() { | |
// creates the in-cluster config | |
/* | |
config, err := rest.InClusterConfig() | |
if err != nil { | |
panic(err.Error()) | |
} | |
*/ | |
dir, err := os.MkdirTemp("", "example") | |
if err != nil { | |
panic(err) | |
} | |
kubeconfig := filepath.Join(dir, "kubeconfig") | |
if err := os.WriteFile(kubeconfig, []byte(kubeconfigInStr), 0600); err != nil { | |
panic(err) | |
} | |
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) | |
if err != nil { | |
panic(err.Error()) | |
} | |
config.QPS = 400 | |
config.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(400, 10) | |
// creates the clientset | |
clientset, err := kubernetes.NewForConfig(config) | |
if err != nil { | |
panic(err.Error()) | |
} | |
var wg sync.WaitGroup | |
for i := 0; i < 300; i++ { | |
wg.Add(1) | |
go func(j int) { | |
defer wg.Done() | |
time.Sleep(time.Duration(rand.Intn(5)) * time.Second) | |
list, err := clientset.CoreV1().ConfigMaps("perf-test").List(context.TODO(), | |
metav1.ListOptions{ | |
LabelSelector: labels.SelectorFromSet(map[string]string{"ownerId": strconv.Itoa(j * 40)}).String(), | |
}, | |
) | |
if err != nil { | |
fmt.Println(err.Error()) | |
return | |
} | |
}(i) | |
} | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment