ultron

package module
v0.0.0-...-855cf82 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2018 License: Apache-2.0 Imports: 27 Imported by: 0

README

Ultron

a http load testing tool in go

Go Report Card Build Status

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 (
	// ZeroDuration 0,用于一些特殊判断
	ZeroDuration = time.Duration(0)
	// StatsReportInterval 统计报表输出间隔
	StatsReportInterval = time.Second * 5
)

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:     2000,
		MaxIdleConnDuration: 90 * time.Second,
		ReadTimeout:         90 * time.Second,
		WriteTimeout:        60 * 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 (
	// MasterRunner 分布式执行,主控执行入口
	MasterRunner *masterRunner

	// MasterListenAddr server端默认监听地址
	MasterListenAddr = ":9500"

	// ServerStart MasterRunner启动压测的信号
	ServerStart = make(chan struct{}, 1)
	// ServerStop MasterRunner停止压测的信号
	ServerStop = make(chan struct{}, 1)
	// ServerInterrupt MasterRunner压测被中断的信号
	ServerInterrupt = make(chan struct{}, 1)
)
View Source
var (

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

	// LocalReportPipelineBufferSize .
	LocalReportPipelineBufferSize = 10
	// SlaveReportPipelineBufferSize .
	SlaveReportPipelineBufferSize = 10
	// MasterReportPipelineBufferSize .
	MasterReportPipelineBufferSize = 20
)
View Source
var (
	// LocalRunner 单机执行入口
	LocalRunner *localRunner

	// SlaveRunner 分布式执行,节点执行入口
	SlaveRunner *slaveRunner
)
View Source
var (
	// DefaultRunnerConfig 默认执行器配置
	DefaultRunnerConfig = &RunnerConfig{
		Duration:    ZeroDuration,
		Requests:    0,
		Concurrence: 100,
		HatchRate:   0,
		MinWait:     time.Second * 3,
		MaxWait:     time.Second * 5,
	}
)
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,
}
View Source
var (

	// ResultStreamBufferSize slave->master
	ResultStreamBufferSize = 100
)

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)

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 FastHTTPAttacker

type FastHTTPAttacker struct {
	Client  *fasthttp.Client
	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  *http.Client
	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"`
	Requests    uint64        `json:"requests"`
	Concurrence int           `json:"concurrence"`
	HatchRate   int           `json:"hatch_rate"`
	MinWait     time.Duration `json:"min_wait"`
	MaxWait     time.Duration `json:"max_wait"`
}

RunnerConfig runner配置参数

func NewRunnerConfig

func NewRunnerConfig() *RunnerConfig

NewRunnerConfig 创建新的执行器配置

type Status

type Status int

Status Runner状态

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