Merge branch 'master' of https://github.com/httprunner/httprunner into refactor-protocol

This commit is contained in:
debugtalk
2022-03-25 09:37:44 +08:00
3 changed files with 16 additions and 8 deletions

View File

@@ -78,7 +78,8 @@ func (b *Boomer) GetDisableCompression() bool {
// SetLoopCount set loop count for test. // SetLoopCount set loop count for test.
func (b *Boomer) SetLoopCount(loopCount int64) { func (b *Boomer) SetLoopCount(loopCount int64) {
b.localRunner.loop = &Loop{loopCount: loopCount} // total loop count for testcase, it will be evenly distributed to each worker
b.localRunner.loop = &Loop{loopCount: loopCount * int64(b.localRunner.spawnCount)}
} }
// AddOutput accepts outputs which implements the boomer.Output interface. // AddOutput accepts outputs which implements the boomer.Output interface.

View File

@@ -65,7 +65,7 @@ type runner struct {
currentClientsNum int32 // current clients count currentClientsNum int32 // current clients count
spawnCount int // target clients to spawn spawnCount int // target clients to spawn
spawnRate float64 spawnRate float64
loop *Loop // specify running cycles loop *Loop // specify loop count for testcase, count = loopCount * spawnCount
spawnDone chan struct{} spawnDone chan struct{}
outputs []Output outputs []Output
@@ -189,6 +189,12 @@ func (r *localRunner) spawnWorkers(spawnCount int, spawnRate float64, quit chan
sleepTime := time.Duration(1000000/r.spawnRate) * time.Microsecond sleepTime := time.Duration(1000000/r.spawnRate) * time.Microsecond
time.Sleep(sleepTime) time.Sleep(sleepTime)
// loop count per worker
var workerLoop *Loop
if r.loop != nil {
workerLoop = &Loop{loopCount: atomic.LoadInt64(&r.loop.loopCount) / int64(r.spawnCount)}
}
select { select {
case <-quit: case <-quit:
// quit spawning goroutine // quit spawning goroutine
@@ -202,7 +208,7 @@ func (r *localRunner) spawnWorkers(spawnCount int, spawnRate float64, quit chan
case <-quit: case <-quit:
return return
default: default:
if r.loop != nil && !r.loop.acquire() { if workerLoop != nil && !workerLoop.acquire() {
return return
} }
if r.rateLimitEnabled { if r.rateLimitEnabled {
@@ -215,8 +221,11 @@ func (r *localRunner) spawnWorkers(spawnCount int, spawnRate float64, quit chan
task := r.getTask() task := r.getTask()
r.safeRun(task.Fn) r.safeRun(task.Fn)
} }
if r.loop != nil { if workerLoop != nil {
// finished count of total
r.loop.increaseFinishedCount() r.loop.increaseFinishedCount()
// finished count of single worker
workerLoop.increaseFinishedCount()
if r.loop.isFinished() { if r.loop.isFinished() {
r.stop() r.stop()
} }

View File

@@ -98,7 +98,7 @@ func TestLoopCount(t *testing.T) {
taskA := &Task{ taskA := &Task{
Weight: 10, Weight: 10,
Fn: func() { Fn: func() {
time.Sleep(time.Second) time.Sleep(time.Millisecond)
}, },
Name: "TaskA", Name: "TaskA",
} }
@@ -107,9 +107,7 @@ func TestLoopCount(t *testing.T) {
runner.loop = &Loop{loopCount: 4} runner.loop = &Loop{loopCount: 4}
runner.setTasks(tasks) runner.setTasks(tasks)
go runner.start() go runner.start()
ticker := time.NewTicker(4 * time.Second) <-runner.stopChan
defer ticker.Stop()
<-ticker.C
if !assert.Equal(t, runner.loop.loopCount, atomic.LoadInt64(&runner.loop.finishedCount)) { if !assert.Equal(t, runner.loop.loopCount, atomic.LoadInt64(&runner.loop.finishedCount)) {
t.Fail() t.Fail()
} }