github.com/link1st/go-stress-testing/stress
import (
"fmt"
// "github.com/dji/go-stress-testing/stress"
"github.com/link1st/go-stress-testing/stress"
"net/http"
"time"
)
go run xxx.go
的时候,报错queries.go:6:2: no required module provides package github.com/link1st/go-stress-testing/stress; to add it:
go get github.com/link1st/go-stress-testing/stress
go get github.com/link1st/go-stress-testing/stress
命令去下载缺少的模块go: github.com/link1st/go-stress-testing/stress: no matching versions for query "upgrade"
go get github.com/link1st/go-stress-testing/stress@latest
go get github.com/link1st/go-stress-testing/stress@v1.0.5
go: github.com/link1st/go-stress-testing/[email protected]: invalid version: unknown revision stress/v1.0.5
$GOPATH/pkg/mod/
目录下github.com/link1st/go-stress-testing/stress
的项目源码
git checkout v1.0.5
go build
go build -o /Users/duzhihao/go/pkg/mod/github.com/link1st/go-stress-testing/[email protected] main.go
github.com/link1st/go-stress-testing/stress
github.com
:模块来源于 githublink1st
:作者是 link1stgo-stress-testing
:归属于项目 go-stress-testingstress
:项目下的 stress 模块通过别的方式找到了stress模块下的源码,一共有四个文件
package stress
import (
"context"
"errors"
)
// Task represents a stress testing task, which consists of a name, a RunFunc function and optional StopFunc function
type Task struct {
Name string
RunFunc func() error
StopFunc func() error
Context context.Context
Cancel context.CancelFunc
}
// Run runs the Task and returns a TestResult
func (t *Task) Run() *TestResult {
var requests uint64
var errors uint64
for {
select {
case <-t.Context.Done():
return &TestResult{
Requests: requests,
Errors: errors,
}
default:
if err := t.RunFunc(); err != nil {
errors++
}
requests++
}
}
}
// NewTask returns a new Task with the given name and RunFunc function
func NewTask(name string, runFunc func() error) *Task {
ctx, cancel := context.WithCancel(context.Background())
return &Task{
Name: name,
RunFunc: runFunc,
Context: ctx,
Cancel: cancel,
}
}
// NewTimedTask returns a new Task with the given name, RunFunc function and duration
func NewTimedTask(name string, runFunc func() error, duration int) *Task {
ctx, cancel := context.WithTimeout(context.Background(), Duration(duration)*time.Second)
return &Task{
Name: name,
RunFunc: runFunc,
StopFunc: cancel,
Context: ctx,
Cancel: cancel,
}
}
// Stop stops the Task
func (t *Task) Stop() error {
if t.StopFunc == nil {
return errors.New("Stop function is not defined")
}
return t.StopFunc()
}
package stress
import (
"sync"
"sync/atomic"
"time"
)
// Scene represents a stress testing scene, which consists of multiple tasks
type Scene struct {
Name string
Frequency time.Duration
Duration time.Duration
SetupFunc func() error
CleanupFunc func() error
tasks []*Task
}
// AddTask adds a new Task to the Scene
func (s *Scene) AddTask(task *Task) {
s.tasks = append(s.tasks, task)
}
// Run runs the Scene and returns a TestResult
func (s *Scene) Run() *TestResult {
resCh := make(chan *TestResult)
wg := sync.WaitGroup{}
wg.Add(len(s.tasks))
for _, task := range s.tasks {
go func(task *Task) {
defer wg.Done()
resCh <- task.Run()
}(task)
}
if s.SetupFunc != nil {
if err := s.SetupFunc(); err != nil {
panic(err)
}
}
ticker := time.NewTicker(s.Frequency)
stop := make(chan bool)
go func() {
time.Sleep(s.Duration)
close(stop)
}()
var requests uint64
var errors uint64
loop:
for {
select {
case <-ticker.C:
for _, task := range s.tasks {
for i := 0; i < requestsPerTask; i++ {
if err := task.RunFunc(); err != nil {
atomic.AddUint64(&errors, 1)
}
atomic.AddUint64(&requests, 1)
}
}
case res := <-resCh:
atomic.AddUint64(&requests, res.Requests)
atomic.AddUint64(&errors, res.Errors)
case <-stop:
break loop
}
}
ticker.Stop()
if s.CleanupFunc != nil {
if err := s.CleanupFunc(); err != nil {
panic(err)
}
}
wg.Wait()
elapsed := time.Duration(requests) * time.Second / time.Duration(rps)
return &TestResult{
Requests: requests,
Errors: errors,
Rps: rps,
Elapsed: elapsed,
}
}
// NewScene returns a new Scene with the given name and default settings
func NewScene(name string) *Scene {
return &Scene{
Name: name,
Frequency: time.Second,
Duration: 10 * time.Second,
SetupFunc: func() error {
return nil
},
CleanupFunc: func() error {
return nil
},
}
}
package stress
import "time"
// TestResult represents the result of a stress test
type TestResult struct {
Requests uint64
Errors uint64
Rps uint64
Elapsed time.Duration
}
package stress
import "time"
const (
requestsPerTask = 100
rps = 100
)
// Duration is an int that can be used with the time package
type Duration int
func (d Duration) String() string {
return time.Duration(d * time.Second).String()
}
需要注意的是,这几个go文件代码并不可靠,实际应用中还需要根据需求进行逻辑适配。