1 Star 0 Fork 0

scgg/GPool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
pool_test.go 1.34 KB
一键复制 编辑 原始数据 按行查看 历史
scgg 提交于 2021-12-22 17:24 +08:00 . 负载生成器
package GPool
import (
"fmt"
"net/http"
_ "net/http/pprof"
"sync"
"testing"
"time"
)
func add(args ...interface{}) {
//if len(args) != 2 {
// return
//}
a := args[0].(int)
b := args[1].(int)
fmt.Println("a =",a," b =", b," a + b =", a + b)
time.Sleep(1 * time.Second)
}
func TestGoPool(t *testing.T) {
pool := NewGoPool(100)
now := time.Now()
for i := 0; i < 50; i++ {
for j := 0; j < 50; j++ {
go pool.AddTask(add, []interface{}{i, j})
}
}
pool.Run2()
pool.Wait()
pool.Close()
fmt.Println(time.Now().Sub(now))
}
func TestPProf(t *testing.T) {
go func(){
for {
TestGoPool(t)
}
}()
http.ListenAndServe("0.0.0.0:6060", nil)
}
func TestGoPoolFortaskChain(t *testing.T) {
pool := NewPoolForTaskChain(100)
now := time.Now()
for i := 0; i < 50; i++ {
for j := 0; j < 50; j++ {
pool.AddTaskToTQ(add, []interface{}{i, j})
}
}
pool.Run_taskChain()
fmt.Println(time.Now().Sub(now))
}
type sub struct {
a, b int
}
func (s *sub) Do() {
fmt.Println(s.a + s.b)
time.Sleep(1 * time.Second)
}
func TestWorkPool(t *testing.T) {
pool := NewWorkPool(100)
now := time.Now()
var wg sync.WaitGroup
for i := 0; i < 50; i++ {
for j := 0; j < 50; j++ {
wg.Add(1)
np := &sub{
a: i,
b: j,
}
go func() {
pool.Run(np)
wg.Done()
}()
}
}
wg.Wait()
pool.Close()
fmt.Println(time.Now().Sub(now))
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/scgg/gpool.git
[email protected]:scgg/gpool.git
scgg
gpool
GPool
master

搜索帮助