ultron

package module
v1.5.4 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2019 License: Apache-2.0 Imports: 25 Imported by: 0

README

Ultron

a http load testing tool in go

Go Report Card Build Status codecov GoDoc

Requirements

Go 1.12+

Example

Script

file path: example/http/main.go

attacker := ultron.NewHTTPAttacker("benchmark", func() (*http.Request, error) { return http.NewRequest(http.MethodGet, "http://127.0.0.1/", nil) })
task := ultron.NewTask()
task.Add(attacker, 1)

ultron.LocalRunner.Config.Concurrence = 1000
ultron.LocalRunner.Config.HatchRate = 10
ultron.LocalRunner.Config.MinWait = ultron.ZeroDuration
ultron.LocalRunner.Config.MaxWait = ultron.ZeroDuration

ultron.LocalRunner.WithTask(task)
ultron.LocalRunner.Start()
Report
{
  "benchmark": {
    "name": "benchmark",
    "requests": 1917994,
    "failures": 0,
    "min": 0,
    "max": 23,
    "median": 2,
    "average": 2,
    "qps": 50211,
    "distributions": {
      "0.50": 2,
      "0.60": 2,
      "0.70": 2,
      "0.80": 2,
      "0.90": 2,
      "0.95": 2,
      "0.97": 2,
      "0.98": 3,
      "0.99": 4,
      "1.00": 23
    },
    "failure_details": {},
    "full_history": false
  }
}

Documentation

Overview

Package ultron is a generated protocol buffer package.

It is generated from these files:

grpc.proto

It has these top-level messages:

Result
AttackerError
ClientInfo
Ack
Message

Index

Constants

View Source
const (
	// DefaultHatchRate 默认的增压/降压幅度
	DefaultHatchRate = 10
	// DefaultDuration 默认的压测持续时间,ZeroDuration表示不控制
	DefaultDuration = ZeroDuration
	// DefaultConcurrence 默认并发数
	DefaultConcurrence = 100
	// DefaultRequests 默认请求总次数,0表示不限制
	DefaultRequests = 0
	// DefaultMinWait 默认最小等待时间
	DefaultMinWait = 3 * time.Second
	// DefaultMaxWait 默认最大等待时间
	DefaultMaxWait = 5 * time.Second
)
View Source
const (
	// ZeroDuration 0,用于一些特殊判断
	ZeroDuration = time.Duration(0)
	// StatsReportInterval 统计报表输出间隔
	StatsReportInterval = 5 * time.Second
)

Variables

View Source
var (
	// DefaultHTTPClient 默认http.Client
	// http://tleyden.github.io/blog/2016/11/21/tuning-the-go-http-client-library-for-load-testing/
	DefaultHTTPClient = &http.Client{
		Timeout: 90 * time.Second,
		Transport: &http.Transport{
			Proxy: nil,
			DialContext: (&net.Dialer{
				Timeout:   30 * time.Second,
				KeepAlive: 30 * time.Second,
				DualStack: true,
			}).DialContext,
			DisableKeepAlives:     false,
			MaxIdleConns:          2000,
			MaxIdleConnsPerHost:   1000,
			IdleConnTimeout:       30 * time.Second,
			TLSHandshakeTimeout:   10 * time.Second,
			ExpectContinueTimeout: 1 * time.Second,
		},
	}

	// DefaultFastHTTPClient define the default fasthttp client use in FastHTTPAttacker
	DefaultFastHTTPClient = &fasthttp.Client{
		Name:                "ultron",
		MaxConnsPerHost:     1000,
		MaxIdleConnDuration: 30 * time.Second,
		ReadTimeout:         30 * time.Second,
		WriteTimeout:        30 * time.Second,
	}
)
View Source
var (
	// LocalEventHook 单机运行时使用的钩子
	LocalEventHook *eventHook
	// SlaveEventHook 分布式执行时,节点钩子
	SlaveEventHook *eventHook
	// MasterEventHook 分布式执行时,主控钩子
	MasterEventHook *eventHook

	// LocalEventHookConcurrency .
	LocalEventHookConcurrency = 200
	// SlaveEventHookConcurrency .
	SlaveEventHookConcurrency = 200
	// MasterEventHookConcurrency 默认不用控制
	MasterEventHookConcurrency = 0
)
View Source
var (

	// J .
	J = json
	// Logger 全局日志
	Logger *zap.Logger
)
View Source
var (

	// LocalResultPipelineBufferSize .
	LocalResultPipelineBufferSize = 1000
	// SlaveResultPipelineBufferSize .
	SlaveResultPipelineBufferSize = 1000
	// MasterResultPipelineBufferSize .
	MasterResultPipelineBufferSize = 2000

	// LocalReportPipelineBufferSize .
	LocalReportPipelineBufferSize = 10
	// SlaveReportPipelineBufferSize .
	SlaveReportPipelineBufferSize = 10
	// MasterReportPipelineBufferSize .
	MasterReportPipelineBufferSize = 20
)
View Source
var (
	// DefaultRunnerConfig 默认执行器配置
	DefaultRunnerConfig = &RunnerConfig{
		Duration:    DefaultDuration,
		Requests:    DefaultRequests,
		Concurrence: DefaultConcurrence,
		HatchRate:   DefaultHatchRate,
		MinWait:     DefaultMinWait,
		MaxWait:     DefaultMaxWait,
	}
)
View Source
var (
	// LocalRunner 单机执行入口
	LocalRunner *localRunner
)
View Source
var Message_Type_name = map[int32]string{
	0: "Disconnect",
	1: "RefreshConfig",
	2: "StartAttack",
	3: "StopAttack",
	9: "Ping",
}
View Source
var Message_Type_value = map[string]int32{
	"Disconnect":    0,
	"RefreshConfig": 1,
	"StartAttack":   2,
	"StopAttack":    3,
	"Ping":          9,
}

Functions

func CheckFastHTTPStatusCode

func CheckFastHTTPStatusCode(resp *fasthttp.Response) error

CheckFastHTTPStatusCode check if status code >= 400

func CheckHTTPStatusCode

func CheckHTTPStatusCode(resp *http.Response, body []byte) error

CheckHTTPStatusCode 检查状态码是否>=400, 如果是则视为请求失败

func RegisterUltronServer

func RegisterUltronServer(s *grpc.Server, srv UltronServer)
func ShowLogo()

Types

type Ack

type Ack struct {
}

func (*Ack) Descriptor

func (*Ack) Descriptor() ([]byte, []int)

func (*Ack) ProtoMessage

func (*Ack) ProtoMessage()

func (*Ack) Reset

func (m *Ack) Reset()

func (*Ack) String

func (m *Ack) String() string

type Attacker

type Attacker interface {
	Name() string
	Fire() error
}

Attacker �定义一个事务、请求,需要确保该对象是Goroutine-safe

type AttackerError

type AttackerError struct {
	Name     string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
	CausedBy string `protobuf:"bytes,2,opt,name=causedBy" json:"causedBy,omitempty"`
}

func (*AttackerError) Descriptor

func (*AttackerError) Descriptor() ([]byte, []int)

func (*AttackerError) Error

func (ae *AttackerError) Error() string

func (*AttackerError) GetCausedBy

func (m *AttackerError) GetCausedBy() string

func (*AttackerError) GetName

func (m *AttackerError) GetName() string

func (*AttackerError) ProtoMessage

func (*AttackerError) ProtoMessage()

func (*AttackerError) Reset

func (m *AttackerError) Reset()

func (*AttackerError) String

func (m *AttackerError) String() string

type AttackerReport

type AttackerReport struct {
	Name           string           `json:"name"`
	Requests       int64            `json:"requests"`
	Failures       int64            `json:"failures"`
	Min            int64            `json:"min"`
	Max            int64            `json:"max"`
	Median         int64            `json:"median"`
	Average        int64            `json:"average"`
	QPS            int64            `json:"qps"`
	Distributions  map[string]int64 `json:"distributions"`
	FailRatio      string           `json:"fail_ratio"`
	FailureDetails map[string]int64 `json:"failure_details"`
	FullHistory    bool             `json:"full_history"`
}

AttackerReport Attacker级别的报告

type ClientInfo

type ClientInfo struct {
	Id string `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"`
}

func (*ClientInfo) Descriptor

func (*ClientInfo) Descriptor() ([]byte, []int)

func (*ClientInfo) GetId

func (m *ClientInfo) GetId() string

func (*ClientInfo) ProtoMessage

func (*ClientInfo) ProtoMessage()

func (*ClientInfo) Reset

func (m *ClientInfo) Reset()

func (*ClientInfo) String

func (m *ClientInfo) String() string

type CommonFastHTTPClient added in v1.5.0

type CommonFastHTTPClient interface {
	Do(*fasthttp.Request, *fasthttp.Response) error
	DoDeadline(*fasthttp.Request, *fasthttp.Response, time.Time) error
	DoTimeout(*fasthttp.Request, *fasthttp.Response, time.Duration) error
}

type CommonHTTPClient added in v1.5.2

type CommonHTTPClient interface {
	Do(r *http.Request) (*http.Response, error)
}

type FastHTTPAttacker

type FastHTTPAttacker struct {
	Client  CommonFastHTTPClient
	Prepare FastHTTPPrepareFunc

	CheckChain []FastHTTPResponseCheck
	// contains filtered or unexported fields
}

FastHTTPAttacker a http attacker base on fasthttp: https://github.com/valyala/fasthttp

func NewFastHTTPAttacker

func NewFastHTTPAttacker(n string, p FastHTTPPrepareFunc, check ...FastHTTPResponseCheck) *FastHTTPAttacker

NewFastHTTPAttacker return a new instance of FastHTTPAttacker

func (*FastHTTPAttacker) Fire

func (fa *FastHTTPAttacker) Fire() error

Fire send request and check response

func (*FastHTTPAttacker) Name

func (fa *FastHTTPAttacker) Name() string

Name return attacker's name

type FastHTTPPrepareFunc

type FastHTTPPrepareFunc func(*fasthttp.Request) error

FastHTTPPrepareFunc 构造fasthttp.Request请求参数

type FastHTTPResponseCheck

type FastHTTPResponseCheck func(*fasthttp.Response) error

FastHTTPResponseCheck check fasthttp.Response

type HTTPAttacker

type HTTPAttacker struct {
	Client  CommonHTTPClient
	Prepare HTTPPrepareFunc

	CheckChain []HTTPResponseCheck
	// contains filtered or unexported fields
}

HTTPAttacker http协议的Attacker实现

func NewHTTPAttacker

func NewHTTPAttacker(n string, p HTTPPrepareFunc, check ...HTTPResponseCheck) *HTTPAttacker

NewHTTPAttacker 创建一个新的HTTPAttacker对象

func (*HTTPAttacker) Fire

func (ha *HTTPAttacker) Fire() error

Fire HTTPAttacker发起一次请求

func (*HTTPAttacker) Name

func (ha *HTTPAttacker) Name() string

Name 返回HTTPAttacker的名称

type HTTPPrepareFunc

type HTTPPrepareFunc func() (*http.Request, error)

HTTPPrepareFunc 构造http.Request函数,需要调用方定义,由HTTPAttacker来发送

type HTTPResponseCheck

type HTTPResponseCheck func(resp *http.Response, body []byte) error

HTTPResponseCheck http.Response校验函数,可由调用方自定义,如果返回error,则视为请求失败

type Message

type Message struct {
	Type Message_Type `protobuf:"varint,1,opt,name=type,enum=ultron.Message_Type" json:"type,omitempty"`
	Data []byte       `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"`
}

func (*Message) Descriptor

func (*Message) Descriptor() ([]byte, []int)

func (*Message) GetData

func (m *Message) GetData() []byte

func (*Message) GetType

func (m *Message) GetType() Message_Type

func (*Message) ProtoMessage

func (*Message) ProtoMessage()

func (*Message) Reset

func (m *Message) Reset()

func (*Message) String

func (m *Message) String() string

type Message_Type

type Message_Type int32
const (
	Message_Disconnect    Message_Type = 0
	Message_RefreshConfig Message_Type = 1
	Message_StartAttack   Message_Type = 2
	Message_StopAttack    Message_Type = 3
	Message_Ping          Message_Type = 9
)

func (Message_Type) EnumDescriptor

func (Message_Type) EnumDescriptor() ([]byte, []int)

func (Message_Type) String

func (x Message_Type) String() string

type Report

type Report map[string]*AttackerReport

Report Task级别的报告

type ReportHandleFunc

type ReportHandleFunc func(Report)

ReportHandleFunc .

type Result

type Result struct {
	Name     string         `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
	Duration int64          `protobuf:"varint,2,opt,name=duration" json:"duration,omitempty"`
	Error    *AttackerError `protobuf:"bytes,3,opt,name=error" json:"error,omitempty"`
}

func (*Result) Descriptor

func (*Result) Descriptor() ([]byte, []int)

func (*Result) GetDuration

func (m *Result) GetDuration() int64

func (*Result) GetError

func (m *Result) GetError() *AttackerError

func (*Result) GetName

func (m *Result) GetName() string

func (*Result) ProtoMessage

func (*Result) ProtoMessage()

func (*Result) Reset

func (m *Result) Reset()

func (*Result) String

func (m *Result) String() string

type ResultHandleFunc

type ResultHandleFunc func(*Result)

ResultHandleFunc .

type Runner

type Runner interface {
	WithConfig(*RunnerConfig)
	WithTask(*Task)
	GetConfig() *RunnerConfig
	GetStatus() Status
	Start()
	Done()
}

Runner 定义执行器接口

type RunnerConfig

type RunnerConfig struct {
	Duration    time.Duration `json:"duration,omitempty"`    //v2废弃,但兼容V1
	Requests    uint64        `json:"requests,omitempty"`    //v2废弃,但兼容v1
	Concurrence int           `json:"concurrence,omitempty"` //v2废弃,但兼容V1
	HatchRate   int           `json:"hatch_rate,omitempty"`  //v2废弃,但兼容V1
	MinWait     time.Duration `json:"min_wait,omitempty"`
	MaxWait     time.Duration `json:"max_wait,omitempty"`
	Stages      []*Stage      `json:"stages,omitempty"`
	// contains filtered or unexported fields
}

RunnerConfig runner配置参数

func NewRunnerConfig

func NewRunnerConfig() *RunnerConfig

NewRunnerConfig 创建新的执行器配置

func (*RunnerConfig) AppendStage added in v1.5.0

func (rc *RunnerConfig) AppendStage(sc *Stage) *RunnerConfig

AppendStage 添加下一个阶段配置

func (*RunnerConfig) AppendStages added in v1.5.0

func (rc *RunnerConfig) AppendStages(sc ...*Stage) *RunnerConfig

AppendStages 批量添加StageConfig

func (*RunnerConfig) CurrentStage added in v1.5.0

func (rc *RunnerConfig) CurrentStage() (int, *Stage)

CurrentStage 获取当前Stage

type Stage added in v1.5.0

type Stage struct {
	Duration    time.Duration `json:"duration,omitempty"` // 阶段期望持续时间,不严格控制
	Requests    uint64        `json:"requests,omitempty"` // 阶段期望请求总数,不严格控制
	Concurrence int           `json:"concurrence"`        // 阶段目标并发数

	HatchRate int `json:"hatch_rate,omitempty"` // 阶段增压/降压频率,为0不表示不控制,对于降压阶段,无需使用负数来表示降压频率
	// contains filtered or unexported fields
}

Stage 压测阶段配置参数

func NewStage added in v1.5.0

func NewStage() *Stage

NewStage 实例化Stage

type StageStatus added in v1.5.0

type StageStatus = uint32

StageStatus Stage状态

const (
	// StageReady stage初始化已完成,尚未开始
	StageReady StageStatus = iota
	// StageExpired stage已经退出
	StageExpired
)

type Status

type Status = uint32
const (
	// StatusIdle 空闲状态
	StatusIdle Status = iota
	// StatusBusy 执行中状态
	StatusBusy
	// StatusStopped 已经停止状态
	StatusStopped
)

type Task

type Task struct {
	// contains filtered or unexported fields
}

Task Attacker集合

func NewTask

func NewTask() *Task

NewTask 创建一个Task对象

func (*Task) Add

func (t *Task) Add(a Attacker, weight int)

Add 往Task中添加一个Attacker对象, weight 表示该Attacker的权重

func (*Task) Del

func (t *Task) Del(a Attacker)

Del 从Task中移除一个Attacker对象

type UltronClient

type UltronClient interface {
	Send(ctx context.Context, opts ...grpc.CallOption) (Ultron_SendClient, error)
	Subscribe(ctx context.Context, in *ClientInfo, opts ...grpc.CallOption) (Ultron_SubscribeClient, error)
}

func NewUltronClient

func NewUltronClient(cc *grpc.ClientConn) UltronClient

type UltronServer

type UltronServer interface {
	Send(Ultron_SendServer) error
	Subscribe(*ClientInfo, Ultron_SubscribeServer) error
}

type Ultron_SendClient

type Ultron_SendClient interface {
	Send(*Result) error
	CloseAndRecv() (*Ack, error)
	grpc.ClientStream
}

type Ultron_SendServer

type Ultron_SendServer interface {
	SendAndClose(*Ack) error
	Recv() (*Result, error)
	grpc.ServerStream
}

type Ultron_SubscribeClient

type Ultron_SubscribeClient interface {
	Recv() (*Message, error)
	grpc.ClientStream
}

type Ultron_SubscribeServer

type Ultron_SubscribeServer interface {
	Send(*Message) error
	grpc.ServerStream
}

Directories

Path Synopsis
cli
example

Jump to

Keyboard shortcuts

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