repeater

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2024 License: MIT Imports: 8 Imported by: 0

README

Repeater

A simple stress test framework for Golang

English | 中文

Install

go get -u github.com/riviera613/reapter

How to use

package main

import (
	"errors"
	"fmt"
	"github.com/riviera613/repeater"
	"math/rand"
	"time"
)

// forTest a function for test, generate a random integer N between 0 and 1024
// if N > 1000, return error; otherwise, sleep N ms
func forTest() error {
	n := time.Duration(rand.Intn(1024))
	if n > 1000 {
		return errors.New("timeout")
	}
	time.Sleep(n * time.Millisecond)
	return nil
}

func main() {
	_repeater := repeater.NewRepeater([]*repeater.InputFunc{
		{Name: "forTest", Func: forTest},
	}, []*repeater.InputParam{
		{Concurrence: 1, TotalCount: 5},
		{Concurrence: 10, TotalCount: 50},
		{Concurrence: 100, TotalCount: 500},
	})
	_repeater.Process()
	fmt.Println(_repeater.Render())   // output result to terminal
	_ = _repeater.ToCsv("result.csv") // output result to csv
}

Output

+---------+-------------+-------------+---------------+--------------+----------+--------------------+----------+----------+
| NAME    | CONCURRENCE | TOTAL COUNT | SUCCESS COUNT | SUCCESS RATE |    TOTAL |                AVG |      P95 |      P99 |
+---------+-------------+-------------+---------------+--------------+----------+--------------------+----------+----------+
| forTest |           1 |           5 |             5 |            1 | 2.973226 | 0.5945933999999999 | 0.931145 | 0.931145 |
| forTest |          10 |          50 |            50 |            1 | 3.390408 |         0.58240476 | 0.967909 | 0.983813 |
| forTest |         100 |         500 |           488 |        0.976 | 2.990706 | 0.4858624651639343 | 0.926045 | 0.975087 |
+---------+-------------+-------------+---------------+--------------+----------+--------------------+----------+----------+

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type InputFunc

type InputFunc struct {
	Name string
	Func func() error
}

type InputParam

type InputParam struct {
	Concurrence int64
	TotalCount  int64
}

type Repeater

type Repeater struct {
	TestCases []*TestCase
}

func NewRepeater

func NewRepeater(inputFuncList []*InputFunc, inputParamList []*InputParam) *Repeater

NewRepeater get a new object

func (*Repeater) Init

func (r *Repeater) Init(inputFuncList []*InputFunc, inputParamList []*InputParam)

Init init

func (*Repeater) Process

func (r *Repeater) Process()

Process run all test cases

func (*Repeater) Render

func (r *Repeater) Render() string

Render render result to a string

func (*Repeater) ToCsv

func (r *Repeater) ToCsv(path string) error

ToCsv output result to csv file

type TestCase

type TestCase struct {
	Name         string
	Func         func() error
	Concurrence  int64
	TotalCount   int64
	SuccessCount int64
	Total        float64
	Avg          float64
	P95          float64
	P99          float64
	IsFinish     bool
}

TestCase Single test case

func (*TestCase) Init

func (c *TestCase) Init(inputFunc *InputFunc, inputParam *InputParam)

Init init

func (*TestCase) Process

func (c *TestCase) Process()

Process run stress test, and save result

Jump to

Keyboard shortcuts

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