Skip to content

Commit

Permalink
chore: add comparison benchmarks
Browse files Browse the repository at this point in the history
Signed-off-by: Arjun Raja Yogidas <[email protected]>
  • Loading branch information
coderbirju committed Oct 10, 2024
1 parent 3525e55 commit 6349eac
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ test-benchmark-vm:

.PHONY: test-benchmark-container
test-benchmark-container:
cd benchmark/container && go test -ldflags $(LDFLAGS) -bench=. -benchmem --installed="$(INSTALLED)"
cd benchmark/container && go test -ldflags $(LDFLAGS) -bench=. -benchmem -benchtime=1x -timeout 2h --installed="$(INSTALLED)"

.PHONY: gen-code
# Since different projects may have different versions of tool binaries,
Expand Down
4 changes: 2 additions & 2 deletions benchmark/all/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func BenchmarkAll(b *testing.B) {
}

b.Run("BenchmarkContainerRun", func(b *testing.B) {
suite.BenchmarkContainerRun(b)
suite.BenchmarkContainerRun(b, "finch")
})

b.Run("BenchmarkImageBuild", func(b *testing.B) {
suite.BenchmarkImageBuild(b)
suite.BenchmarkImageBuild(b, "finch")
})

err = suite.StopVM()
Expand Down
53 changes: 53 additions & 0 deletions benchmark/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,59 @@ func GetSubject() (string, error) {
return subject, nil
}

// GetDocker gets the path of docker installation
func GetDocker() (string, error) {
subject := filepath.Join("/usr/bin/", "docker")
return subject, nil
}

// func CleanUpFunc() error {
// cmd0 := exec.Command("sudo", "systemctl", "stop", "docker")
// if err := cmd0.Run(); err != nil {
// return fmt.Errorf("docker stop failed: %v", err)
// }
// cmd1 := exec.Command("sudo", "rm", "-rf", "/var/lib/docker")
// if err := cmd1.Run(); err != nil {
// return fmt.Errorf("docker cleanup failed: %v", err)
// }
// cmd2 := exec.Command("sudo", "rm", "-rf", "/var/run/docker.pid")
// if err := cmd2.Run(); err != nil {
// return fmt.Errorf("failed to stop docker process: %v", err)
// }
// cmd3 := exec.Command("sudo", "systemctl", "restart", "docker")
// if err := cmd3.Run(); err != nil {
// return fmt.Errorf("docker failed to restart: %v", err)
// }

// cmd4 := exec.Command("sudo", "rm", "-rf", "/var/lib/finch/buildkit/cache.db")
// if err := cmd4.Run(); err != nil {
// return fmt.Errorf("finch buildkit cleanup failed: %v", err)
// }

// cmd5 := exec.Command("sudo", "docker", "builder", "prune", "-a", "-f")
// if err := cmd5.Run(); err != nil {
// return fmt.Errorf("docker builder prune failed: %v", err)
// }

// cmd6 := exec.Command("sudo", "docker", "image", "prune", "-a", "-f")
// if err := cmd6.Run(); err != nil {
// return fmt.Errorf("docker image prune failed: %v", err)
// }

// cmd7 := exec.Command("sudo", "rm", "-rf", "/var/lib/containerd")
// if err := cmd7.Run(); err != nil {
// return fmt.Errorf("containerd cleanup failed: %v", err)
// }

// cmd8 := exec.Command("sudo", "systemctl", "restart", "containerd")
// if err := cmd8.Run(); err != nil {
// return fmt.Errorf("docker failed to restart: %v", err)
// }

// return nil

// }

// Wrapper reports the benchmarking metrics of targetFunc to testing.B.
func Wrapper(b *testing.B, targetFunc func(), cleanupFunc func()) {
metricsSum := Metrics{}
Expand Down
33 changes: 29 additions & 4 deletions benchmark/container/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,36 @@ func BenchmarkContainer(b *testing.B) {
b.Fatal(err)
}

b.Run("BenchmarkContainerRun", func(b *testing.B) {
suite.BenchmarkContainerRun(b)
b.Run("BenchmarkContainerRun-docker", func(b *testing.B) {
suite.BenchmarkContainerRun(b, "docker")
})

b.Run("BenchmarkImageBuild", func(b *testing.B) {
suite.BenchmarkImageBuild(b)
b.Run("BenchmarkContainerRun-finch", func(b *testing.B) {
suite.BenchmarkContainerRun(b, "finch")
})

b.Run("BenchmarkContainerPull-docker", func(b *testing.B) {
suite.BenchmarkContainerPull(b, "docker")
})

b.Run("BenchmarkContainerPull-finch", func(b *testing.B) {
suite.BenchmarkContainerPull(b, "finch")
})

b.Run("BenchmarkImageBuild-docker", func(b *testing.B) {
suite.BenchmarkImageBuild(b, "docker")
})

b.Run("BenchmarkImageBuild-finch", func(b *testing.B) {
suite.BenchmarkImageBuild(b, "finch")
})

b.Run("BenchmarkImageDelete-docker", func(b *testing.B) {
suite.BenchmarkImageDelete(b, "docker")
})

b.Run("BenchmarkImageDelete-finch", func(b *testing.B) {
suite.BenchmarkImageDelete(b, "finch")
})

}
111 changes: 96 additions & 15 deletions benchmark/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ import (

const (
virtualMachineRootCmd = "vm"
alpineImage = "public.ecr.aws/docker/library/alpine:latest"
alpineImage = "public.ecr.aws/y0o4y9o3/anaconda-pkg-build:latest"
testImageName = "test:tag"
testContainerName = "ctr-test"
ligthImage = "public.ecr.aws/docker/library/amazonlinux:latest"
)

// public.ecr.aws/soci-workshop-examples/mongo:latest public.ecr.aws/soci-workshop-examples/redis:latest public.ecr.aws/docker/library/alpine:latest
// Suite is a struct that groups benchmark functions and shared state.
type Suite struct {
subject string
docker string
}

// Setup initializes the Suite by getting the subject.
Expand All @@ -32,7 +35,13 @@ func (suite *Suite) Setup() error {
if err != nil {
return err
}

docker, err := GetDocker()
if err != nil {
return err
}
suite.subject = subject
suite.docker = docker
return nil
}

Expand Down Expand Up @@ -80,18 +89,49 @@ func (suite *Suite) BenchmarkVMStart(b *testing.B) {
}

// BenchmarkContainerRun measures the metrics to run a container.
func (suite *Suite) BenchmarkContainerRun(b *testing.B) {
assert.NoError(b, exec.Command(suite.subject, "pull", alpineImage).Run()) //nolint:gosec // testing only
Wrapper(b, func() {
assert.NoError(b, exec.Command(suite.subject, "run", "--name", testContainerName, alpineImage).Run()) //nolint:gosec // testing only
}, func() {
assert.NoError(b, exec.Command(suite.subject, "rm", "--force", testContainerName).Run()) //nolint:gosec // testing only
})
assert.NoError(b, exec.Command(suite.subject, "rmi", "--force", alpineImage).Run()) //nolint:gosec // testing only
func (suite *Suite) BenchmarkContainerRun(b *testing.B, binaryName string) {
// assert.NoError(b, CleanUpFunc())
if binaryName == "finch" {
assert.NoError(b, exec.Command("sudo", suite.subject, "pull", alpineImage).Run()) //nolint:gosec // testing only
Wrapper(b, func() {
assert.NoError(b, exec.Command("sudo", suite.subject, "run", "--name", testContainerName, alpineImage).Run()) //nolint:gosec // testing only
}, func() {
assert.NoError(b, exec.Command("sudo", suite.subject, "rm", "--force", testContainerName).Run()) //nolint:gosec // testing only
})
assert.NoError(b, exec.Command("sudo", suite.subject, "rmi", "--force", alpineImage).Run()) //nolint:gosec // testing only
} else {
// assert.NoError(b, CleanUpFunc())
assert.NoError(b, exec.Command("sudo", suite.docker, "pull", alpineImage).Run()) //nolint:gosec // testing only
Wrapper(b, func() {
assert.NoError(b, exec.Command("sudo", suite.docker, "run", "--name", testContainerName, alpineImage).Run()) //nolint:gosec // testing only
}, func() {
assert.NoError(b, exec.Command("sudo", suite.docker, "rm", "--force", testContainerName).Run()) //nolint:gosec // testing only
})
assert.NoError(b, exec.Command("sudo", suite.docker, "rmi", "--force", alpineImage).Run()) //nolint:gosec // testing only
}

}

func (suite *Suite) BenchmarkContainerPull(b *testing.B, binaryName string) {
if binaryName == "finch" {
Wrapper(b, func() {
assert.NoError(b, exec.Command("sudo", suite.subject, "pull", alpineImage, "--namespace=finch").Run()) //nolint:gosec // testing only
}, func() {
assert.NoError(b, exec.Command("sudo", suite.subject, "rmi", "--force", alpineImage).Run()) //nolint:gosec // testing only
})
} else {
assert.NoError(b, exec.Command("sudo", suite.docker, "images", "prune", "-a").Run())
assert.NoError(b, exec.Command("sudo", suite.docker, "volume", "prune", "-a").Run())
Wrapper(b, func() {
assert.NoError(b, exec.Command("sudo", suite.docker, "pull", alpineImage).Run()) //nolint:gosec // testing only
}, func() {
assert.NoError(b, exec.Command("sudo", suite.docker, "rmi", "--force", alpineImage).Run()) //nolint:gosec // testing only
})
}
}

// BenchmarkImageBuild measures the metrics to build an image.
func (suite *Suite) BenchmarkImageBuild(b *testing.B) {
func (suite *Suite) BenchmarkImageBuild(b *testing.B, binaryName string) {
homeDir, err := os.UserHomeDir()
assert.NoError(b, err)
tempDir, err := os.MkdirTemp(homeDir, "finch-test")
Expand All @@ -103,9 +143,50 @@ func (suite *Suite) BenchmarkImageBuild(b *testing.B) {
assert.NoError(b, err)
buildContext := filepath.Dir(dockerFilePath)
defer os.RemoveAll(buildContext) //nolint:errcheck // testing only
Wrapper(b, func() {
assert.NoError(b, exec.Command(suite.subject, "build", "--tag", testImageName, buildContext).Run()) //nolint:gosec // testing only
}, func() {
assert.NoError(b, exec.Command(suite.subject, "rmi", "--force", testImageName).Run()) //nolint:gosec // testing only
})
// assert.NoError(b, CleanUpFunc())
if binaryName == "finch" {
assert.NoError(b, exec.Command("sudo", suite.subject, "builder", "prune").Run())
Wrapper(b, func() {
assert.NoError(b, exec.Command("sudo", suite.subject, "build", "--tag", testImageName, buildContext, "--namespace=finch").Run()) //nolint:gosec // testing only
}, func() {
assert.NoError(b, exec.Command("sudo", suite.subject, "rmi", "--force", testImageName).Run()) //nolint:gosec // testing only
})
} else {
// assert.NoError(b, CleanUpFunc())
assert.NoError(b, exec.Command("sudo", suite.docker, "builder", "prune").Run())
Wrapper(b, func() {
assert.NoError(b, exec.Command("sudo", suite.docker, "build", "--tag", testImageName, buildContext, "--no-cache").Run()) //nolint:gosec // testing only
}, func() {
assert.NoError(b, exec.Command("sudo", suite.docker, "rmi", "--force", testImageName).Run()) //nolint:gosec // testing only
})
}
}

func (suite *Suite) BenchmarkImageDelete(b *testing.B, binaryImage string) {
homeDir, err := os.UserHomeDir()
assert.NoError(b, err)
tempDir, err := os.MkdirTemp(homeDir, "finch-test")
assert.NoError(b, err)
dockerFilePath := filepath.Join(tempDir, "Dockerfile")
err = os.WriteFile(dockerFilePath, []byte(fmt.Sprintf(`FROM %s
CMD ["echo", "finch-test-dummy-output"]
`, alpineImage)), 0o600)
assert.NoError(b, err)
buildContext := filepath.Dir(dockerFilePath)
defer os.RemoveAll(buildContext) //nolint:errcheck // testing only
if binaryImage == "finch" {
assert.NoError(b, exec.Command("sudo", suite.subject, "build", "--tag", testImageName, buildContext).Run()) //nolint:gosec // testing only
Wrapper(b, func() {
assert.NoError(b, exec.Command("sudo", suite.subject, "rmi", "--force", testImageName).Run()) //nolint:gosec // testing only
}, func() {
assert.NoError(b, exec.Command("sudo", suite.subject, "rmi", "--help").Run())
})
} else {
assert.NoError(b, exec.Command("sudo", suite.docker, "build", "--tag", testImageName, buildContext).Run()) //nolint:gosec // testing only
Wrapper(b, func() {
assert.NoError(b, exec.Command("sudo", suite.docker, "rmi", "--force", testImageName).Run()) //nolint:gosec // testing only
}, func() {
assert.NoError(b, exec.Command("sudo", suite.docker, "rmi", "--help").Run())
})
}
}

0 comments on commit 6349eac

Please sign in to comment.