Documentation ¶
Index ¶
- Variables
- func Stringify(v interface{}) string
- type Blaster
- type Config
- type FunctionWorker
- type HitSegment
- type Payload
- func (p *Payload) AddHeader(key string, values ...string)
- func (p *Payload) AddParam(key string, value string)
- func (p Payload) From(newBody []byte) Payload
- func (p Payload) IsNil() bool
- func (p Payload) MarshalJSONObject(encoder *gojay.Encoder)
- func (p Payload) With(params map[string]string, headers map[string][]string, body []byte) Payload
- type Segment
- type Starter
- type Stats
- type Status
- type Stopper
- type Total
- type Worker
- type WorkerContext
- func (w *WorkerContext) Elapsed() time.Duration
- func (w *WorkerContext) Error() error
- func (w *WorkerContext) FromContext(title string, nextReq Payload, meta interface{}) *WorkerContext
- func (w *WorkerContext) IsFinished() bool
- func (w *WorkerContext) IsNil() bool
- func (w *WorkerContext) LastContext() *WorkerContext
- func (w *WorkerContext) MarshalJSONObject(enc *gojay.Encoder)
- func (w *WorkerContext) Meta() interface{}
- func (w *WorkerContext) Request() Payload
- func (w *WorkerContext) Response() (Payload, error)
- func (w *WorkerContext) SetResponse(status string, payload Payload, err error) error
- func (w *WorkerContext) Since(from time.Time) time.Duration
- func (w *WorkerContext) Status() string
- type WorkerFunc
Constants ¶
This section is empty.
Variables ¶
var ( // ErrWorkerNotFinished is returned when worker finished processing and response is not yet received. ErrWorkerNotFinished = errors.New("worker response not yet received") // ErrWorkerFinished is returned when worker is finished processing and response was received therefore not // allowing changes to be made to it's context. ErrWorkerFinished = errors.New("worker response already received") )
Functions ¶
Types ¶
type Blaster ¶
type Blaster struct {
// contains filtered or unexported fields
}
Blaster provides the back-end blast: a simple tool for API load testing and batch jobs. Use the New function to create a Blaster with default values.
func (*Blaster) AddHitSegment ¶
AddHitSegment adds a new hit segment if the underline blaster as not finished.
func (*Blaster) Exit ¶
func (b *Blaster) Exit()
Exit cancels any goroutines that are still processing, and closes all files.
type Config ¶
type Config struct { // WorkerFunc is responsible for generating workers for load-testing. WorkerFunc WorkerFunc // FilterWorkerContext sets a function which will be used if available to filter // out which root WorkerContext will be logged into the Config.Log writer. // // If function returns true, then WorkerContext will be logged, else not logged. FilterWorkerContext func(*WorkerContext) bool // FilterChildWorkerContext sets a function which will be used if available to filter // out which WorkerContext child within a root WorkerContext will be logged into the // Config.Log writer. It also passes the level of which this child WorkerContext belongs. // // If function returns true, then WorkerContext will be logged, else not logged. FilterChildWorkerContext func(*WorkerContext, int) bool // OnNextSegment sets a function to be executed once a new hit segment has begun. OnNextSegment func(HitSegment, Stats) // SegmentedEnded sets a function to be executed once a hit segment has finished. OnSegmentEnd func(HitSegment, Stats) // OnEachRun sets a function to be called on every finished execution of a giving // worker's request. This way you get access to the current Stats, worker id // and worker context used within a single run of a hit segment. // // Note this is called for every completion of an individual request, so if you // set a HitSegment.MaxHits of 1000, then this would be called 1000 times. OnEachRun func(workerId int, workerContext *WorkerContext, stat Stats) // DefaultHeaders contains default headers that all workers must include // in their requests. // // All header values are copied/appended into the initial content of a worker // WorkerContext, if an existing header is found then it will append default // header values into key header list. // // The header is also returned by the call to Worker.Prepare. DefaultHeaders map[string][]string // DefaultParams contains default params that all workers must include // in their requests. // // All parameters are copied into the initial content of a worker start // WorkerContext, but it will not replace any key already provided for, // if found in the WorkerContext returned by the Worker.Prepare method call. DefaultParams map[string]string // Log sets the Writer to write internal blaster logs into. Log io.Writer // Metrics sets the Writer to write period stats of blaster into. Metrics io.Writer // PeriodicWrite sets the intervals at which the current stats of the // blaster is written into the Config.Metrics writer. // ( Defaults: 5 second ). PeriodicWrite time.Duration // Segments sets the sampling size and total different blast segments // rates and max hits which will be used for each worker. Segments []HitSegment // Workers sets the number of concurrent workers to be used. // (Default: 10 workers). Workers int // Timeout sets the deadline in the context passed to the worker. Workers must // respect the context cancellation. // We exit with an error if any worker is processing for timeout + 1 second. // (Default: 1 second). Timeout time.Duration // HitCoolOff sets the duration for which we cool off before starting the next // hit segments, this allows us to provide some time for the API to respond to // pending possible requests. // (Default: 1 second). HitCoolOff time.Duration // Endless sets whether the blaster should still continue working, if it has exhausted // it's segments list. This allows us dynamically deliver new segments to be delivered // into a current running blaster. Endless bool }
Config provides all the standard config options. Use the Initialise method to configure with a provided Config.
type FunctionWorker ¶
type FunctionWorker struct { StopFunc func(ctx context.Context) error StartFunc func(ctx context.Context) error PrepareFunc func(ctx context.Context) (*WorkerContext, error) SendFunc func(ctx context.Context, workerCtx *WorkerContext) }
FunctionWorker facilitates code examples by satisfying the Worker, Starter and Stopper interfaces with provided functions.
func (*FunctionWorker) Prepare ¶
func (e *FunctionWorker) Prepare(ctx context.Context) (*WorkerContext, error)
Prepare satisfies the Worker interface.
func (*FunctionWorker) Send ¶
func (e *FunctionWorker) Send(ctx context.Context, lastWctx *WorkerContext)
Send satisfies the Worker interface.
type HitSegment ¶
type HitSegment struct { // Rate sets the initial rate in requests per second. // (Default: 10 requests / second). Rate float64 // MaxHits sets the maximum amount of hits per the rate value // which this segment will run. // (Defaults: 1000). MaxHits int }
HitSegment details the giving rate per second and maximum allowed hits using available max workers to send hit requests against a target.
type Payload ¶
type Payload struct { Body []byte Attached interface{} Params map[string]string Headers map[string][]string }
Payload is defines the content to be used for a giving request with it's headers body and possible parameters depending on the underline protocol logic.
func (Payload) From ¶
From clones giving body parameters and headers returning a new Payload using supplied body.
func (Payload) MarshalJSONObject ¶
MarshalJSONObject implements gojay.MarshalJSONObject interface.
type Segment ¶
type Segment struct { DesiredRate float64 ActualRate float64 AverageConcurrency float64 Duration time.Duration Summary *Total Status []*Status }
Segment is a rate segment - a new segment is created each time the rate is changed.
type Starter ¶
Starter is an interface a worker can optionally satisfy to provide initialization logic.
type Stats ¶
type Stats struct { ConcurrencyCurrent int ConcurrencyMaximum int Skipped int64 All *Segment Segments []*Segment }
Stats is a snapshot of the metrics (as is printed during interactive execution).
type Status ¶
type Status struct { Status string Count int64 Fraction float64 Mean time.Duration NinetyFifth time.Duration }
Status is a summary of all requests that returned a specific status
type Stopper ¶
Stopper is an interface a worker can optionally satisfy to provide finalization logic.
type Total ¶
type Total struct { Started int64 Finished int64 Success int64 Fail int64 Mean time.Duration NinetyFifth time.Duration }
Total is the summary of all requests in this segment
type Worker ¶
type Worker interface { // Prepare handles the loading of the initial data a worker should begin with, it is // responsible for creating what data a request should be or contain. Prepare(ctx context.Context) (*WorkerContext, error) // Send handles the internal request/requests we wish to make for to our title. // // You only make request for a single or set of requests for just one call to your // service title. Blitzkrieg handles concurrent hits to your title by calling // multiple versions of your worker. Send(ctx context.Context, workerCtx *WorkerContext) }
Worker is an interface that allows blast to easily be extended to support any protocol. A worker receives a WorkerContext which holds underline data about a request to be made and can be provided with details for it's response.
The WorkerContext provides the WorkerContext.FromContext which creates a parent-child chain for a context, this allows a single worker to make multiple request each with a unique title that the parent will collect, this tree will be followed to generate a complete view and metric information on the whole and individual requests for the worker.
By using this architecture we allow workers which are created once but called repeatedly to process requests concurrently and house different internal behaviours or sub calls which we can equally measure without much complexity in the library itself.
type WorkerContext ¶
type WorkerContext struct {
// contains filtered or unexported fields
}
WorkerContext exists to define and contain the request body, headers and response content, header and status for a giving request work done by a worker. It also provides a means of providing response from a previous request to a next request in a sequence or for the desire of alternating the behaviour of the next worker based on the response from the last.
WorkerContext is not safe for concurrent use by multiple go-routines, nor is it intended to be.
func NewWorkerContext ¶
func NewWorkerContext(title string, req Payload, meta interface{}) *WorkerContext
NewWorkerContext returns a new WorkerContext which has no previous context. Arguments:
- meta: This is any meta type you wish to be attached to your WorkerContext like a Config for the title information.
func WorkerContextWithoutPayload ¶
func WorkerContextWithoutPayload(meta interface{}) *WorkerContext
WorkerContextWithoutPayload returns a new WorkerContext with a default empty payload.
Always provide a descriptive title for a worker context for easy recognition.
func (*WorkerContext) Elapsed ¶
func (w *WorkerContext) Elapsed() time.Duration
Elapsed returns the total duration taking from the creation of a context till the call to it's method SetResponse().
func (*WorkerContext) Error ¶
func (w *WorkerContext) Error() error
Error returns occured error for last executed worker.
func (*WorkerContext) FromContext ¶
func (w *WorkerContext) FromContext(title string, nextReq Payload, meta interface{}) *WorkerContext
FromContext returns a new WorkerContext which is based of this worker context, connecting this as it's previous context.
Always provide a descriptive title for a worker context for easy recognition.
func (*WorkerContext) IsFinished ¶
func (w *WorkerContext) IsFinished() bool
IsFinished returns true/false if giving context is finished and concluded.
func (*WorkerContext) IsNil ¶
func (w *WorkerContext) IsNil() bool
IsNil implements gojay.MarshalJSONObject interface method.
func (*WorkerContext) LastContext ¶
func (w *WorkerContext) LastContext() *WorkerContext
LastContext returns parent worker context from where it is derived from. A root WorkerContext never has a parent.
func (*WorkerContext) MarshalJSONObject ¶
func (w *WorkerContext) MarshalJSONObject(enc *gojay.Encoder)
MarshalJSONObject implements gojay.MarshalJSONObject interface method.
func (*WorkerContext) Meta ¶
func (w *WorkerContext) Meta() interface{}
Meta returns attached Meta if any for giving request context.
func (*WorkerContext) Request ¶
func (w *WorkerContext) Request() Payload
Request returns Payload for giving request context.
func (*WorkerContext) Response ¶
func (w *WorkerContext) Response() (Payload, error)
Response returns Payload for giving response, this is only ever available to the next sequence once the current has completed it's run in a set of sequence request.
func (*WorkerContext) SetResponse ¶
func (w *WorkerContext) SetResponse(status string, payload Payload, err error) error
SetResponse sets the response of the worker context, finished it and setting the response payload and status and possible error which you wish to set as the signifying error for giving request else this get's set to the returned error from a worker when finished a giving run.
if an error occurs on the worker or if the response err value is set then we consider the work a failure and not a success.
func (*WorkerContext) Since ¶
func (w *WorkerContext) Since(from time.Time) time.Duration
Since returns the total duration taking from the passed in time of a context till the call to it's method SetResponse().
func (*WorkerContext) Status ¶
func (w *WorkerContext) Status() string
Status returns response status for giving request worker and is not available until after a request was finished.
type WorkerFunc ¶
type WorkerFunc func() Worker
WorkerFunc defines a function type which generates a new Worker for running a load test.