hazana

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2017 License: Apache-2.0 Imports: 15 Imported by: 0

README

hazana - package for creating load tests of services

Build Status GoDoc

Hazana is created for load tests that use (generated) clients in Go to communicate to services (in any supported language). By providing the Attack interface, any client and protocol could potentially be tested with this package. This package was created to load test gRPC services.

Compared to existing HTTP load testing tools (e.g. tsenart/vegeta) that can send raw HTTP requests, this package requires the use of client code to send the requests and receive the response.

Attack

    // Attack must be implemented by a service client.
    type Attack interface {
            // Setup should establish the connection to the service
            // It may want to access the config of the runner.
            Setup(c Config) error

            // Do performs one request and is executed in a separate goroutine.
            // The context is used to cancel the request on timeout.
            Do(ctx context.Context) DoResult

            // Teardown can be used to close the connection to the service.
            Teardown() error

            // Clone should return a fresh new Attack
            // Make sure the new Attack has values for shared struct fields initialized at Setup.
            Clone() Attack
    }

The hazana runner will spawn goroutines to meet this load. Each goroutine will use one Attack value to perform the communication ( see Do() ). Typically each Attack value uses its own connection but your implementation can use another strategy.

Rampup

The hazana runner will use a rampup period in which the RPS is increased (every second) during the rampup time. In this phase, new goroutines are spawned up to the given maximum. This package has two strategies for adding new attackers to meet the rps. The linear rampup strategy will create exactly the maximum number of goroutines within the rampup period. The exp2 strategy spawn goroutines as needed (exponential with max factor of 2) to match the current rps load during.

Flags

Programs that use the hazana package will have several flags to control the load runner.

Usage of <<your load test program>>:
    -attack int
            duration of the attack in seconds (default 60)
    -max int
            maximum concurrent attackers (default 10)
    -timeout int
            timeout in seconds for an attack call (default 5)
    -o string
            output file to write the metrics per sample request index (use stdout if empty)
    -ramp int
            ramp up time in seconds (default 10)
    -s string
            set the rampup strategy, possible values are {linear,exp2}
    -rps int
            target number of requests per second, must be greater than zero (default 1)
    -t int
            test your attack implementation with a number of sample calls. Your program exits after this.
    -v verbose logging
Example

After creating your implementation type YourAttack then this would be the minimal program to run a load test.

    func main() {
            r := hazana.Run(new(YourAttack), hazana.ConfigFromFlags())

            // inspect the report and compute whether the test has failed
            // e.g by looking at the success percentage and mean response time of each metric.
            r.Failed = false

            hazana.PrintReport(r)
    }
Configuration

In addition to using flags, you can load the configuration from a JSON file. Values set with flags will override those from the configuration file.

    {
            "RPS": 10,
            "AttackTimeSec": 20,
            "RampupTimeSec": 10,
            "MaxAttackers": 10,
            "OutputFilename": "myreport.json",
            "Verbose": true,
            "Metadata": {
                    "service" : "happiness.services.com",
                    "environment" : "staging",
                    "version": "v1.42",
                    "apiToken*": "your-secret-token"
            }
    }

Note that metadata keys that end with * will be obfuscated when reporting.

Example
    func main() {
            r := hazana.Run(YourAttack{}, hazana.ConfigFromFile("myconfig.json"))
            hazana.PrintReport(r)
            hazana.PrintSummary(r)
    }

See examples/zombie.go for a complete minimal example.

See examples/clock for an actual gRPC service that can tell time under load.

Sample verbose output from one of our services
    2017/08/17 10:26:32 hazana - load runner
    2017/08/17 10:26:32 begin rampup of [10] seconds
    2017/08/17 10:26:32 setup and spawn new attacker [1]
    2017/08/17 10:26:34 current rate [1.998330838214118], target rate [1], attackers [1], mean response time [229.576185ms]
    2017/08/17 10:26:34 setup and spawn new attacker [2]
    2017/08/17 10:26:36 current rate [2.9997667261403085], target rate [2], attackers [2], mean response time [52.674453ms]
    2017/08/17 10:26:36 setup and spawn new attacker [3]
    2017/08/17 10:26:37 current rate [3.9896134642448655], target rate [3], attackers [3], mean response time [52.805457ms]
    2017/08/17 10:26:37 setup and spawn new attacker [4]
    2017/08/17 10:26:39 current rate [4.973899789133906], target rate [4], attackers [4], mean response time [54.645931ms]
    2017/08/17 10:26:39 setup and spawn new attacker [5]
    2017/08/17 10:26:40 current rate [5.995198996681865], target rate [5], attackers [5], mean response time [53.096359ms]
    2017/08/17 10:26:40 setup and spawn new attacker [6]
    2017/08/17 10:26:41 current rate [6.973412713416999], target rate [6], attackers [6], mean response time [55.183152ms]
    2017/08/17 10:26:41 setup and spawn new attacker [7]
    2017/08/17 10:26:42 current rate [7.982915515035891], target rate [7], attackers [7], mean response time [45.521208ms]
    2017/08/17 10:26:42 setup and spawn new attacker [8]
    2017/08/17 10:26:43 current rate [8.953025436248573], target rate [8], attackers [8], mean response time [42.844917ms]
    2017/08/17 10:26:43 setup and spawn new attacker [9]
    2017/08/17 10:26:44 current rate [9.982111816054946], target rate [9], attackers [9], mean response time [42.128101ms]
    2017/08/17 10:26:44 setup and spawn new attacker [10]
    2017/08/17 10:26:45 current rate [10.99217377013479], target rate [10], attackers [10], mean response time [37.483798ms]
    2017/08/17 10:26:45 end rampup ending up with [10] attackers
    2017/08/17 10:26:45 begin full attack of [10] remaining seconds
    2017/08/17 10:26:55 end full attack
    2017/08/17 10:26:55 stopping attackers [10]
    2017/08/17 10:26:55 tearing down attackers [10]
    {
            "startedAt": "2017-08-17T10:26:32.976273638+02:00",
            "finishedAt": "2017-08-17T10:26:55.961789195+02:00",
            "configuration": {
                    "rps": 10,
                    "attackTimeSec": 20,
                    "rampupTimeSec": 10,
                    "maxAttackers": 10,
                    "rampupStrategy" : "linear",
                    "verbose": true,
                    "doTimeoutSec": 5,
                    "metadata": {
                            "service" : "happiness.services.com",
                            "environment" : "staging",
                            "version": "v1.42",
                            "apiToken*": "***---***---***"
                    }
            },
            "metrics": {
                    "POST item.xml": {
                            "latencies": {
                                    "total": 3817277924,
                                    "mean": 37794830,
                                    "50th": 32147032,
                                    "95th": 46125381,
                                    "99th": 71243508,
                                    "max": 422720083
                            },
                            "earliest": "2017-08-17T10:26:45.924789988+02:00",
                            "latest": "2017-08-17T10:26:55.929145547+02:00",
                            "end": "2017-08-17T10:26:55.961659257+02:00",
                            "duration": 10004355559,
                            "wait": 32513710,
                            "requests": 101,
                            "rate": 10.095602800636126,
                            "success": 1,
                            "status_codes": null,
                            "errors": null
                    }
            },
            "failed":false
    }
Stackdriver integration

The hazana-stackdriver-monitoring project offers a tool to send the results of a loadtest to a Google Stackdriver account. The metrics from the load test are sent as custom metrics to Stackdriver Monitoring. The report itseld is sent as a log entry to Stackdriver Logging.

© 2017, ernestmicklei.com. Apache v2 License. Contributions welcome.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetEnv

func GetEnv(key, absentValue string) string

GetEnv returns the environment variable value or absentValue if it is missing

func PrintReport

func PrintReport(r RunReport)

PrintReport writes the JSON report to a file or stdout, depending on the configuration.

func PrintSummary

func PrintSummary(r RunReport)

PrintSummary logs a subset of the report for each metric label

Types

type Attack

type Attack interface {
	// Setup should establish the connection to the service
	// It may want to access the config of the runner.
	Setup(c Config) error
	// Do performs one request and is executed in a separate goroutine.
	// The context is used to cancel the request on timeout.
	Do(ctx context.Context) DoResult
	// Teardown can be used to close the connection to the service
	Teardown() error
	// Clone should return a fresh new Attack
	// Make sure the new Attack has values for shared struct fields initialized at Setup.
	Clone() Attack
}

Attack must be implemented by a service client.

type Config

type Config struct {
	RPS            int               `json:"rps"`
	AttackTimeSec  int               `json:"attackTimeSec"`
	RampupTimeSec  int               `json:"rampupTimeSec"`
	RampupStrategy string            `json:"rampupStrategy"`
	MaxAttackers   int               `json:"maxAttackers"`
	OutputFilename string            `json:"outputFilename,omitempty"`
	Verbose        bool              `json:"verbose"`
	Metadata       map[string]string `json:"metadata,omitempty"`
	DoTimeoutSec   int               `json:"doTimeoutSec"`
}

Config holds settings for a Runner.

func ConfigFromFile

func ConfigFromFile(named string) Config

ConfigFromFile loads a Config for use in a runner.

func ConfigFromFlags

func ConfigFromFlags() Config

ConfigFromFlags creates a Config for use in a runner.

func (Config) Validate

func (c Config) Validate() (list []string)

Validate checks all settings and returns a list of strings with problems.

type DoResult

type DoResult struct {
	// Label identifying the request that was send which is only used for reporting the metrics.
	RequestLabel string
	// The error that happened when sending the request or receiving the response.
	Error error
	// The HTTP status code.
	StatusCode int
	// Number of bytes transferred when sending the request.
	BytesIn int64
	// Number of bytes transferred when receiving the response.
	BytesOut int64
}

DoResult is the return value of a Do call on an Attack.

type LatencyMetrics

type LatencyMetrics struct {
	// Total is the total latency sum of all requests in an attack.
	Total time.Duration `json:"total"`
	// Mean is the mean request latency.
	Mean time.Duration `json:"mean"`
	// P50 is the 50th percentile request latency.
	P50 time.Duration `json:"50th"`
	// P95 is the 95th percentile request latency.
	P95 time.Duration `json:"95th"`
	// P99 is the 99th percentile request latency.
	P99 time.Duration `json:"99th"`
	// Max is the maximum observed request latency.
	Max time.Duration `json:"max"`
}

LatencyMetrics holds computed request latency metrics.

type Metrics

type Metrics struct {
	// Latencies holds computed request latency metrics.
	Latencies LatencyMetrics `json:"latencies"`
	// First is the earliest timestamp in a Result set.
	Earliest time.Time `json:"earliest"`
	// Latest is the latest timestamp in a Result set.
	Latest time.Time `json:"latest"`
	// End is the latest timestamp in a Result set plus its latency.
	End time.Time `json:"end"`
	// Duration is the duration of the attack.
	Duration time.Duration `json:"duration"`
	// Wait is the extra time waiting for responses from targets.
	Wait time.Duration `json:"wait"`
	// Requests is the total number of requests executed.
	Requests uint64 `json:"requests"`
	// Rate is the rate of requests per second.
	Rate float64 `json:"rate"`
	// Success is the percentage of non-error responses.
	Success float64 `json:"success"`
	// StatusCodes is a histogram of the responses' status codes.
	StatusCodes map[string]int `json:"status_codes"`
	// Errors is a set of unique errors returned by the targets during the attack.
	Errors []string `json:"errors"`
	// contains filtered or unexported fields
}

Metrics holds metrics computed out of a slice of Results which are used in some of the Reporters

type RunReport

type RunReport struct {
	StartedAt     time.Time `json:"startedAt"`
	FinishedAt    time.Time `json:"finishedAt"`
	Configuration Config    `json:"configuration"`
	// RunError is set when a Run could not be called or executed.
	RunError string              `json:"runError"`
	Metrics  map[string]*Metrics `json:"metrics"`
	// Failed can be set by your load test program to indicate that the results are not acceptable.
	Failed bool `json:"failed"`
	// Output is used to publish any custom output in the report.
	Output map[string]interface{} `json:"output"`
}

RunReport is a composition of configuration, measurements and custom output from a load run.

func NewErrorReport

func NewErrorReport(err error, config Config) RunReport

NewErrorReport returns a report when a Run could not be called or executed.

func Run

func Run(a Attack, c Config) RunReport

Run starts attacking a service using an Attack implementation and a configuration. Return a report with statistics per sample and the configuration used.

Directories

Path Synopsis
grpc
Package main is a generated protocol buffer package.
Package main is a generated protocol buffer package.
grpc/server
Package main is a generated protocol buffer package.
Package main is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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