Skip to content

Commit

Permalink
Merge pull request #130 from upbound/perf-test-url-input
Browse files Browse the repository at this point in the history
Performance tool can accept test MRs via URLs
  • Loading branch information
Piotr1215 committed Aug 29, 2023
2 parents 3f72fda + cf073f6 commit 0807fae
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
41 changes: 35 additions & 6 deletions cmd/perf/internal/managed/managed.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"bufio"
"context"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -223,16 +225,43 @@ func prepareGVR(m map[interface{}]interface{}) schema.GroupVersionResource {
return pluralGVR
}

func readYamlFile(fileName string) (map[interface{}]interface{}, error) {
yamlFile, err := os.ReadFile(filepath.Clean(fileName))
if err != nil {
return nil, errors.Wrap(err, "cannot read file")
func readYamlFile(pathOrURL string) (map[interface{}]interface{}, error) {
var content []byte
var err error

if strings.HasPrefix(pathOrURL, "http://") || strings.HasPrefix(pathOrURL, "https://") {
// Download the file if it's an HTTP/HTTPS URL
resp, err := http.Get(pathOrURL) //nolint:gosec,noctx // This is not a security-sensitive URL.
if err != nil {
return nil, errors.Wrap(err, "cannot fetch URL")
}
defer func() {
err := resp.Body.Close()
if err != nil {
log.Fatal("cannot close response body")
}
}()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to download; HTTP code: %d", resp.StatusCode)
}

content, err = io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "cannot read content from URL")
}
} else {
// If it's a local path, read the file directly
content, err = os.ReadFile(filepath.Clean(pathOrURL))
if err != nil {
return nil, errors.Wrap(err, "cannot read file")
}
}

m := make(map[interface{}]interface{})
err = yaml.Unmarshal(yamlFile, m)
err = yaml.Unmarshal(content, &m)
if err != nil {
return nil, errors.Wrap(err, "cannot marshal map")
return nil, errors.Wrap(err, "cannot unmarshal map")
}

return m, nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/perf/internal/quantify.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewCmdQuantify() *cobra.Command {
Short: "This tool collects CPU & Memory Utilization and time to readiness of MRs metrics of providers and " +
"reports them. When you execute this tool an end-to-end experiment will run.",
Example: "provider-scale --mrs ./internal/providerScale/manifests/virtualnetwork.yaml=2 " +
"--mrs ./internal/providerScale/manifests/loadbalancer.yaml=2" +
"--mrs https:... OR ./internal/providerScale/manifests/loadbalancer.yaml=2" +
"--provider-pods crossplane-provider-jet-azure " +
"--provider-namespace crossplane-system",
RunE: o.Run,
Expand All @@ -65,7 +65,7 @@ func NewCmdQuantify() *cobra.Command {
o.cmd.Flags().StringSliceVarP(&o.providerPods, "provider-pods", "p", []string{}, "Names of the provider pods. Multiple names can be specified, separated by commas (spaces are ignored).")
o.cmd.Flags().StringVar(&o.providerNamespace, "provider-namespace", "crossplane-system",
"Namespace name of provider")
o.cmd.Flags().StringToIntVar(&o.mrPaths, "mrs", nil, "Managed resource templates that will be deployed")
o.cmd.Flags().StringToIntVar(&o.mrPaths, "mrs", nil, "Managed resource templates that will be deployed, provided as local paths or URLs")
o.cmd.Flags().StringVar(&o.address, "address", "http://localhost:9090", "Address of Prometheus service")
o.cmd.Flags().DurationVar(&o.stepDuration, "step-duration", 1*time.Second, "Step duration between two data points")
o.cmd.Flags().BoolVar(&o.clean, "clean", true, "Delete deployed MRs")
Expand Down

0 comments on commit 0807fae

Please sign in to comment.