
By default, the generated spec will place containers in cgroups by their ids, we need to use the namespace as the cgroup root to avoid containers with the same name being placed in the same cgroup. ``` 11:perf_event:/to/redis 10:freezer:/to/redis 9:memory:/to/redis 8:devices:/to/redis 7:net_cls,net_prio:/to/redis 6:pids:/to/redis 5:hugetlb:/to/redis 4:cpuset:/to/redis 3:blkio:/to/redis 2:cpu,cpuacct:/to/redis 1:name=systemd:/to/redis 11:perf_event:/te/redis 10:freezer:/te/redis 9:memory:/te/redis 8:devices:/te/redis 7:net_cls,net_prio:/te/redis 6:pids:/te/redis 5:hugetlb:/te/redis 4:cpuset:/te/redis 3:blkio:/te/redis 2:cpu,cpuacct:/te/redis 1:name=systemd:/te/redis ``` Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
108 lines
2.2 KiB
Go
108 lines
2.2 KiB
Go
package containerd
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/containerd/containerd/containers"
|
|
)
|
|
|
|
func BenchmarkContainerCreate(b *testing.B) {
|
|
client, err := newClient(b, address)
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
defer client.Close()
|
|
|
|
ctx, cancel := testContext()
|
|
defer cancel()
|
|
|
|
image, err := client.GetImage(ctx, testImage)
|
|
if err != nil {
|
|
b.Error(err)
|
|
return
|
|
}
|
|
spec, err := GenerateSpec(ctx, client, &containers.Container{ID: b.Name()}, WithImageConfig(image), withTrue())
|
|
if err != nil {
|
|
b.Error(err)
|
|
return
|
|
}
|
|
var containers []Container
|
|
defer func() {
|
|
for _, c := range containers {
|
|
if err := c.Delete(ctx, WithSnapshotCleanup); err != nil {
|
|
b.Error(err)
|
|
}
|
|
}
|
|
}()
|
|
|
|
// reset the timer before creating containers
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
id := fmt.Sprintf("%s-%d", b.Name(), i)
|
|
container, err := client.NewContainer(ctx, id, WithSpec(spec), WithNewSnapshot(id, image))
|
|
if err != nil {
|
|
b.Error(err)
|
|
return
|
|
}
|
|
containers = append(containers, container)
|
|
}
|
|
b.StopTimer()
|
|
}
|
|
|
|
func BenchmarkContainerStart(b *testing.B) {
|
|
client, err := newClient(b, address)
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
defer client.Close()
|
|
|
|
ctx, cancel := testContext()
|
|
defer cancel()
|
|
|
|
image, err := client.GetImage(ctx, testImage)
|
|
if err != nil {
|
|
b.Error(err)
|
|
return
|
|
}
|
|
spec, err := GenerateSpec(ctx, client, &containers.Container{ID: b.Name()}, WithImageConfig(image), withTrue())
|
|
if err != nil {
|
|
b.Error(err)
|
|
return
|
|
}
|
|
var containers []Container
|
|
defer func() {
|
|
for _, c := range containers {
|
|
if err := c.Delete(ctx, WithSnapshotCleanup); err != nil {
|
|
b.Error(err)
|
|
}
|
|
}
|
|
}()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
id := fmt.Sprintf("%s-%d", b.Name(), i)
|
|
container, err := client.NewContainer(ctx, id, WithSpec(spec), WithNewSnapshot(id, image))
|
|
if err != nil {
|
|
b.Error(err)
|
|
return
|
|
}
|
|
containers = append(containers, container)
|
|
|
|
}
|
|
// reset the timer before starting tasks
|
|
b.ResetTimer()
|
|
for _, c := range containers {
|
|
task, err := c.NewTask(ctx, empty())
|
|
if err != nil {
|
|
b.Error(err)
|
|
return
|
|
}
|
|
defer task.Delete(ctx)
|
|
if err := task.Start(ctx); err != nil {
|
|
b.Error(err)
|
|
return
|
|
}
|
|
}
|
|
b.StopTimer()
|
|
}
|