|
package main |
|
|
|
import ( |
|
"fmt" |
|
"math/rand" |
|
"testing" |
|
|
|
v1 "k8s.io/api/core/v1" |
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
|
) |
|
|
|
func generatePods(n int) map[int]v1.Pod { |
|
result := map[int]v1.Pod{} |
|
fakeData := RandStringRunes(10000) |
|
for i := 0; i < n; i++ { |
|
result[i] = v1.Pod{ |
|
ObjectMeta: metav1.ObjectMeta{ |
|
Name: fmt.Sprintf("pod%d", i), |
|
Namespace: "testns", |
|
}, |
|
Spec: v1.PodSpec{ |
|
Containers: []v1.Container{ |
|
{ |
|
Command: []string{"/bin/bash", "-c", "exec sleep 10000", fakeData}, |
|
Name: "hostexec", |
|
Image: "centos:7", |
|
ImagePullPolicy: v1.PullIfNotPresent, |
|
}, |
|
{ |
|
Command: []string{"/bin/bash", "-c", "exec sleep 10000", fakeData}, |
|
Name: "hostexec", |
|
Image: "centos:7", |
|
ImagePullPolicy: v1.PullIfNotPresent, |
|
}, |
|
{ |
|
Command: []string{"/bin/bash", "-c", "exec sleep 10000", fakeData}, |
|
Name: "hostexec", |
|
Image: "centos:7", |
|
ImagePullPolicy: v1.PullIfNotPresent, |
|
}, |
|
{ |
|
Command: []string{"/bin/bash", "-c", "exec sleep 10000", fakeData}, |
|
Name: "hostexec", |
|
Image: "centos:7", |
|
ImagePullPolicy: v1.PullIfNotPresent, |
|
}, |
|
{ |
|
Command: []string{"/bin/bash", "-c", "exec sleep 10000", fakeData}, |
|
Name: "hostexec", |
|
Image: "centos:7", |
|
ImagePullPolicy: v1.PullIfNotPresent, |
|
}, |
|
{ |
|
Command: []string{"/bin/bash", "-c", "exec sleep 10000", fakeData}, |
|
Name: "hostexec", |
|
Image: "centos:7", |
|
ImagePullPolicy: v1.PullIfNotPresent, |
|
}, |
|
}, |
|
HostNetwork: true, |
|
}, |
|
} |
|
} |
|
return result |
|
} |
|
|
|
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") |
|
|
|
func RandStringRunes(n int) string { |
|
b := make([]rune, n) |
|
for i := range b { |
|
b[i] = letterRunes[rand.Intn(len(letterRunes))] |
|
} |
|
return string(b) |
|
} |
|
|
|
func IterateValue(pods map[int]v1.Pod) { |
|
found := false |
|
for _, pod := range pods { |
|
if pod.Name == "hola" { |
|
found = true |
|
} |
|
} |
|
if found { |
|
|
|
} |
|
|
|
} |
|
|
|
func IterateKey(pods map[int]v1.Pod) { |
|
found := false |
|
for i, _ := range pods { |
|
if pods[i].Name == "hola" { |
|
found = true |
|
} |
|
} |
|
if found { |
|
|
|
} |
|
} |
|
|
|
func Benchmark_Key(b *testing.B) { |
|
pods := generatePods(1000) |
|
for i := 0; i < b.N; i++ { |
|
IterateKey(pods) |
|
} |
|
} |
|
|
|
func Benchmark_Value(b *testing.B) { |
|
pods := generatePods(1000) |
|
for i := 0; i < b.N; i++ { |
|
IterateValue(pods) |
|
} |
|
} |