-
Notifications
You must be signed in to change notification settings - Fork 245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Intelligent addition or reduction of workers #193
Conversation
because use of context, the dependent version needs to be upgraded. |
log.Println("The total number of clients required is ", spawnCount) | ||
|
||
var gapCount int | ||
if spawnCount > int(r.numClients) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should consider the weight of tasks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在setTask的时候根据权重生成一个runTask []*Task
每个活跃的goroutine将一直getTask 从runTask中获取任务并执行任务
关于权重这块提交了代码,请查阅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
嗯,你是对的,直接用 getTask 来管权重就好。
log.Println("The total number of clients required is ", spawnCount) | ||
|
||
var gapCount int | ||
if spawnCount > int(r.numClients) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
嗯,你是对的,直接用 getTask 来管权重就好。
@@ -30,6 +30,7 @@ type runner struct { | |||
|
|||
tasks []*Task | |||
totalTaskWeight int | |||
runTask []*Task // goroutine execute tasks according to the list |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为啥要维护这个数组?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
维护这个数组里面存放的需要循环执行的任务(按照权重排序。例如:A-2 B-3 C-4,排序后ABCABCBCC),每个活跃的线程都按照这个顺序执行任务。个人理解可能比随机要好点。
能保证任务执行的权重,也能保证任务都能执行。
runner.go
Outdated
stopChan chan bool | ||
// Cancellation method for all running workers(goroutines) | ||
cancelFuncs []context.CancelFunc | ||
mu sync.Mutex |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
避免用 mutex,维护的心智负担有点大。有状态机来保证执行顺序了。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里看起来可以直接删除锁机制
理由:增减线程命令来自于监听chan得到,本身是非并发的。整个操作不引入协程的话,就可以直接删除锁机制了
但是这样是否合理,大佬可以一起帮忙看一下,我现在提交
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我只是不想在这里用锁,具体的实现有没有问题,要看测试用例。
runner.go
Outdated
// stop previous goroutines without blocking | ||
// those goroutines will exit when r.safeRun returns | ||
close(r.stopChan) | ||
go r.spawnWorkers(0, nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
靠 spawn 0 来实现 stop,有点拗口。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果把 spawnWorkers方法名修改为 setwWorkerNumber会不会好点
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
直接用 stop 就好了吧,那怕是在这里遍历所有 context
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是的,这里可以直接调用reduceWorkers 把当前活跃线程总数丢进去
我修改一下
这个 PR,需要补充对应的单元测试用例。 |
我挺想把这块改平滑的,之前比较粗暴。但我之前的想法,是从 locust 那边改起,还没时间弄。 |
Codecov Report
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. @@ Coverage Diff @@
## master #193 +/- ##
==========================================
- Coverage 81.19% 80.60% -0.59%
==========================================
Files 10 10
Lines 1521 1542 +21
==========================================
+ Hits 1235 1243 +8
- Misses 238 248 +10
- Partials 48 51 +3
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 2 files with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
已合并,多谢 |
负载变化时导致并发突增 #173