perf

package
v0.3.16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 23, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var PerfCmd = &cobra.Command{
	Use:     "perf",
	Short:   "Performance Testing",
	Long:    `Performance Testing & Benchmarking using Meshery CLI.`,
	Example: "mesheryctl perf --name \"a quick stress test\" --url http://192.168.1.15/productpage --qps 300 --concurrent-requests 2 --duration 30s --token \"provider=Meshery\"",
	Args:    cobra.NoArgs,
	PreRunE: func(cmd *cobra.Command, args []string) error {

		return utils.PreReqCheck()
	},
	RunE: func(cmd *cobra.Command, args []string) error {

		if filePath != "" {
			var t models.PerformanceSpec
			err := yaml.Unmarshal([]byte(filePath), &t)
			if err != nil {
				return errors.Wrapf(err, utils.PerfError(fmt.Sprintf("failed to unmarshal yaml file %s", filePath)))
			}
			if testDuration == "" {
				testDuration = fmt.Sprintf("%fs", t.EndTime.Sub(t.StartTime).Seconds())
			}
			if testURL == "" {
				testURL = t.EndpointURL
			}
			if concurrentRequests == "" {
				concurrentRequests = fmt.Sprintf("%d", t.Client.Connections)
			}
			if qps == "" {
				qps = fmt.Sprintf("%f", t.Client.Rps)
			}
		}

		if testName == "" {
			log.Debug("Test Name not provided")
			testName = utils.StringWithCharset(8)
			log.Debug("Using random test name: ", testName)
		}

		postData := ""

		startTime := time.Now()
		duration, err := time.ParseDuration(testDuration)
		if err != nil {
			return errors.Wrapf(err, utils.PerfError(fmt.Sprintf("failed to parse test duration %s", testDuration)))
		}

		endTime := startTime.Add(duration)

		postData = postData + "start_time: " + startTime.Format(time.RFC3339)
		postData = postData + "\nend_time: " + endTime.Format(time.RFC3339)

		if testURL != "" {
			postData = postData + "\nendpoint_url: " + testURL
		} else {
			return errors.New(utils.PerfError("please enter a test URL"))
		}

		// Method to check if the entered Test URL is valid or not
		var validURL = govalidator.IsURL(testURL)

		if !validURL {
			return errors.New(utils.PerfError("please enter a valid test URL"))
		}

		postData = postData + "\nclient:"
		postData = postData + "\n connections: " + concurrentRequests
		postData = postData + "\n rps: " + qps

		req, err := http.NewRequest("POST", mctlCfg.GetBaseMesheryURL(), bytes.NewBuffer([]byte(postData)))
		if err != nil {
			return errors.Wrapf(err, utils.PerfError(fmt.Sprintf("failed to create new request to %s", mctlCfg.GetBaseMesheryURL())))
		}

		if err := utils.AddAuthDetails(req, tokenPath); err != nil {
			return errors.Wrap(err, utils.PerfError("failed to add auth details to request"))
		}

		q := req.URL.Query()
		q.Add("name", testName)
		q.Add("loadGenerator", loadGenerator)
		if testMesh != "" {
			q.Add("mesh", testMesh)
		}
		req.URL.RawQuery = q.Encode()

		client := &http.Client{}
		resp, err := client.Do(req)
		if err != nil {
			return errors.Wrapf(err, utils.PerfError(fmt.Sprintf("failed to make request to %s", testURL)))
		}
		log.Debug("Initiating Performance test ...")
		log.Debug(resp.Status)

		defer utils.SafeClose(resp.Body)
		data, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return errors.Wrap(err, utils.PerfError("failed to read response body"))
		}
		log.Debug(string(data))

		if err := utils.UpdateAuthDetails(tokenPath); err != nil {
			return errors.Wrap(err, utils.PerfError("failed to update auth details"))
		}

		log.Debug("Test Completed Successfully!")
		return nil
	},
}

PerfCmd represents the Performance Management CLI command

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL