diff --git a/Makefile b/Makefile index 81839e8b7..425eff1e6 100644 --- a/Makefile +++ b/Makefile @@ -252,6 +252,13 @@ test-e2e-container: test-e2e-vm: go test -ldflags $(LDFLAGS) -timeout 45m ./e2e/vm -test.v -ginkgo.v --installed="$(INSTALLED)" +.PHONY: test-benchmark +test-benchmark: test-benchmark-vm + +.PHONY: test-benchmark-vm +test-benchmark-vm: + cd benchmark/vm && go test -ldflags $(LDFLAGS) -bench=. -benchmem --installed="$(INSTALLED)" + .PHONY: gen-code # Since different projects may have different versions of tool binaries, # GOBIN is introduced to maintain a set of tool binaries dedicated to our project use. diff --git a/benchmark/vm/vm_test.go b/benchmark/vm/vm_test.go new file mode 100644 index 000000000..a6e147d90 --- /dev/null +++ b/benchmark/vm/vm_test.go @@ -0,0 +1,71 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// Package vm runs tests related to the virtual machine. +package vm + +import ( + "flag" + "fmt" + "os" + "os/exec" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" +) + +const ( + virtualMachineRootCmd = "vm" +) + +// InstalledTestSubject is the test subject when Finch is installed. +const InstalledTestSubject = "finch" + +// Installed indicates whether the tests are run against installed application. +var Installed = flag.Bool("installed", false, "the flag to show whether the tests are run against installed application") + +func BenchmarkVMInit(b *testing.B) { + subject, err := getSubject() + if err != nil { + b.Fatal(err) + } + b.ResetTimer() + for i := 0; i < b.N; i++ { + b.StartTimer() + assert.NoError(b, exec.Command(subject, virtualMachineRootCmd, "init").Run()) //nolint:gosec // testing only + b.StopTimer() + assert.NoError(b, exec.Command(subject, virtualMachineRootCmd, "stop", "-f").Run()) //nolint:gosec // testing only + assert.NoError(b, exec.Command(subject, virtualMachineRootCmd, "remove", "-f").Run()) //nolint:gosec // testing only + } +} + +func BenchmarkVMStart(b *testing.B) { + subject, err := getSubject() + if err != nil { + b.Fatal(err) + } + assert.NoError(b, exec.Command(subject, virtualMachineRootCmd, "init").Run()) //nolint:gosec // testing only + assert.NoError(b, exec.Command(subject, virtualMachineRootCmd, "stop", "-f").Run()) //nolint:gosec // testing only + b.ResetTimer() + for i := 0; i < b.N; i++ { + b.StartTimer() + assert.NoError(b, exec.Command(subject, virtualMachineRootCmd, "start").Run()) //nolint:gosec // testing only + b.StopTimer() + assert.NoError(b, exec.Command(subject, virtualMachineRootCmd, "stop", "-f").Run()) //nolint:gosec // testing only + } + assert.NoError(b, exec.Command(subject, virtualMachineRootCmd, "remove", "-f").Run()) //nolint:gosec // testing only +} + +func getSubject() (string, error) { + wd, err := os.Getwd() + if err != nil { + return "", fmt.Errorf("failed to get the current working directory: %w", err) + } + + subject := filepath.Join(wd, "../../_output/bin/finch") + if *Installed { + subject = InstalledTestSubject + } + return subject, nil +}